1 CSE 373 Sorting 4: Heap Sort, Bucket Sort, Radix Sort, Stooge Sort reading: Weiss Ch. 7 slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Sorting Part 4 CS221 – 3/25/09. Sort Matrix NameWorst Time Complexity Average Time Complexity Best Time Complexity Worst Space (Auxiliary) Selection SortO(n^2)
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
CSE 373: Data Structures and Algorithms
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE 373: Data Structures and Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CSE 373: Data Structures and Algorithms
1 TCSS 342, Winter 2005 Lecture Notes Sorting Weiss Ch. 8, pp
Sorting Algorithms Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
CSE 373 Data Structures and Algorithms
data ordered along paths from root to leaf
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
Outline Priority Queues Binary Heaps Randomized Mergeable Heaps.
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.
Comparison-Based Sorting & Analysis Smt Genap
Comparison of Optimization Algorithms By Jonathan Lutu.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
Concepts of Algorithms CSC-244 Unit 11 & 12 Sorting (Radix Sort) Shahid Iqbal Lone Computer College Qassim University K.S.A.
CS 261 – Data Structures Hash Tables Hash-like Sorting.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
Lecture 25: Searching and Sorting
Recitation 13 Searching and Sorting.
CSc 110, Autumn 2017 Lecture 37: searching and sorting
CSCE 3100 Data Structures and Algorithm Analysis
slides created by Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Data Structures & Algorithms Priority Queues & HeapSort
CSc 110, Spring 2017 Lecture 39: searching.
Building Java Programs
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
CSc 110, Spring 2017 Lecture 39: searching and sorting
CSE 154 Sorting reading: 13.3, 13.4
Adapted from slides by Marty Stepp and Stuart Reges
slides created by Marty Stepp
slides adapted from Marty Stepp
slides created by Marty Stepp and Ethan Apter
slides created by Marty Stepp
CSE 326: Data Structures Sorting
slides created by Marty Stepp
CSE 143 Sorting reading: 13.3, 13.4.
CSE 332: Data Abstractions Sorting I
CSE 373: Data Structures and Algorithms
Analysis of Algorithms
CSE 373 Data Structures and Algorithms
slides created by Marty Stepp and Hélène Martin
Building Java Programs
slides adapted from Marty Stepp
CSE 373 Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
Stacks, Queues, ListNodes
Presentation transcript:

1 CSE 373 Sorting 4: Heap Sort, Bucket Sort, Radix Sort, Stooge Sort reading: Weiss Ch. 7 slides created by Marty Stepp © University of Washington, all rights reserved.

2 Heap sort heap sort: Arranges elements by adding all of them to a priority queue, then calling remove-min to grab the elements in order.  For an input array a, create a priority queue pq.  For each element in a, add a to pq.  For each index i in a, until pq is empty, remove-min from pq and put it into the next slot a[i].  Takes advantage of the heap's ordering property.  Requires O(N log N) average runtime. Faster with ascending or descending input (less bubbling needed). Faster than shell sort but somewhat slower than merge sort, and harder to parallelize across multiple computers / processors, so generally considered worse than merge sort for general usage.

3 Heap sort code // Sorts the contents of a using heap sort algorithm. public static void heapSort(int[] a) { Queue pq = new PriorityQueue (); for (int k : a) { pq.add(k); } // put the elements back into a, sorted int i = 0; while (!pq.isEmpty()) { a[i] = pq.remove(); i++; } O(N) memory is used to store the auxiliary data structure.

4 Building a heap in-place Rather than copying a into a separate pq, we can just treat a as our pq's internal heap array.  Requires us to arrange a into max-heap vertical order.  A clever way to build a heap from an unordered array is to run the equivalent of a "bubble down" on each non-leaf from right to left. index value root 4 69 index value index value

5 Heap sort code v2 // Sorts the contents of a using heap sort algorithm. public static void heapSort(int[] a) { // turn a into a max-heap for (int i = a.length / 2; i >= 0; i--) { bubbleDown(a, i, a.length - 1); } for (int i = a.length - 1; i > 0; i--) { swap(a, 0, i); // remove-max, move to end bubbleDown(a, 0, i - 1); } variation: Turn a itself into a max-heap, rather than copying it into an external priority queue.  On remove-max, move the max to the end of the array.  No extra memory used! More efficient.

6 Heap sort code v2, cont'd. // Swaps a[hole] down with its larger child until in place. private static void bubbleDown(int[] a, int hole, int max) { int temp = a[hole]; while (hole * 2 <= max) { // pick larger child int child = hole * 2; if (child != max && a[child + 1] > a[child]) { child++; } if (a[child] > temp) { a[hole] = a[child]; } else { break; } hole = child; } a[hole] = temp; }

