COMP Data Structures

Similar documents
CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Sorting and Selection

4. Sorting and Order-Statistics

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

COMP Analysis of Algorithms & Data Structures

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

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

Faster Sorting Methods

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

CSC 273 Data Structures

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

CS 303 Design and Analysis of Algorithms

Lecture 19 Sorting Goodrich, Tamassia

Comparison Sorts. Chapter 9.4, 12.1, 12.2

SORTING. Comparison of Quadratic Sorts

Design and Analysis of Algorithms

O(n): printing a list of n items to the screen, looking at each item once.

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

II (Sorting and) Order Statistics

Data Structures and Algorithms Week 4

CS61BL. Lecture 5: Graphs Sorting

Lecture 5: Sorting Part A

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Data Structures and Algorithms

COMP Analysis of Algorithms & Data Structures

Lecture 8: Mergesort / Quicksort Steven Skiena

CS 310 Advanced Data Structures and Algorithms

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Unit-2 Divide and conquer 2016

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Data Structures and Algorithms Chapter 4

Sorting Algorithms. + Analysis of the Sorting Algorithms

Lecture 9: Sorting Algorithms

Sorting (I) Hwansoo Han

Divide and Conquer Algorithms: Advanced Sorting

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

SORTING AND SELECTION

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

CS 5321: Advanced Algorithms Sorting. Acknowledgement. Ali Ebnenasir Department of Computer Science Michigan Technological University

We can use a max-heap to sort data.

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

DATA STRUCTURES AND ALGORITHMS

Sorting Algorithms. For special input, O(n) sorting is possible. Between O(n 2 ) and O(nlogn) E.g., input integer between O(n) and O(n)

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

Cpt S 122 Data Structures. Sorting

CSci 231 Final Review

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Merge Sort

COMP 352 FALL Tutorial 10

Priority Queues (Heaps)

Data Structures and Algorithms. Roberto Sebastiani

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

having any value between and. For array element, the plot will have a dot at the intersection of and, subject to scaling constraints.

Sorting Pearson Education, Inc. All rights reserved.

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Sorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

ECE 242 Data Structures and Algorithms. Advanced Sorting II. Lecture 17. Prof.

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Overview of Sorting Algorithms

Programming II (CS300)

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

Programming II (CS300)

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting

Sorting is a problem for which we can prove a non-trivial lower bound.

CSE 373: Data Structures and Algorithms

COSC 311: ALGORITHMS HW1: SORTING

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

SORTING, SETS, AND SELECTION

The complexity of Sorting and sorting in linear-time. Median and Order Statistics. Chapter 8 and Chapter 9

EECS 2011M: Fundamentals of Data Structures

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

CS61B Lectures # Purposes of Sorting. Some Definitions. Classifications. Sorting supports searching Binary search standard example

Module 3: Sorting and Randomized Algorithms

Divide and Conquer Sorting Algorithms and Noncomparison-based

Searching in General

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Transcription:

COMP 2140 - Data Structures Shahin Kamali Topic 5 - Sorting University of Manitoba Based on notes by S. Durocher. COMP 2140 - Data Structures 1 / 55

Overview Review: Insertion Sort Merge Sort Quicksort Heapsort Counting Sort For further reading, refer to Open Data Structures Book (Chapter 11) COMP 2140 - Data Structures 2 / 55

Sorting Input: Output: a sequence of n objects: A[0],..., A[n 1] (typically an array or a linked list) a comparison predicate,, that defines a total order on A an ordered representation of the objects in A Many sorting algorithms exist: bubble sort, insertion sort, merge sort, heapsort, radix sort, bucket sort, quicksort, etc. COMP 2140 - Data Structures 3 / 55

Insertion Sort Go through the items in the array (list) one by one For each item x at index i: We know the sub-array A[0]... A[i 1] is sorted Insert x in its correct position in the sub-array A[i]... A[i]. COMP 2140 - Data Structures 4 / 55

Insertion Sort /* Function to sort an array using insertion sort */ void insertionsort ( int arr [], int n) { int i, key, j; for ( i = 1; i < n; i ++) { key = arr [i]; j = i -1; } } /* Move elements of arr [0.. i -1], that are greater than key, to one position ahead of their current position */ while ( j >= 0 && arr [ j] > key ) { arr [j +1] = arr [j]; j = j -1; } arr [j +1] = key ; COMP 2140 - Data Structures 5 / 55

