lecture02sorts1 - Sorting 1 and Analysis CSE 331 Lecture 2...

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 CSE 331 Lecture 2
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 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 2
3 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 www.cs.oswego.edu/~mohammad/classes/cs c241/samples/sort/Sort2-E.html
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 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 4
5 Templated 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 5

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

View Full DocumentRight Arrow Icon
6 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 6
7 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 7

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

View Full DocumentRight Arrow Icon
8 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 - = = - ∑ ∑ = - = 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 Cont… So, we could say: 2 ) 1 ( ) 1 ( 2 ) 1 ( ) ( ) 1 ( ) ( max - + - = - + + + - = N N t N t N N t t t N t N T bcd a d c b a 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 9

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

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

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

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/25/2008 for the course CSE 331 taught by Professor M.mccullen during the Spring '08 term at Michigan State University.

Page1 / 61

lecture02sorts1 - Sorting 1 and Analysis CSE 331 Lecture 2...

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