7 Bucket sort bucket sort: A sort on integers in a known range where elements are "sorted" by tallying counts of occurrences of each unique value.  For an input array a of integers from [0.. M), create an array of counters C of size M.  For each element in a, if its value is k, increment C[k].  Use the counters in C to place a's contents back in sorted order.  This sort can be even faster than quick sort! It takes advantage of additional information about the data, namely, that it is a collection of integers in a specific range. With some kinds of data, can be even more clever:  if all values in a are unique, C can be boolean[] instead of int[]  can use a BitSet to save memory (C will be smaller)

8 Bucket sort example input array a: create array of tallies: use tallies to generate sorted contents of a index value index value index value

9 Bucket sort code // Sorts the contents of a using bucket sort algorithm. // Precondition: all elements in a are in range public static void bucketSort(int[] a) { int[] counters = new int[ ]; for (int k : a) { counters[k]++; // tally the counters } // put the counter information back into a int i = 0; for (int j = 0; j < counters.length; j++) { for (int k = 0; k < counters[j]; k++) { a[i] = j; i++; }

10 Bucket sort code v2 // Sorts the contents of a using bucket sort algorithm. // Works for any range of integers in a. public static void bucketSort(int[] a) { int min = Integer.MAX_VALUE; // find range of values int max = Integer.MIN_VALUE; // stored in a for (int k : a) { max = Math.max(max, k); min = Math.min(min, k); } int[] counters = new int[max - min + 1]; for (int k : a) { counters[k - min]++; } int i = 0; for (int j = 0; j < counters.length; j++) { for (int k = 0; k < counters[j]; k++) { a[i] = j + min; i++; }

11 Bucket sort runtime bucketSort(a, length=N): C = new int[M]// O(M) for (k : a) {C[k]++}// O(N) for (j : C) {// T(N/2) for (C[j]) times: a[i] = j.// O(M)  O(M + N)  ~ O(N) time when N >>> M  linear runtime on sorting! w00t

12 Radix sort radix sort: Makes several passes of bucket sort, one for each "digit" or significant part of each element. The algorithm:  Create an array of queues C of size 10.  For each digit #i from least to most significant: For each element in a, if its digit #i has value is k, add it to queue C[k]. Use the queues in C to place a's contents back in sorted order.  O(k N) for N elements with k digits each  lower memory usage than bucket sort

13 Radix sort example input array a: sort by last digit, then by tens digit, then by hundreds digit: index value index value index value index value

14 Radix sort, detailed input array a: create array of queues, ordered by last digit: put elements back into a, sorted by last digit.... index value index value240722, , , , 646 8, index value

15 Radix sort code // Arranges the elements in the array into ascending order // using the "radix sort" algorithm, which sorts into ten // queues by ones digit, then tens,... until public static void radixSort(int[] a) { // initialize array of 10 queues for digit value 0-9 Queue [] buckets = (Queue []) new Queue[10]; for (int i = 0; i < buckets.length; i++) { buckets[i] = new ArrayDeque (); } // copy to/from buckets repeatedly until sorted int digit = 1; while (toBuckets(a, digit, buckets)) { fromBuckets(a, buckets); digit++; }

16 Radix sort code, continued // Organizes the integers in the array into the given array // of queues based on their digit at the given place. // For example, if digit == 2, uses the tens digit, so array // {343, 219, 841, 295} would be put in queues #4, 1, 4, 9. // Returns true if any elements have a non-zero digit value. private static boolean toBuckets(int[] a, int digit, Queue [] buckets) { boolean nonZero = false; for (int element : a) { int which = kthDigit(element, digit); buckets[which].add(element); if (which != 0) { nonZero = true; } return nonZero; }

17 Radix sort code, continued // Moves the data in the given array of queues back into the // given array, in ascending order by bucket. private static void fromBuckets(int[] a, Queue [] buckets) { int i = 0; for (int j = 0; j < buckets.length; j++) { while (!buckets[j].isEmpty()) { a[i] = buckets[j].remove(); i++; } // Returns the k'th least significant digit from the integer. // For example, kthDigit( , 3) returns 7. private static final int kthDigit(int element, int k) { for (int i = 1; i <= k - 1; i++) { element = element / 10; } return element % 10; }

18 Stooge sort stooge sort: A silly sorting algorithm with the following algorithm: stoogeSort(a, min, max): if a[min] and a[max] are out of order: swap them. stooge sort the first 2/3 of a. stooge sort the last 2/3 of a. stooge sort the first 2/3 of a, again.  Surprisingly, it works!  It is very inefficient. O(N 2.71 ) on average, slower than bubble sort.  Named for the Three Stooges, where Moe would repeatedly slap the other two stooges, much like stooge sort repeatedly sorts 2/3 of the array multiple times.

19 Stooge sort code public static void stoogeSort(int[] a) { stoogeSort(a, 0, a.length - 1); } private static void stoogeSort(int[] a, int min, int max) { if (min < max) { if (a[min] > a[max]) { swap(a, min, max); } int oneThird = (max - min + 1) / 3; if (oneThird >= 1) { stoogeSort(a, min, max - oneThird); stoogeSort(a, min + oneThird, max); stoogeSort(a, min, max - oneThird); }