Insertion Sort Summary One Iteration of the Insertion Sort Algorithm: After the ith iteration, A[0..i] is sorted. Insert item A[i + 1] in its proper place in A[0..i]. In the worst case, i items are moved in the i + 1th iteration! COMP 2140 - Data Structures 6 / 55

Insertion Sort Analysis In the worst case the array is sorted backwards. The total number of moved items:. 1 + 2 +... + n 1 = n(n 1)/2 (n 2 ) COMP 2140 - Data Structures 7 / 55

Insertion Sort Time Complexity The worst-case running time of insertion sort is O(n 2 ). As it turns out, the average-case running time is also O(n 2 ). Faster sorting algorithms exist. These include: The lower bound on the worst-case time complexity of any comparison-based sorting algorithm is also n log n. COMP 2140 - Data Structures 8 / 55

Merge Sort Merge sort is an example of a divide-and-conquer algorithm. Divide the input into two or more disjoint subsets. Recursively solve each sub-problem. Combine solutions to the sub-problems to give the solution to the original problem. COMP 2140 - Data Structures 9 / 55

Merge Sort Algorithm Input: an array A[0..n 1] of comparable elements. Divide A into two subarrays A[0.. n/2 ] and A[ n/2 + 1, n 1] Recursively sort each sub-array Combine the two subarray via merging them The base of recursion is an array of size 1 which is sorted In practice, when the length of sub-array is less than 100, selection sort is applied. COMP 2140 - Data Structures 10 / 55

Merge Sort Scheme COMP 2140 - Data Structures 11 / 55

Merge Sort Example COMP 2140 - Data Structures 12 / 55

Merging Sorted Sub-arrays Given two sorted arrays A and B of size n and m, merge them into array C of size m + n. i, j, and k are three indices moving on A, B, and C. They are initially 0. At each step, copy the smaller of A[i] and B[j] to C[k]. Increment k If A[i] is copied, increment i; otherwise increment j. If one array ends (i = n or j = m), copy the remaining items of the other array to C. COMP 2140 - Data Structures 13 / 55

Merge Example COMP 2140 - Data Structures 14 / 55

Java Code for Merging // Merge A [ 0.. n ] and B [ 0..m] i n t o C [ 0.. n+m 1] v o i d mergearrays ( i n t a r r 1 [ ], i n t a r r 2 [ ], i n t n, i n t m, i n t a r r 3 [ ] ) { i n t i = 0, j = 0, k = 0 ; w h i l e ( i < n \&\& j < m) { i f (A [ i ] < B [ j ] ) { C [ k ] = A [ i ] ; i ++; k++; } e l s e { C [ k ] = B [ j ] ; j ++; k++ } } w h i l e ( i < n ) { C [ k ] = A [ i ] ; i ++; k++; } w h i l e ( j < m) { C [ k ] = B [ j ] ; j ++; k++; } } COMP 2140 - Data Structures 15 / 55

Merge Sort Summary Recursively sort the left half of the input array A Recursively sort the left half of the input array B Merge the two sub-arrays into a new one note that merging requires a new array, that is, it cannot be done in place. COMP 2140 - Data Structures 16 / 55

