{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
1 Sorting 1 and Analysis CSE 331 Lecture 2
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right 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
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?
Image of page 5

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

View Full Document Right 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());
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?
Image of page 7

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

View Full Document Right 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}
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}
Image of page 9

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

View Full Document Right 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
Image of page 10
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
Image of page 11

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

View Full Document Right Arrow Icon
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
Image of page 12
13
Image of page 13

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

View Full Document Right Arrow Icon
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
Image of page 14
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 + =
Image of page 15

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

View Full Document Right Arrow Icon
16 Alright… You already knew that, right?
Image of page 16
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
Image of page 17

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

View Full Document Right Arrow Icon
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern