# Sorting Quicksort analysis Bubble sort November 20 2017

Sorting Quicksort analysis Bubble sort November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

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 2

Quicksort best case Running time • Same complexity as Merge sort November 20, 2017 Hassan Khosravi / Geoffrey Tien 3

Quicksort worst case • As bad as selection sort! November 20, 2017 Hassan Khosravi / Geoffrey Tien 4

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 5

Quicksort average case Intuition • Same best-case complexity as Merge sort, but in practice, Quicksort tends to be slightly faster. Why? November 20, 2017 Hassan Khosravi / Geoffrey Tien 6

Merge sort vs Quicksort and Quicksort summary • Name Best Average Worst Stable Selection sort challenging Insertion sort Yes Merge sort Yes Quicksort challenging November 20, 2017 Hassan Khosravi / Geoffrey Tien Memory 7

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 complete without performing any swaps November 20, 2017 Hassan Khosravi / Geoffrey Tien 8

Bubble sort Example TRUE swapped = FALSE 6 i 5 3 1 8 7 2 4 TRUE swapped = FALSE 5 3 1 i 6 7 2 4 8 TRUE swapped = FALSE 3 1 5 6 i 2 4 7 8 TRUE swapped = FALSE 1 3 5 2 i 4 6 7 8 TRUE swapped = FALSE 1 3 2 4 5 i 6 7 8 TRUE swapped = FALSE 1 2 3 4 5 6 i 7 8 November 20, 2017 Hassan Khosravi / Geoffrey Tien Completed scan with no swaps 9

Bubble sort algorithm #define TRUE 1 #define FALSE 0 void bubble. Sort(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 10

Bubble sort analysis • November 20, 2017 Hassan Khosravi / Geoffrey Tien 11

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 Selection sort challenging Insertion sort Yes Merge sort Yes Quicksort challenging Bubble sort Yes Heapsort No November 20, 2017 Hassan Khosravi / Geoffrey Tien Memory 12

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, 11, -5, 19, 1, 2 November 20, 2017 Hassan Khosravi / Geoffrey Tien 13

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 14

- Slides: 14