{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture02sorts1

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

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

1 Sorting 1 and Analysis CSE 331 Lecture 2

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

View Full Document
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
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

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

View Full Document
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
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?

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

View Full Document
6 Utilizing it… // Data … vector<double> data; // fill in the data… // Instantiate the sort object… CBubbleSort<double> sort; sort.Sort(&data[0], data.size());
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?

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

View Full Document
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}
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}

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

View Full Document
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
11 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

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

View Full Document
12 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
13

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

View Full Document
14 Is BubbleSort O(N 2 )? Can we find a c and n 0 ?: 2 ) 1 ( ) 1 ( ) ( max - + - = N N t N t N T bcd a
15 Is BubbleSort O(N 2 )? Can we find a c and n 0 ?: 2 2 2 2 2 max 2 2 2 2 2 2 ) 1 ( ) 1 ( ) ( N t t N t N t N t N t N t N t t N t N N t N t N T bcd a bcd a bcd a bcd bcd a a bcd a + = + + < - + - = - + - = 2 max ) ( : 2 cN N T Then t t c Let bcd a + =

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

View Full Document
16 Alright… You already knew that, right?
17 Some rules to simplify things… Loops: Running time is body time times # iterations Nested loops: Analysis from the inside out… Statements: Assume time of 1 each Unless the statement is an algorithm

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 ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern