Quicksort analysis Bubble sort

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Sorting Chapter 10.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Divide and Conquer Sorting
Prof. U V THETE Dept. of Computer Science YMA
Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort
School of Computing Clemson University Fall, 2012
Insertion sort Loop invariants Dynamic memory
CSCE 210 Data Structures and Algorithms
Sorting Mr. Jacobs.
Fundamental Data Structures and Algorithms
Sorting Why? Displaying in order Faster Searching Categories Internal
Introduction to Search Algorithms
Simple Sorting Algorithms
Algorithms and Data Structures
Divide-And-Conquer-And-Combine
Chapter 7 Sorting Spring 14
Algorithm Analysis CSE 2011 Winter September 2018.
Week 12 - Wednesday CS221.
Teach A level Computing: Algorithms and Data Structures
Cinda Heeren / Geoffrey Tien
Data Structures and Algorithms
CSE 143 Lecture 23: quick sort.
Description Given a linear collection of items x1, x2, x3,….,xn
10.3 Bubble Sort Chapter 10 - Sorting.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Algorithm design and Analysis
Advanced Sorting Methods: Shellsort
Quick Sort (11.2) CSE 2011 Winter November 2018.
CO 303 Algorithm Analysis And Design Quicksort
CSC215 Lecture Algorithms.
Unit-2 Divide and Conquer
Ch 7: Quicksort Ming-Te Chi
Hassan Khosravi / Geoffrey Tien
Sorting Algorithms Ellysa N. Kosinaya.
Lecture 3 / 4 Algorithm Analysis
Hassan Khosravi / Geoffrey Tien
8/04/2009 Many thanks to David Sun for some of the included slides!
Sorting … and Insertion Sort.
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Divide-And-Conquer-And-Combine
Sorting Chapter 8 CS 225.
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
Sorting Chapter 8.
Searching.
EE 312 Software Design and Implementation I
Algorithms and Data Structures
Simple Sorting Algorithms
CSE 373 Data Structures and Algorithms
Ch. 2: Getting Started.
Sorting Chapter 10.
Data Structures & Algorithms
Searching/Sorting/Searching
Simple Sorting Algorithms
Sorting Taking an arbitrary permutation of n items and rearranging them into total order Sorting is, without doubt, the most fundamental algorithmic.
Stacks, Queues, ListNodes
Data Structures and Algorithms CS 244
Advanced Sorting Methods: Shellsort
10.3 Bubble Sort Chapter 10 - Sorting.
the fourth iteration of this loop is shown here
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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