Download presentation
Presentation is loading. Please wait.
1
Quicksort analysis Bubble sort
Sorting Quicksort analysis Bubble sort November 20, 2017 Hassan Khosravi / Geoffrey Tien
2
Hassan Khosravi / Geoffrey Tien
Quicksort analysis How long does Quicksort take to run? Let's consider the best and the worst case These differ because the partitioning algorithm may not always do a good job Let's look at the best case first Each time a sub-array is partitioned the pivot is the exact midpoint of the slice (or as close as it can get) So it is divided in half What is the running time? November 20, 2017 Hassan Khosravi / Geoffrey Tien
3
Hassan Khosravi / Geoffrey Tien
Quicksort best case Running time Each sub-array is divided in half in each partition Each time a series of sub-arrays are partitioned 𝑛 (approximately) comparisons are made The process ends once all the sub-arrays left to be partitioned are of size 1 How many times does 𝑛 have to be divided in half before the result is 1? log 2 𝑛 times Quicksort performs 𝑛∙ log 2 𝑛 operations in the best case Same complexity as Merge sort November 20, 2017 Hassan Khosravi / Geoffrey Tien
4
Hassan Khosravi / Geoffrey Tien
Quicksort worst case Every partition step ends with no values on one side of the pivot The array has to be partitioned 𝑛 times, not log 2 𝑛 times 𝑛 comparisons in the first partition step… 𝑛−1 comparisons in the second step… 𝑛−2 comparisons in the third step… … 𝑛+ 𝑛−1 + 𝑛−2 +…+2+1= 𝑛(𝑛−1) 2 So in the worst case Quicksort performs around 𝑛 2 operations The worst case usually occurs when the array is nearly sorted (in either direction) As bad as selection sort! November 20, 2017 Hassan Khosravi / Geoffrey Tien
5
Quicksort average case
With a large array we would have to be very, very unlucky to get the worst case Unless there was some reason for the array to already be partially sorted The average case is much more like the best case than the worst case There is an easy way to fix a partially sorted array to that it is ready for Quicksort Randomize the positions of the array elements! What is the complexity of performing a random scramble of the array? November 20, 2017 Hassan Khosravi / Geoffrey Tien
6
Quicksort average case
Intuition Let’s assume that pivot choice is random Half the time the pivot will be from the centre half of the array Thus at worst the split will be 𝑛 4 and 3𝑛 4 We can apply this to the notion of a good split Every "good" split: 2 partitions of size 𝑛/4 and 3𝑛/4 Or, divides 𝑛 by 4/3 Expected number of partitions is at most 2∙ log 4/3 𝑛 𝑂 log 𝑛 Given 𝑛 comparisons at each partitioning step, we have Θ 𝑛 log 𝑛 Same best-case complexity as Merge sort, but in practice, Quicksort tends to be slightly faster. Why? November 20, 2017 Hassan Khosravi / Geoffrey Tien
7
Merge sort vs Quicksort
and Quicksort summary If Quicksort worst case is so bad, why use it? worst case is exceedingly rare and can be easily avoided in practice, faster than Merge sort. can be sorted in-place using 𝑂 log 𝑛 stack space also highly parallelizable Name Best Average Worst Stable Memory Selection sort 𝑂 𝑛 2 challenging 𝑂 1 Insertion sort 𝑂 𝑛 Yes Merge sort 𝑂 𝑛 log 𝑛 Quicksort 𝑂 log 𝑛 November 20, 2017 Hassan Khosravi / Geoffrey Tien
8
Hassan Khosravi / Geoffrey Tien
Bubble sort Bubble sort is a simple sorting algorithm with a good best case performance, but generally poor performance in average and worst case Scans the array from left to right checks adjacent pairs and swaps if out of order If any swap was performed during a scan, re-scan the array The next largest item is put into place during each scan, so each scan does not need to go all the way to the end of the array Array is sorted if a scan can complete without performing any swaps November 20, 2017 Hassan Khosravi / Geoffrey Tien
9
Hassan Khosravi / Geoffrey Tien
Bubble sort Example swapped = TRUE FALSE 6 5 3 1 8 7 2 4 i swapped = FALSE TRUE 5 3 1 6 7 2 4 8 i swapped = TRUE FALSE 3 1 5 6 2 4 7 8 i swapped = TRUE FALSE 1 3 5 2 4 6 7 8 i swapped = FALSE TRUE 1 3 2 4 5 6 7 8 i swapped = TRUE FALSE 1 2 3 4 5 6 7 8 Completed scan with no swaps i November 20, 2017 Hassan Khosravi / Geoffrey Tien
10
Hassan Khosravi / Geoffrey Tien
Bubble sort algorithm #define TRUE 1 #define FALSE 0 void bubbleSort(int arr[], int size) { int i, j, temp; int swapped = FALSE; for (i = 0; i < n; i++) { swapped = FALSE; // reset flag for this scan iteration for (j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { temp = arr[j+1]; arr[j+1] = arr[j]; arr[j] = temp; swapped = TRUE; // a swap occurred during this scan } if (!swapped) return; November 20, 2017 Hassan Khosravi / Geoffrey Tien
11
Hassan Khosravi / Geoffrey Tien
Bubble sort analysis Inner loop increments and swaps Outer loop repeats if a swap occurred if no swap occurred, algorithm terminates Best case – everything already in order! An item out of place can only move to the left by one position per outer loop iteration if the smallest item is at the rightmost position, it requires 𝑛−1 outer loop iterations – worst case! Average case is more similar to worst case 𝑂 𝑛 𝑂 𝑛 2 November 20, 2017 Hassan Khosravi / Geoffrey Tien
12
When do we use Bubble sort
Rarely, in practice When we want to show some "bad" (but easy to implement) sorting algorithms for comparison in a second-year computer science class but it is far from the worst, e.g. Bogosort Name Best Average Worst Stable Memory Selection sort 𝑂 𝑛 2 challenging 𝑂 1 Insertion sort 𝑂 𝑛 Yes Merge sort 𝑂 𝑛 log 𝑛 Quicksort 𝑂 log 𝑛 Bubble sort Heapsort 𝑂 𝑛 log 𝑛 No 𝑂 1 November 20, 2017 Hassan Khosravi / Geoffrey Tien
13
Hassan Khosravi / Geoffrey Tien
Exercise Suppose we implemented Merge sort, to split the array into 3 (roughly) equally-sized subarrays, and the Merge function repeatedly copies in the smallest of the three values in each subarray index Perform a recurrence analysis to see how the (asymptotic) running time will be affected. For the following sequence of values, perform a single round of Quicksort partition using the implementation described in these notes; identify which element becomes the pivot value 9, 6, 7, 17, 11, -1, -5, 19, 1, 2 November 20, 2017 Hassan Khosravi / Geoffrey Tien
14
Readings for this lesson
Thareja Chapter 14.11, 14.7 (Quicksort, Bubble sort) Next class: Thareja Chapter 15.1 – 15.3 (Hash tables, hash functions) November 20, 2017 Hassan Khosravi / Geoffrey Tien
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.