Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.

Similar presentations


Presentation on theme: "Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort."— Presentation transcript:

1 Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort

2 Mudasser Naseer 2 3/4/2016 Bubblesort l Works by repeatedly swapping adjacent elements that are out of order. l Falls in the category of exchange sorts. Bubblesort(A) 1for i = 1 to length[A] 2 do for j = length[A] downto i + 1 3 do if A[ j ] < A[ j − 1] 4 then exchange A[ j ] ↔ A[ j − 1]

3 Bubblesort Example l Let the array A={60,42,75,83,27} Mudasser Naseer 3 3/4/2016 60 27 42 2760 75 2742 322775 2732

4 Bubblesort Example 27 6032 426042 754260 3275 Mudasser Naseer 4 3/4/2016 One Pass Array after Completion of Each Pass 60 27 42 2760 75 2742 322775 2732

5 Mudasser Naseer 5 3/4/2016 Bubblesort l Sorts in place l Sorts O(n 2 ) comparisons and swaps l Adaptive: O(n) when nearly sorted

6 Mudasser Naseer 6 3/4/2016 Quicksort Another divide-and-conquer algorithm l Divide: The array A[p..r] is partitioned into two (possibly-empty) subarrays A[p..q-1] and A[q+1..r] n Invariant: All elements in A[p..q-1] are less than all elements in A[q+1..r] l Conquer: The subarrays are recursively sorted by calls to quicksort l Combine: No combining step: two subarrays form an already-sorted array

7 Mudasser Naseer 7 3/4/2016 Partition l Clearly, all the action takes place in the partition() function n Rearranges the subarray in place n End result: u Two subarrays u All values in first subarray  all values in second subarray n Returns the index of the “pivot” element separating the two subarrays l How do you suppose we implement this function?

8 Mudasser Naseer 8 3/4/2016 Quicksort Code Quicksort(A, p, r) 1 if (p < r) 2 q = Partition(A, p, r); 3 Quicksort(A, p, q–1); 4 Quicksort(A, q+1, r); Initially p = 1 and r = n

9 Mudasser Naseer 9 3/4/2016 Partition In Words l Partition(A, p, r): n Select an element to act as the “pivot” (which?) n Grow two regions, A[p..i] and A[i+1..j-1] u All elements in A[p..i] <= pivot u All elements in A[i+1..j-1] > pivot

10 Mudasser Naseer 10 3/4/2016 Partition Code Partition(A, p, r) 1x = A[r] 2i = p - 1 3for j = p to r - 1 4if A[j] <= x 5i = i+1 6exchange A[i] with A[j] 7exchange A[i+1] with A[r] 8return i+1 Illustrate on A = {2, 8, 7, 1, 3, 5, 6, 4}; What is the running time of partition() ?

11 Mudasser Naseer 11 3/4/2016 Partition Example

12 Mudasser Naseer 12 3/4/2016 Partition Example

13 Correctness of Quiksort l As the procedure executes, the array is partitioned into four regions, some of which may be empty, Thus we can state loop invariant as: l Loop invariant: 1. All entries in A[p.. i ] are ≤ pivot. 2. All entries in A[i + 1.. j − 1] are > pivot. 3. A[r ] = pivot. Mudasser Naseer 13 3/4/2016

14 Correctness of Quiksort l Use the loop invariant to prove correctness of Partition. l Initialization: Before the loop starts, all the conditions of the loop invariant are satisfied, because r is the pivot and the subarrays A[p.. i ] and A[i +1.. j −1]are empty. l Maintenance: While the loop is running, if A[ j ] ≤ pivot, then A[ j ] and A[i +1] are swapped and then i and j are incremented. If A[ j ] > pivot, then increment only j. l Termination: When the loop terminates, j = r, so all elements in A are partitioned into one of the three cases: A[p.. i ] ≤ pivot, A[i + 1.. r − 1] > pivot, and A[r ] = pivot. Mudasser Naseer 14 3/4/2016

15 Mudasser Naseer 15 3/4/2016 Quicksort l Sorts in place l Sorts O(n lg n) in the average case l Sorts O(n 2 ) in the worst case l So why would people use it instead of merge sort? l Running time: n Worst case: T(n)= T(n-1) +  (n) =  (n 2 ) n Best case: T(n) = 2 T(n/2) + O(n) =  (n log n) n Average case: T(9n/10) + T(n/10) + n =  (n log n)


Download ppt "Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort."

Similar presentations


Ads by Google