CS1371 25 November 2013

%Admin:

% Please do the CIOS % No Class Wednesday, 27 Nov 13 (Day before Thanksgiving) Take At Home Assignment (Tests and Quizzes) Homework13 (Optional/Extra Credit)

% Final Exam: A: Monday, 9 DEC 6pm B: Tuesday, 10 DEC 6pm C: Wednesday, 11 DEC 6pm D: Thursday, 12 DEC 6pm

% Note on Images Change2Gray = (pic(:,:,1)./3) + (pic(:,:,2)./3) + pic(:,:,3)./3)

% Agenda:

Big O Chapter 16.1

Big O is an algebra that permits us to express how the amount of work done in a problem relates to the amount of data.

Example1: Telephone Book

Example2: Insertion Sort

Example3: Bubble Sort

Big O is the worst case performance

- O(1) - O(N) - O(logN) - O(N^2) - Bubble Sort - O(2^N) - Fibonacci

Sorting Algorithms

% - algorithms only - no code (sortDemo.jar) % - Big O - quick reminder % - sorting questions will appear on the Final % - why not Matlab sort? % - demo using sortdemo.jar % - performance comparison % - use for each algorithm Type Sort Big "O" In-Place Form Page/use Insertion N^2 no Single Loop Page 372/ Used when adding to sorted data Bubble N^2 yes Nested For Loops Page 374/ Used for sorting short vectors, in place Merge NlogN no Recursive Page 378/ used for Recursive merging sorted Merge data Quick NlogN yes Partition Page 378? Used Recursive for random data, Recursive not good for sorted.

Linear Search 1, 2, 3, 4 ,..., N-1, N % Average N+1/2 -> O(N)

Binary Search

Number Items: 2 4 8 16 Number Looks: 1 2 3 4 % N = 2^N -> O(logN)

% Insertion Sort (Efficient for growing with time)

[55 96 97 16 98 96 49] [] [55] [55 96] [55 96 97] [16 55 96 97] [16 55 96 97 98] [16 55 96 96 97 98] [16 49 55 96 96 97 98] % - Big O? - O(N^2)

% Bubble Sort (Good for short vectors in place)

[81 15 43 92 80 96 6] [15 43 81 80 92 6 96] [15 43 80 81 6 92 96] [15 43 80 6 81 92 96] [15 43 6 80 81 92 96] [15 6 43 80 81 92 96] [ 6 15 43 80 81 92 96] % - Big O? O(N^2)

% Merge Sort [ 4 85 94 68 76 75 40 66 18] % - Big O?

% Merge Sort (good for sorted data) [ 4 85 94 68 76 75 40 66 18] [ 4 85 94 68 76] [75 40 66 18] [ 4 85 94] [68 76] [75 40] [66 18] [ 4 85] [94] [68] [76] [75] [40] [66] [18] [ 4 85] [94] [ 4] [85] [ 4 85 94] [68 76] [40 75] [18 66] [ 4 68 76 85 94] [18 40 66 75] [ 4 18 40 66 68 75 76 85 94] % - Big O? O(NlogN)

% Quick Sort [47 44 39 77 80 19 49 45 65]

% Quick Sort (not good if data is already sorted) [47 44 39 77 80 19 49 45 65] [44 39 19 45] [47] [77 80 49 65] [39 19] [44] [45] [47] [49 65] [77] [80] [19] [39] [44] [45] [47] [49] [65] [77] [80] % - Big O? O(NlogN)

Type Sort Big "O" In-Place Form Page/use Insertion N^2 no Single Loop Page 372/ Used when adding to sorted data Bubble N^2 yes Nested For Loops Page 374/ Used for sorting short vectors, in place Merge NlogN no Recursive Page 378/ used for Recursive merging sorted Merge data Quick NlogN yes Partition Page 378? Used Recursive for random data, Recursive not good for sorted.