Time complexity of merge sort What is the time complexity of merge-sort? { 1, n 1 T (n) = 2T (n/2) + O(n) n 2 We solve this using replacement method to get T (n) O(n log n). COMP 2140 - Data Structures 17 / 55

Analysis of Merge Sort T (n) = { 1, n 1 2T (n/2) + O(n) n 2 Since merging two subarrays of size n/2 takes O(n), the merge time is at most cn for some constant value of c (think of c as M in the definition of big-oh). So we have: T (n) 2T (n/2) + c n 2(2T (n/4) + c n/2) + c n = 4T (n/4) + 2cn 4(2T (n/8) + c n/4) + 2c n = 8T (n/8) + 3cn... 2 k T ( n 2 k ) + kcn... 2 log n n T ( ) + log ncn = nt (1) + cn log n O(n log n). 2log n COMP 2140 - Data Structures 18 / 55

Should we use merge sort? Unlike insertion sort, merge sort does not work in place. Merging two arrays requires temporary storage. COMP 2140 - Data Structures 19 / 55

Quick Sort Like insertion sort, quicksort works in place. Like merge sort, quicksort employs a divide and conquer strategy. Quicksort is usually implemented as a randomized algorithm. COMP 2140 - Data Structures 20 / 55

Quick Sort Select an arbitrary element in the array as a pivot. Partition the array such that elements less than or equal to the pivot appear to its left and elements greater than or equal to the pivot appear to its right. Recursively sort each partition. In the base case we have an array of size 1. COMP 2140 - Data Structures 21 / 55

Quick Sort Any element in the array can be selected as the pivot. Typically, the pivot is selected randomly. Elements are partitioned into those less than or equal to the pivot and those greater than the pivot. In general, elements within a partition are not initially sorted. Partitioning can be performed in place. COMP 2140 - Data Structures 22 / 55

Partition Algorithm First, swap pivot with the first element. Store elements the pivot at the front of the array and elements the pivot at the back of the array. 1. Scan the array starting from the front until an element is found that is > the pivot. 2. Scan the array starting from the back until an element is found that is < the pivot. 3. Swap these two items. 4. Continue until the entire array has been partitioned. COMP 2140 - Data Structures 23 / 55

Java Code for Partition // Quicksort Partition Java Code public static int partition ( int [] A, int lo, int hi, int pivot ) { int finalpivot ; int left = lo + 1; int right = hi; swap (A, lo, pivot ); while ( left < right ) { while ( left < right && A[ lo] >= A[ left ]) left ++; while ( left < right && A[ lo] <= A[ right ]) right - -; swap (A, left, right ); } if (A[ right ] <= A[lo ]) finalpivot = right ; else finalpivot = right - 1; } swap (A, lo, finalpivot ); return finalpivot ; COMP 2140 - Data Structures 24 / 55

Quicksort Example Sort the following array using quicksort. Step 1. Select a pivot element and swap it with the first element. Note that you could select any element as the pivot. COMP 2140 - Data Structures 25 / 55

Quicksort Example We now partition the array such that elements to the left of the pivot are than the pivot and element to the right of the pivot are the pivot. Initialize left to the leftmost element after the pivot and right to the rightmost element. step 2: Increment left until we find an element that is greater than the pivot. COMP 2140 - Data Structures 26 / 55

Quicksort Example Step 3: Decrement right until we find an element that is less than the pivot. Step 4: Swap the elements at positions left and right. COMP 2140 - Data Structures 27 / 55

Quicksort Example Step 5: Repeat until left right. Step 6: If the element at position right is less than the pivot, then swap it with the pivot. Otherwise, swap the element at position right 1 with the pivot. COMP 2140 - Data Structures 28 / 55

Quicksort Example The pivot element is now in the correct position in the array. Elements to the left of the pivot are than it and elements to the right of the pivot are than it. Quicksort is called recursively on the left and right partitions. Once the left subarray is recursively sorted and the right subarray is recursively sorted, the entire array is sorted. The base case is reached when the array has size 1. COMP 2140 - Data Structures 29 / 55

Java Code for Partition (Review) // Quicksort Partition Java Code public static int partition ( int [] A, int lo, int hi, int pivot ) { int finalpivot ; int left = lo + 1; int right = hi; swap (A, lo, pivot ); while ( left < right ) { while ( left < right && A[ lo] >= A[ left ]) left ++; while ( left < right && A[ lo] <= A[ right ]) right - -; swap (A, left, right ); } if (A[ right ] <= A[lo ]) finalpivot = right ; else finalpivot = right - 1; } swap (A, lo, finalpivot ); return finalpivot ; COMP 2140 - Data Structures 30 / 55

Java Code for Quick-Sort public static void quicksort ( int [] A) { quicksortaux (A, 0, A. length - 1); } private static void quicksortaux ( int [] A, int lo, int hi){ if (lo < hi) { int pivot = lo + ( int ) ( Math. random () * (hi - lo + 1)); // pivot is selected as a random index in [ lo, hi ) pivot = partition (A, lo, hi, pivot ); // now pivot is in its right position } } quicksortaux (A, lo, pivot - 1); quicksortaux (A, pivot + 1, hi); COMP 2140 - Data Structures 31 / 55

Time Complexity of Quick-Sort Recall that we can consider the time complexity in the worst-case, best-case, and average-case. We express the time complexity using big-oh notation! What are these time complexities for quick-sort? COMP 2140 - Data Structures 32 / 55

Worst-case Analysis of Quick-sort In the worst case, the pivot partitions an array of size n into one array of size n 1 and a second array of size 0. This happens when the pivot is the smallest or largest element This leads to the following running time: { 1 if n 1 T (n) = T (n 1) + O(n) if n 2 For some constant c we have: T (n) T (n 1) + cn (T (n 2) + c(n 1)) + cn (T (n 3) + c(n 2)) + c(n 1) + cn... T (n k) + c(n k + 1) + c(n k + 2) +... + c(n 1) + cn... T (1) + c(1) + c(2) +... + c(n 1) + cn = O(n 2 ) (after k = n steps, we get to T (1), which is 1.) COMP 2140 - Data Structures 33 / 55

Best-case Analysis of Quick-sort In the best case, the pivot partitions an array of size n into two array of size roughly n/2. This happens when the pivot is the median This leads to the following running time: { 1 if n 1 T (n) = 2T (n/2) + O(n) if n 2 For some constant c we have: T (n) 2T (n/2) + cn 2(2T (n/4) + cn/2) + cn = 4T (n/4) + 2cn 4(2T (n/8) + c(n/4)) + 2cn = 8T (n/8) + 3cn... 2 k T (n/2 k ) + ckn... 2 log n T (1) + c log n n = O(n log n) (after k = log n steps, we get to T (1), which is 1.) COMP 2140 - Data Structures 34 / 55

Average-case Analysis of Quick-sort As it turns out, the average case running time of quick-sort is similar to the best case. This is because making a bad random choice for a pivot is unlikely. Making a bad random choice at every level of the recursion tree is even less likely. This extreme bad case happens when the list is already sorted (decreasing or increasing). Furthermore, there exist O(n)-time algorithms for finding the median of a set of n values. Using such a median algorithm guarantees an optimal choice of pivot. COMP 2140 - Data Structures 35 / 55

Final Notes on Quick-sort The running time remains O(n log n) even if the pivot partitions the array into any fixed fraction. For example, say the choice of pivot always guarantees a split of n = 16 and 15n/16. The running time becomes: { 1 if n 1 T (n) = T (n/16) + T (15n/16) + O(n) if n 2 For some constant c we have: T (n) 2T (15n/16) + cn (after k = log 16/15 n = O(log n) steps, we get to T (1), which is 1.) COMP 2140 - Data Structures 36 / 55

Heap-Sort Heap-sort is another sorting algorithm that is often as efficient as quick-sort It is in-place and runs in O(n log n). It is consistent in the sense that it performs equally well in the best, average and worst cases. Heapsort is implemented using algorithms related to heaps: insert, extractmax, and buildheap. COMP 2140 - Data Structures 37 / 55

Heaps It is a complete binary tree, i.e., the binary tree is filled from top to bottom and from left to right. Each node stores a value which is larger than the value of its children. So, the max element is the root! COMP 2140 - Data Structures 38 / 55

Heap Operations As we will see later, a heap can support the two operations insert (add a new element to the tree) and extract-max (return and delete the maximum element) efficiently (each in O(log n) time). These operations are associated with a priority queue abstract data type. HeapSort: sorting with a Priority Queue: Insert all n elements into the priority queue in O(n log n) time. Call extractmax n times to retrieve the elements in reverse order. COMP 2140 - Data Structures 39 / 55

Insertion Sort public static void priorityqueuesort ( int [] A) { PriorityQueue pq = new PriorityQueue (); for ( int i = 0 ; i < A. length ; i ++) pq. insert (A[i]); for ( int i = A. length -1 ; i >= 0 ; i - -) A[ i] = pq. extractmax (); } Note, this implementation does not sort in place: A new heap is generated and items are copied in it. COMP 2140 - Data Structures 40 / 55

Heap Sort In Place Step I: Rearrange the input array to form a heap (in place). Simply run buildheap on the array. Step II: call extractmax n times, storing each result in the next available empty cell in the array. Elements in the heap are stored at the front of the array. Those already sorted are stored at the back of the array. COMP 2140 - Data Structures 41 / 55

Heap Sort Time Complexity We will see later how we can implement a heap so that: Building a heap of n items take O(n) time Extract max takes O(log n) time Step I: re-arranging the array to form a heap takes O(n) time. Step II: calling extract-max n times takes O(n log n) time. Theorem Heap-sort can be implemented to run in O(n log n) time in the worst case. COMP 2140 - Data Structures 42 / 55

Heap Sort Summary Heap-sort can be implemented in-place, i.e., it does not need to create a new data structure (just re-arrange the input array). It is more consistent than the naive implementation of quick-sort, i.e., its worst-case and best-case performance is the same. It is slightly slower than the quick-sort with random pivot; but it is considered the most direct competitor of quicksort! It is not a divide-and-conquer algorithm does not need stack memory! COMP 2140 - Data Structures 43 / 55

Lower Bound for Comparison-Based Sorting In comparison-based sorting, we have a set of objects (e.g., a bag of potato) and an operator that can tell us whether an object is smaller than the other (e.g., a scale for comparing weights of potatoes) No other assumption is made for the objects (e.g., they are not necessarily numbers). Algorithms like Bubble-sort, Quick-sort, Merge-sort, and Heap-sort are all comparison-based. Does a comparison-base sorting with time asymptotically less than O(n log n) exist? We show the answer is No! COMP 2140 - Data Structures 44 / 55

Lower Bound for Comparison-Based Sorting Consider a set of n distinct objects a 1,..., a n. Any permutation of thee objects forms an ordering (a possible input array). How many ways they can be ordered? n! ways, that is, there are n! possible inputs! Sorting corresponds to identifying the permutation of a sequence of elements. Once the permutation is known, the position of each item can be restored. COMP 2140 - Data Structures 45 / 55

Decision Tree for Sorting an Array Suppose an array of A of three elements [a, b, c] is to be sorted. Any algorithm can be described with a decision tree for determining the correct sorted order (i.e., the array s permutation). The number of comparisons made by the algorithm equals to the height of the tree! The number of leaves is n! Here is the decision tree for one algorithm: COMP 2140 - Data Structures 46 / 55

Lower Bound using Decision Tree The minimum height of a binary tree with n nodes is at least log n. Intuitively, whenever two elements are compared (is x < y?) this eliminates a number of possible permutations. In the worst case, at most half of the remaining possible permutations are eliminated. If there are n! permutations and half are eliminated on each step, it takes at least log 2 (n!) comparisons to identify the correct permutation of the items. So, any algorithm has to make at least log n! comparisons. COMP 2140 - Data Structures 47 / 55

Lower Bound for Comparison-Based Sorting Theorem Any comparison-based sorting has time complexity of Ω(n log n) We use Ω(n log n) to denote the time complexity that is asymptotically at least n log n COMP 2140 - Data Structures 48 / 55

Non-comparison based algorithms The lower bound of Ω(nlogn) applies to algorithms that determine a sorted ordering by comparing pairs of elements. If the set of elements to be sorted has specific characteristics, then faster sorting algorithms may be possible. Such algorithms must use techniques other than comparison alone to determine the sorted order. Examples: counting sort, radix sort, bucket sort COMP 2140 - Data Structures 49 / 55

Counting Sort Let A be an array of n integers in the range {0, 1,..., k}. For each value i in {0, 1,..., k}, count the number of occurrences of i in {0, 1,..., k} and store it in C[i]. Overwrite A[0..n 1] with the number of occurrences of each value {0, 1,..., k} in sorted order. COMP 2140 - Data Structures 50 / 55

Counting Sort Example Consider the following array A of size n = 10 with items in the range {1..k} where k = 7. COMP 2140 - Data Structures 51 / 55

Insertion Sort // sorting integers in the range [0.. k] and stored in A public static void countingsort ( int [] A, int k) { int [] C = new int [k +1]; for ( int i = 0 ; i <= k ; i ++) C[ i] = 0; for ( int j = 0 ; j < A. length ; j ++) C[A[j ]]++; } int index = 0; for ( int i = 0 ; i <= k ; i ++) for ( int p = 0 ; p < C[ i] ; p ++) A[ index ++] = i; Unlike comparison based sortings, there are no comparisons between elements of A (all less-than operators test for-loop exit conditions). COMP 2140 - Data Structures 52 / 55

Counting Sort Running Time Counting Sort runs in O(n + k) times. Initializing elements of C to 0 takes O(k) times Looping over A and setting values of C takes O(n) times Looping over C and adding sorted elements back to array A takes O(n + k) times. Are these worst-case, average-case, or best-case times? No! the code contains no branches (if statements). If k O(nlogn), then counting sort is at least as efficient as a comparison-based sorting algorithm in the worst case. If k k / O(nlogn), then counting sort is slower than an efficient comparison-based sorting algorithm in the worst case. COMP 2140 - Data Structures 53 / 55

Augmented Counting Sort Suppose the value of k is initially unknown. We can compute k: int k = 0; for ( int j = 0 ; j < A. length ; j ++) if ( A[ j] > k) k = A[ j]; It takes O(n) time If k is too large, call an efficient comparison-based sorting algorithm (e.g., quick-sort), otherwise continue with counting sort. What if array A contains both positive and negative values? Add a fixed value to all elements to make them non-negative; apply counting sort, and after sorting deduce the added element. COMP 2140 - Data Structures 54 / 55

Sorting Summary We have examined a few sorting algorithms. COMP 2140 - Data Structures 55 / 55