Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Practice Quiz Question
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT.
CPS120: Introduction to Computer Science Searching and Sorting.
CSCE 3110 Data Structures & Algorithm Analysis
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Introduction to Algorithms Chapter 7: Quick Sort.
Sorting Algorithms and Average Case Time Complexity
CMPS1371 Introduction to Computing for Engineers SORTING.
 1 Sorting. For computer, sorting is the process of ordering data. [ ]  [ ] [ “Tom”, “Michael”, “Betty” ]  [ “Betty”, “Michael”,
Algorithm An algorithm is a step-by-step set of operations to be performed. Real-life example: a recipe Computer science example: determining the mode.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
CHAPTER 11 Sorting.
Merge sort, Insertion sort
Sorting Chapter 10.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Sorting Chapter 10. Chapter 10: Sorting2 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn how to implement.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
(c) , University of Washington
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.
CIS 068 Welcome to CIS 068 ! Lesson 9: Sorting. CIS 068 Overview Algorithmic Description and Analysis of Selection Sort Bubble Sort Insertion Sort Merge.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Sorting HKOI Training Team (Advanced)
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Computer Science Searching & Sorting.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
Sorting CS 105 See Chapter 14 of Horstmann text. Sorting Slide 2 The Sorting problem Input: a collection S of n elements that can be ordered Output: the.
Sorting CS 110: Data Structures and Algorithms First Semester,
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
Sort Algorithms.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting – Part II CS 367 – Introduction to Data Structures.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
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.
Data Structures and Algorithms Lecture 17, 18 and 19 (Sorting) Instructor: Quratulain Date: 10, 13 and 17 November, 2009 Faculty of Computer Science, IBA.
ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Quick Sort Modifications By Mr. Dave Clausen Updated for Python.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
CS 367 Introduction to Data Structures Lecture 11.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Prof. U V THETE Dept. of Computer Science YMA
CSCI 104 Sorting Algorithms
Sorting Why? Displaying in order Faster Searching Categories Internal
Warmup What is an abstract class?
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Sorting Algorithms Written by J.J. Shepherd.
Sorting Algorithms Ellysa N. Kosinaya.
8/04/2009 Many thanks to David Sun for some of the included slides!
CSE 326: Data Structures Sorting
CSC 143 Java Sorting.
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Presentation transcript:

Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front of the list Repeat by finding the minimum in the sublist On the i th iteration, find the i th smallest number in the list Keep going until you reach the end of the list 4 Copyright © William C. Cheng

5 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort First pass through the list

Data Structures - CSCI 102 Selection Sort First pass through the list Minimum 6 Copyright © William C. Cheng

7 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort First pass through the list MinimumOutput

Data Structures - CSCI 102 Selection Sort First pass through the list Swap 8 Copyright © William C. Cheng

9 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort First pass through the list UnsortedSorted

Data Structures - CSCI 102 Selection Sort 2nd pass through the list Minimum 10 Copyright © William C. Cheng

Data Structures - CSCI 102 Selection Sort 2nd pass through the list Minimum Stay 11 Copyright © William C. Cheng

12 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort 2nd pass through the list Minimum Stay SortedUnsorted

13 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort 3rd pass through the list MinimumOutput

Data Structures - CSCI 102 Selection Sort 3rd pass through the list Minimum Output Swap 14 Copyright © William C. Cheng

15 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort 3rd pass through the list Minimum Output SortedUnsorted Swap

16 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort 4th pass through the list MinimumOutput

Data Structures - CSCI 102 Selection Sort 4th pass through the list Minimum Output Swap 17 Copyright © William C. Cheng

18 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort 4th pass through the list Minimum Output SortedUnsorted Swap

19 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort 5th pass through the list No 5th pass needed The last element must be sorted by now

20 Data Structures - CSCI 102 Copyright © William C. Cheng Selection Sort void selectionSort(vector & numbers) { for(int i=0; i < numbers.size()-1; i++) { //find the minimum int minValue = numbers[i] int minIndex = i; for(int j=i+1; j < numbers.size(); j++) { if(numbers[j] < minValue) { minValue = numbers[j]; minIndex = j; }}}} //move the minimum into the sorted section swap(numbers[i],numbers[minIndex]); }}}}

None, O(n ) O(n ) What’s the best case scenarios? What’s the Big O for this case? Selection Sort 22 Data Structures - CSCI 102 Copyright © William C. Cheng Selection Sort What’s the worst case scenario? What’s the Big O for this case? What’s the overall Big O? What are the problems with selection sort? Still too many comparisons

Bubble Sort 23 Data Structures - CSCI 102 Copyright © William C. Cheng Bubble Sort Compare adjacent numbers If they’re out of order swap them Otherwise leave them be Loop through the list until there are no more swaps to make Smaller numbers slowly "bubble" up toward the top of the list

24 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list

Data Structures - CSCI 102 Bubble Sort First pass through the list Swap 25 Copyright © William C. Cheng

26 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list Swap

27 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list

Data Structures - CSCI 102 Bubble Sort First pass through the list Stay 28 Copyright © William C. Cheng

29 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list

Data Structures - CSCI 102 Bubble Sort First pass through the list Swap 30 Copyright © William C. Cheng

31 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list Swap

32 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list

Data Structures - CSCI 102 Bubble Sort First pass through the list Swap 33 Copyright © William C. Cheng

34 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list Swap

35 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list

36 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list

37 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 2nd pass through the list

Data Structures - CSCI 102 Bubble Sort 2nd pass through the list Stay 38 Copyright © William C. Cheng

39 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 2nd pass through the list

Data Structures - CSCI 102 Bubble Sort 2nd pass through the list Swap 40 Copyright © William C. Cheng

41 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 2nd pass through the list Swap

42 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 2nd pass through the list

Data Structures - CSCI 102 Bubble Sort 2nd pass through the list Stay 43 Copyright © William C. Cheng

44 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 2nd pass through the list

45 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 2nd pass through the list

46 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 3rd pass through the list

Data Structures - CSCI 102 Bubble Sort 3rd pass through the list Swap 47 Copyright © William C. Cheng

48 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 3rd pass through the list Swap

49 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 3rd pass through the list

Data Structures - CSCI 102 Bubble Sort 3rd pass through the list Stay 50 Copyright © William C. Cheng

51 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 3rd pass through the list

52 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 4th pass through the list

Data Structures - CSCI 102 Bubble Sort 4th pass through the list Stay 53 Copyright © William C. Cheng

54 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 4th pass through the list

55 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 4th pass through the list During the 4th pass, nothing got swapped This means that the unsorted part is actually sorted No more pass needed

56 Data Structures - CSCI 102 Copyright © William C. Cheng Bubble Sort void naiveBubbleSort(vector & numbers) { for(int i=0; i < numbers.size()-1; i++) { for(int j=0; j < numbers.size()-1; j++) { if(numbers[j] > numbers[j+1]) { swap(numbers[j],numbers[j+1]); }}}}}}}} Naive Bubble Sort

BubbleSort(n) = O(n ) 2) Take advantage of the fact that on the i iteration, 58 Data Structures - CSCI 102 Copyright © William C. Cheng Bubble Sort Naive Bubble Sort What’s the Big O for this bubble sort? How could we improve this algorithm? 2 1) Make it terminate early if the sorting is already done th the last i elements are sorted

List in Reverse Order, O(n ) O(n ) 61 Data Structures - CSCI 102 Copyright © William C. Cheng Bubble Sort Better Bubble Sort What’s the overall Big O? What are the problems with bubble sort? What’s the best case scenarios? What’s the Big O for this case? What’s the worst case scenario? What’s the Big O for this case? Already Sorted, O(n) 2 2 Too much swapping! Too many comparisons!

On the i iteration, i items will be sorted Keep the list separated into sorted and unsorted sections Insertion Sort 62 Data Structures - CSCI 102 Copyright © William C. Cheng Insertion Sort Take each number in the unsorted portion of the list and shift it over into the sorted list th Keep going until you reach the end of the list "Bubble" to the left until finding the right place

63 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort First pass through the list SortedUnsorted

Data Structures - CSCI 102 Insertion Sort First pass through the list Swap 64 Copyright © William C. Cheng

65 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort First pass through the list

66 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 2nd pass through the list SortedUnsorted

67 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 2nd pass through the list

Data Structures - CSCI 102 Insertion Sort 2nd pass through the list Stay 68 Copyright © William C. Cheng

69 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 2nd pass through the list

70 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 3rd pass through the list SortedUnsorted

71 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 3rd pass through the list

Data Structures - CSCI 102 Insertion Sort 3rd pass through the list Swap 72 Copyright © William C. Cheng

73 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 3rd pass through the list

Data Structures - CSCI 102 Insertion Sort 3rd pass through the list Swap 74 Copyright © William C. Cheng

75 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 3rd pass through the list

Data Structures - CSCI 102 Insertion Sort 3rd pass through the list Swap 76 Copyright © William C. Cheng

77 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 3rd pass through the list

Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 4th pass through the list SortedUnsorted

Data Structures - CSCI 102 Insertion Sort 4th pass through the list Swap 79 Copyright © William C. Cheng

80 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 4th pass through the list

Data Structures - CSCI 102 Insertion Sort 4th pass through the list Stay 81 Copyright © William C. Cheng

82 Data Structures - CSCI 102 Insertion Sort 4th pass through the list Too many swaps! Since the left part is sorted, just need to find a place to insert the value (16 in this case) Need to remember the value 16 Copyright © William C. Cheng

In Reverse Order, O(n ) O(n ) 115 Data Structures - CSCI 102 Copyright © William C. Cheng Insertion Sort What’s the best case scenarios? What’s the Big O for this case? Already Sorted, O(n) What’s the worst case scenario? What’s the Big O for this case? 2 What’s the overall Big O? What are the problems with insertion sort? 2 Still have to use nested loops

Divide 4 Data Structures - CSCI 102 Copyright © William C. Cheng Merge Sort Divide the N-element sequence to be sorted into two subsequences of N/2 elements each Conquer Sort the two subsequences recursively using merge sort Combine Merge the two sorted subsequences to produce the a single sorted result. Repeat.

5 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort First pass through the list

6 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort Divide

7 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort

8 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort Divide

9 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort

10 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort Divide

11 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort

12 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort

13 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort Merge

14 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort Merge

15 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort

16 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort Merge

17 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort

18 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort Merge

19 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort

20 Data Structures - CSCI 102 Merge Sort What functions will we need to do merge sort? Need a function to split list in half Need a function to merge two sorted lists Most of the work in the merge sort algorithm is done in the "merge" method Precondition: Given two sorted arrays Postcondition: Need a single, merged sorted array How does it work? Please note that Merge Sort is not an in-place sorting algorithm It uses an output buffer whose size is on the order of the size of the input buffer, i.e., O(n) extra space Merge Sort may not be practical for some applications Copyright © William C. Cheng

21 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist

22 Copyright © William C. Cheng Left Sublist Right Sublist Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Right is smaller Copy right to output

23 Copyright © William C. Cheng Left Sublist Right Sublist Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 3

24 Copyright © William C. Cheng Left Sublist Right Sublist Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 3

25 Copyright © William C. Cheng Left Sublist Right Sublist Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 3 Left is smaller Copy left to output

26 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist 35

27 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist 35

28 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist 35 Left is smaller Copy left to output

29 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist 357

30 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist 357

31 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist 357 Right is smaller Copy right to output

32 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist 3578

33 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist 3578

34 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist 3578 Left is smaller Copy left to output

35 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist

36 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist

37 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist Right is smaller Copy right to output

38 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist

39 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist

40 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist Right is smaller Copy right to output 16

Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist 41 Copyright © William C. Cheng

Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist Copy all the remaining items from the left sublist to the output 42 Copyright © William C. Cheng

Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Left Sublist Right Sublist Copy all the remaining items from the left sublist to the output Done 43 Copyright © William C. Cheng

What’s the best case scenarios? What’s the Big O for this case? Merge Sort 47 Data Structures - CSCI 102 Copyright © William C. Cheng Merge Sort What’s the worst case scenario? What’s the Big O for this case? What’s the overall Big O? What are the problems with merge sort? None, O(n log n) O(n log n)

Stable Sort (generally) Maintains original ordering of equal elements The Good 48 Data Structures - CSCI 102 Copyright © William C. Cheng Merge Sort Fairly easy to code Useful for lists that must be accessed sequentially e.g. Linked Lists Sorting in place is really difficult The Bad O(N) additional space complexity if not in place There are better in-place sorts Lots of copying data

51 Data Structures - CSCI 102 Copyright © William C. Cheng Quick Sort Pick an element in the array and call it Pivot Divide Split the rest of the array into two subarrays: One array containing all numbers <= Pivot One array containing all numbers > Pivot Sort the two new arrays recursively using quick sort Conquer The arrays are sorted in place. No recombination necessary. Combine We will simply use the last element

52 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort The idea behind the Divide part

Data Structures - CSCI 102 Quick Sort The idea behind the Divide part 1) Choose the last element in the array as the pivot It doesn’t move (until the very end) So, we will pin it where it’s at for now 53 Copyright © William C. Cheng

Data Structures - CSCI 102 Quick Sort The idea behind the Divide part Pivot 1) Choose the last element in the array as the pivot 2) Identify all the numbers smaller than the pivot (in blue) Identify all the numbers larger than the pivot (in orange) 54 Copyright © William C. Cheng

Data Structures - CSCI 102 Quick Sort The idea behind the Divide part Pivot 1) Choose the last element in the array as the pivot 2) Identify all the numbers smaller than the pivot (in blue) Identify all the numbers larger than the pivot (in orange) 3) Move all the numbers smaller than the pivot to the left Move all the numbers larger than the pivot to the right 55 Copyright © William C. Cheng

Data Structures - CSCI 102 Quick Sort The idea behind the Divide part Pivot 1) Choose the last element in the array as the pivot 2) Identify all the numbers smaller than the pivot (in blue) Identify all the numbers larger than the pivot (in orange) 3) Move all the numbers smaller than the pivot to the left Move all the numbers larger than the pivot to the right 4) Move the pivot between the blue and the orange numbers 56 Copyright © William C. Cheng

57 Data Structures - CSCI 102 Quick Sort The idea behind the Divide part final sort order Copyright © William C. Cheng Pivot 1) Choose the last element in the array as the pivot 2) Identify all the numbers smaller than the pivot (in blue) Identify all the numbers larger than the pivot (in orange) 3) Move all the numbers smaller than the pivot to the left Move all the numbers larger than the pivot to the right 4) Move the pivot between the blue and the orange numbers The pivot is now at the correct position in the

58 Data Structures - CSCI 102 Quick Sort The idea behind the Divide part final sort order Copyright © William C. Cheng Pivot QuickSort this 1) Choose the last element in the array as the pivot 2) Identify all the numbers smaller than the pivot (in blue) Identify all the numbers larger than the pivot (in orange) 3) Move all the numbers smaller than the pivot to the left Move all the numbers larger than the pivot to the right 4) Move the pivot between the blue and the orange numbers The pivot is now at the correct position in the

59 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort How to do it?

Data Structures - CSCI 102 Quick Sort How to do it? Pivot Step (1) is easy 60 Copyright © William C. Cheng

Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? PivotUnsorted Keep array split into 3 sections: Numbers less than or equal to the pivot (<= 4, in blue) Numbers greater than the pivot (> 4, in orange) Numbers yet to be sorted 65 Copyright © William C. Cheng

Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? PivotUnsorted Keep array split into 3 sections: Numbers less than or equal to the pivot (<= 4, in blue) Numbers greater than the pivot (> 4, in orange) Numbers yet to be sorted 66 Copyright © William C. Cheng

Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? PivotUnsortedLess Than Pivot Keep array split into 3 sections: Numbers less than or equal to the pivot (<= 4, in blue) Numbers greater than the pivot (> 4, in orange) Numbers yet to be sorted 67 Copyright © William C. Cheng

Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? PivotUnsorted Keep array split into 3 sections: Numbers less than or equal to the pivot (<= 4, in blue) Numbers greater than the pivot (> 4, in orange) Numbers yet to be sorted 68 Copyright © William C. Cheng Less Than Pivot More Than Pivot

Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? PivotUnsorted Keep array split into 3 sections: Numbers less than or equal to the pivot (<= 4, in blue) Numbers greater than the pivot (> 4, in orange) Numbers yet to be sorted 69 Copyright © William C. Cheng Less Than Pivot More Than Pivot

Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? PivotUnsortedLess Than Pivot More Than Pivot Swap 70 Copyright © William C. Cheng

71 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? PivotUnsortedLess Than Pivot More Than Pivot

Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? PivotUnsortedLess Than Pivot More Than Pivot Swap 72 Copyright © William C. Cheng

73 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? PivotUnsortedLess Than Pivot More Than Pivot

74 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? Pivot Unsorted Less Than Pivot More Than Pivot

75 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? PivotLess Than Pivot More Than Pivot

Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? PivotLess Than Pivot More Than Pivot Note: we only swap when the number is < pivot When the number is > pivot, we just move along and did nothing 76 Copyright © William C. Cheng

Data Structures - CSCI 102 Quick Sort Step (4) PivotLess Than Pivot More Than Pivot Swap 77 Copyright © William C. Cheng

78 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Step (4) PivotLess Than Pivot More Than Pivot

Data Structures - CSCI 102 Quick Sort Step (4) PivotLess Than Pivot More Than Pivot What if some other cell has the same value as the pivot? Does that cell belong to the blue or the orange section? 79 Copyright © William C. Cheng

Data Structures - CSCI 102 Quick Sort Step (4) PivotLess Than Pivot More Than Pivot What if some other cell has the same value as the pivot? Does that cell belong to the blue or the orange section? The answer is: it does not matter if it is done consistently Arbitrarily, we use: blue is for numbers  pivot 80 Copyright © William C. Cheng

81 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Sort Recursively

82 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Sort Recursively

83 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Sort Recursively

84 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Sort Recursively

85 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Sort Recursively

86 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Sort Recursively

87 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort All done! No merge! Sort Recursively

Sorted or reverse sorted, O(n ) O(n ), but O(n log n) on the average 1) certain inputs can be O(n ) Quick Sort 92 Data Structures - CSCI 102 Copyright © William C. Cheng Quick Sort What’s the best case scenarios? What’s the Big O for this case? What’s the worst case scenario? What’s the Big O for this case? What’s the overall Big O? What are the problems with quick sort? Every partition splits list in half, O(n log n) ) slow on small lists

116 Data Structures - CSCI 102 Copyright © William C. Cheng Extra Material What do sorting algorithms sound like? sorting-algorithms-sound-like/ algorithms/musical_sorting_algorithms.html CSCI 102 YouTube channel Sorting Algorithm Animations