Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort

Slides:



Advertisements
Similar presentations
Chapter 9 continued: Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
CSCE 3110 Data Structures & Algorithm Analysis
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
The Substitution method T(n) = 2T(n/2) + cn Guess:T(n) = O(n log n) Proof by Mathematical Induction: Prove that T(n)  d n log n for d>0 T(n)  2(d  n/2.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
Sorting Algorithms and Average Case Time Complexity
CMPS1371 Introduction to Computing for Engineers SORTING.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Quicksort Divide-and-Conquer. Quicksort Algorithm Given an array S of n elements (e.g., integers): If array only contains one element, return it. Else.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
Quicksort.
Quicksort.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
Quicksort
Cmpt-225 Sorting – Part two. Idea of Quick Sort 1) Select: pick an element 2) Divide: partition elements so that x goes to its final position E 3) Conquer:
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
CSE 373 Data Structures Lecture 15
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.
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.
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Sorting CS 110: Data Structures and Algorithms First Semester,
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
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 Data Structures and Algorithms (60-254). Sorting Sorting is one of the most well-studied problems in Computer Science The ultimate reference on.
1 Heapsort, Mergesort, and Quicksort Sections 7.5 to 7.7.
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 - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Chapter 9 sorting. Insertion Sort I The list is assumed to be broken into a sorted portion and an unsorted portion The list is assumed to be broken into.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2010.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Algorithm Design and Analysis (ADA)
Advanced Sorting Methods: Shellsort
Quick Sort (11.2) CSE 2011 Winter November 2018.
Sub-Quadratic Sorting Algorithms
CSE 326: Data Structures Sorting
EE 312 Software Design and Implementation I
CSE 373 Data Structures and Algorithms
Presentation transcript:

Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort Sorting Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort

Motivation of Sorting Sorting algorithms contain interesting and important ideas for code optimization as well as algorithm design.

Merge Sort Last time we talked about Merge Sort Recursively calls: MergeSort(1st half of list) MergeSort(2nd half of list) Then Merges results

Quick Sort This probably the most common sort used in practice, since it is usually the quickest in practice. It uses the idea of a partition, without using an additional array, and recursion to achieve this efficiency.

QuickSort Basically the partition works like this: Given an array of n values you must randomly pick an element in the array to partition by. Once you have picked this value, compare all of the rest of the elements to this value. If they are greater, put them to the “right” of the partition element. If they are less, put them to the “left” of the partition element. So if we sort those 2 sides the whole array will be sorted. 88 35 44 99 71 20 45 42 67 61 35 44 20 42 45 88 61 99 67 71 Still need to be sorted Still need to be sorted In the right spot :D

QuickSort It should be clear that this algorithm will work Thus, similar to MergeSort, we can use a partition to break the sorting problem into two smaller sorting problems. QuickSort at a general level: Partition the array with respect to a random element. Sort the left part of the array, using Quick Sort. Sort the right part of the array, using Quick Sort. It should be clear that this algorithm will work But it may not be clear why it is faster than MergeSort. Like MergeSort it recursively solves 2 sub problems and requires linear additional work. BUT unlike MergeSort the sub problems are NOT guaranteed to be of equal size. The reason that QuickSort is faster is that the partitioning step can actually be performed in place and very efficiently. This efficiency can more than make up for the lack of equal sized recursive calls.

How to Partition in Place 8 3 6 9 2 4 7 5 Element Pivot LOW HIGH Assume for now, that we partition based on the last element in the array, 5. Start 2 counters: Low at array index 0 High at 2nd to last index in the array Advance the Low counter forward until a value greater than the pivot is encountered. Advance the High counter backward until a value less than the pivot is encountered. 8 3 6 9 2 4 7 5 4 3 6 9 2 8 7 5 Element Pivot LOW LOW HIGH HIGH Continue to advance the counters as before. Now, swap these 2 elements, since we know that they are both on the “wrong” side.

How to Partition in Place 4 3 2 5 6 8 7 9 4 3 2 9 6 8 7 5 4 3 6 9 2 8 7 5 Element Pivot LOW LOW HIGH HIGH SWAP When both counters line up, SWAP the last element with the counter position to finish the partition. Now as you can see our array is partitioned into a “left” and a “right”.

Picking the Pivot Although the Partition algorithm works no matter which element is chosen as the pivot, some choices are better than others. Wrong Choice: Just use the first element in the list If the input is random, this is acceptable. BUT, what if the list is already sorted or in reverse order? Then all the elements go into S1 or S2, consistently throughout recursive calls. So it would take O(n2) to do nothing at all! (If presorted) EMBARRASSING!

Picking the Pivot A Safer Way Median-of-Three Partitioning Choose the pivot randomly Generally safe, since it’s unlikely the random pivot would consistently be a poor partition. Random number generation is generally expensive. Median-of-Three Partitioning The best choice would be the median of the array. But that would be hard to calculate and slow. A good estimate is to pick 3 elements and use the median of those as the pivot. The rule of thumb: Pick the left, center, and right elements and take the median as the pivot. 8 1 4 9 6 3 5 2 7 Left Center Element Pivot Right

Analysis of Quicksort Shown on the board

We know the kth smalles is the pivot Quickselect Given an array of n elements, determine the kth smallest element. Clearly k must lie in between 1 and n inclusive The selection problem is different, but related to the sorting problem. The idea is: Partition the array. There are 2 subarrays: One of size m, with the m smallest elements. The other of size n-m-1 . If k ≤ m, we know the kth smallest element is in the 1st partition. If k == m+1, we know the kth smallest element IS the pivot. Otherwise, the kth smallest element is in the 2nd partition. 35 44 20 42 45 88 61 99 67 71 Size m: if (k ≤ m) the kth smallest element is in here. Size n-m-1: if (k > m) the kth smallest element is in here. if (k==m+1) We know the kth smalles is the pivot

Quickselect Algorithm: Quickselect(A, low, high, k): m=Partition(A, low, high) // m is how many values are less than the partition element. if k≤m, return Quickselect(low, low+m-1, k) if k==m+1 return the pivot, A[low+m] else return Quickselect(low+m+1, high, k-m-1) So instead of doing 2 recursive calls, w only make one here. It turns out that on average Quickselect takes O(n) time, which is far better than it’s worst case performace of O(n2) time.

Quickselect Analysis Shown on the board

Shellsort Although this doesn’t have the fastest running time of all the sorting algorithms... It is fairly competitive with Quick and Merge sort for fairly decent sized data sets. The basic idea: Instead of sorting all the elements at once, sort a small set of them Such as every 5th element (you can use insertion sort) Then sort every 3rd element, etc. Finally sort all the elements using insertion sort. Rationale: A small insertion sort is quite efficient. A larger insertion sort can be efficient, if the elements are already “close” to sorted order. By doing the smaller insertion sorts, the elements do get closer to in order before the larger insertions are done.

Shellsort example 12 4 3 9 18 7 2 17 13 1 5 6 12 4 3 9 18 7 2 17 13 1 5 6 Sort every 5th element: 5 2 3 9 1 7 4 17 13 18 12 6 5 2 3 9 1 7 4 17 13 18 12 6 Sort every 3rd element: 4 1 3 5 2 6 9 12 7 18 17 13 Final a normal insertion sort: 1 2 3 4 5 6 7 9 12 13 17 18 Notice that by the time we do this last insertion sort, most elements don’t have a long way to go before being inserted.

Shellsort – choosing increments Do we always do a 5,3,1 sort? No, in general shell sort will ALWAYS work as long as the last “pass” is a 1-sort. What tends to work well is if each of the values in the increment sequence are in a geometric series. i.e. 1,2,4,8,16, etc. The initial passes will be really quick, O(n). It turns out, in practice a geometric ratio of 2.2 produces the best results. The actual average case analysis of this sort is too difficult to derive, But it can be shown with experimental results to indicate an average running time of O(n1.25). Dependent on the gap sequence.

Counting Sort In counting sort, we know that each of the values being sorted are in the range from 0 to m inclusive. We use this information to count the occurrences of each value. Here is the algorithm for sorting an array a[0], …, a[n-1]: Create an auxiliary C, indexed from 0 to m, and initialize each value to 0. Run through the input array A, incrementing the number of occurrences of each value 0 through m by adding +1 to the appropriate index in C. (Thus C is a frequency array) Run through the array C, a 2nd time so that the value stored in each array slot represents the # of elements ≤ the index value in the original array A. Now, run through the original input array A, and for each value in A, use the auxiliary array C, to tell you the proper placement of that value in the sorted input, which be a new array B. Copy the sorted array B into A.

Counting Sort Example Consider the input array to sort. There are values from 0 to 6 Index: 1 2 3 4 5 6 7 A: First, create the frequency array C: Index: 1 2 3 4 5 6 C: Now we want to change C so that each array element stores the # of values less than or equal to the given index - 1: Index: 1 2 3 4 5 6 C: -1 7 Now that C is completed, we can start…

Counting Sort Example Original array: Frequency array: Index: 1 2 3 4 5 6 7 A: Frequency array: Index: 1 2 3 4 5 6 C: -1 7 Now that C is completed, We can start putting the elements in A in their correct spot in B: Start with A[7], which contains 4. Since C[ A[7]] = C[4] = 6, this means there are 6 elements ≤ 4, thus 4 should be place in index 6 of the output array, B[6]: Index: 1 2 3 4 5 6 7 B: Update C: We must decrement C[4] so that the next time we place a 4, it’s in a new location. Index: 1 2 3 4 5 6 C: -1 7

Counting Sort Example Finish on the board

Counting Sort Note: Counting sort is a stable sort Another note: This means that ties in the original input stay in the same relative order after being sorted. For example, the last 4 in the input will be in array index 6 of the output, the second to last 4 in the input will be in array index 5 of the output, and the 4 in array index 3 of the input will be placed in index 4 of the output. So there was no unnecessary switching of equal values. Another note: After getting the frequency array, why didn’t we just loop through each index in C and place each corresponding number in the array A directly? i.e. Since C[1]=1 originally, why not just place a 1 in A[0] and A[1] and move on… The reason is these numbers may be keys with associated data with them, and this approach would place the keys, but not all of the data.

Counting Sort Analysis O(n+k), Best, Worst, and Average runtime where n is the length of the input array and k is the length of the counting array.

Radix Sort The input to this sort must be non-negative integers all of a fixed length of digits. For example, numbers in the range100 – 999 or 1000 – 9999, have numbers of a fixed length of digits. The sort works as follows: Sort the values using a O(n) stable sort on the kth most significant digit. Decrement k by 1. Repeat step 1. (Unless k=0, they you’re done.

Radix Sort Example Unsorted 235 162 734 175 237 674 628 Sort digits Sort tens 628 734 235 237 162 674 175 Sort hundreds 162 175 235 237 628 674 734

Radix Sort Analysis The running time of this sort should be O(nk) Since we do k stable sorts that each run O(n) time. Where k is the number of digits. (A stable sort is one where if two values being sorted, say vi and vj are equal, and vi comes before vj in the unsorted list, then vi will STILL come before vj in the sorted list.) Depending on how many digits the numbers are, this sort can be more efficient than any O(n log n) sort, depending on the range of values.

Radix Sort Question: Would it work the other way around, namely from most significant digit to least significant digit?

A lower time bound for comparison based sorting Shown on the board