# lecture02 - Sorting 1 and Analysis Sort problem is obvious...

This preview shows pages 1–12. Sign up to view the full content.

1 Sorting 1 and Analysis Sort problem is obvious to all Several distinct algorithms to solve it Algorithms use different data structures Algorithm performance varies widely See Chapters 1, 2, and 7 For now, skip ShellSort and QuickSort

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Sorting 1 and Analysis Bubble Sort Insertion Sort Merge Sort And analysis as we go… Chapters 1, 2, and 7 For now, skip ShellSort and QuickSort
3 BubbleSort procedure BubbleSort( var A : InputArray; N : int); var j, P : integer; begin for P := N to 2 by -1 do begin for j := 1 to P - 1 do begin if A[ j ] > A[ j + 1 ] then Swap( A[ j ], A[ j + 1 ] ); end; {for} end; {for} end; VisualSort is available in: \\samba\cse331\Examples

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Template BubbleSort class template<class T> class CBubbleSort { public: void Sort(T *A, int N) { for(int P = N-1; P >= 1; P--) { for(int j = 0; j< r P-1; j++) { if(A[j + 1] < A[j]) { T t = A[j + 1]; A[j + 1] = A[j]; A[j] = t; } } } } }; procedure BubbleSort( var A : InputArray; N : int); var j, P : integer; begin for P := N to 2 by -1 do begin for j := 1 to P - 1 do begin if A[ j ] > A[ j + 1 ] then Swap( A[ j ], A[ j + 1 ] ); end; {for} end; {for} end; Differences?
5 Utilizing it… // Data … vector<double> data; // fill in the data… // Instantiate the sort object… CBubbleSort<double> sort; sort.Sort(&data[0], data.size());

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Running time for BubbleSort a for P := N to 2 by -1 do begin b for j := 1 to P - 1 do begin c if A[ j ] > A[ j + 1 ] then d Swap( A[ j ], A[ j + 1 ] ); e end; {for} f end; {for} How many times does c,d get executed?
7 Running time for BubbleSort How many times does c,d get executed? 2 2 ) 1 ( ) 1 ( 2 2 1 1 N N N i i N i N i i - = = - = - = a for P := N to 2 by -1 do begin b for j := 1 to P - 1 do begin c if A[ j ] > A[ j + 1 ] then d Swap( A[ j ], A[ j + 1 ] ); e end; {for} f end; {for}

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 Cont… So, we could say: 2 ) 1 ( ) 1 ( 2 ) 1 ( ) ( ) 1 ( ) ( max - + - = - + + + - = Ν τ Τ βχδ α δ χ β a for P := N to 2 by -1 do begin b for j := 1 to P - 1 do begin c if A[ j ] > A[ j + 1 ] then d Swap( A[ j ], A[ j + 1 ] ); e end; {for} f end; {for}
9 Big-Oh notation Definition: T(N)=O( f(N) ) if there are positive constants c and n 0 such that T(N) <= cf(N) when N >= n 0 Another notation: Intuitively what does this mean? 0 0 ) ( ) ( : , n N N cf N T n c 2200 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
10 Asymptotic Upper Bound f ( n ) g ( n ) c g ( n ) f ( n ) c g ( n ) for all n n 0 g ( n ) is called an asymptotic upper bound of f ( n ). We write f ( n )=O( g ( n )) It reads f ( n ) is big oh of g ( n ). n 0
Example of Asymptotic Upper Bound f ( n )=3 n 2 +5 g ( n )= n 2 4 g ( n )=4 n 2 f(n)=3n 2 +5 <3n 2 +9 ≤3n 2 +n 2 for all n 3 =4n 2 So, for c=4 and n0=3, f(n)≤cg(n) for all n≥n0 Thus, f ( n )=O( g ( n )). 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 60

lecture02 - Sorting 1 and Analysis Sort problem is obvious...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online