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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full Document Right Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full Document Right Arrow Icon
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?
Background image of page 4
5 Utilizing it… // Data … vector<double> data; // fill in the data… // Instantiate the sort object… CBubbleSort<double> sort; sort.Sort(&data[0], data.size());
Background image of page 5

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

View Full Document Right Arrow Icon
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?
Background image of page 6
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}
Background image of page 7

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

View Full Document Right Arrow Icon
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}
Background image of page 8
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
Background image of page 9

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

View Full Document Right Arrow Icon
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
Background image of page 10
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
Background image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
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 Right Arrow Icon
Ask a homework question - tutors are online