Chapter 7 Sorting. Terminology. Selection Sort

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

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

Cpt S 122 Data Structures. Sorting

Quick Sort. CSE Data Structures May 15, 2002

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

Sorting Algorithms. + Analysis of the Sorting Algorithms

Sorting. Task Description. Selection Sort. Should we worry about speed?

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

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

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

Sorting. Order in the court! sorting 1

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

UNIT 7. SEARCH, SORT AND MERGE

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

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

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

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

Divide and Conquer Sorting Algorithms and Noncomparison-based

Sorting. Order in the court! sorting 1

DATA STRUCTURES AND ALGORITHMS

Comparison Sorts. Chapter 9.4, 12.1, 12.2

BM267 - Introduction to Data Structures

Sorting and Selection

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

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

Lecture 8: Mergesort / Quicksort Steven Skiena

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

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

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Sorting (I) Hwansoo Han

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

// walk through array stepping by step amount, moving elements down for (i = unsorted; i >= step && item < a[i-step]; i-=step) { a[i] = a[i-step];

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

7 Sorting Algorithms. 7.1 O n 2 sorting algorithms. 7.2 Shell sort. Reading: MAW 7.1 and 7.2. Insertion sort: Worst-case time: O n 2.

CS221: Algorithms and Data Structures. Sorting Takes Priority. Steve Wolfman (minor tweaks by Alan Hu)

Mergesort again. 1. Split the list into two equal parts

Lecture 19 Sorting Goodrich, Tamassia

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

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

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

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

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

CSE 373: Data Structures and Algorithms

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

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

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

COMP Data Structures

Introduction. Sorting. Table of Contents

Introduction. Sorting. Definitions and Terminology: Program efficiency. Sorting Algorithm Analysis. 13. Sorting. 13. Sorting.

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

Sorting. Dr. Baldassano Yu s Elite Education

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

UE Algorithmen und Datenstrukturen 1 UE Praktische Informatik 1. Übung 9. Sorting

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

SORTING, SETS, AND SELECTION

Giri Narasimhan. COT 5993: Introduction to Algorithms. ECS 389; Phone: x3748

About this exam review

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

Data Structures and Algorithms

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

Sorting. Data structures and Algorithms

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

SORTING AND SELECTION

Practical Session #11 - Sort properties, QuickSort algorithm, Selection

CSE 373: Data Structures and Algorithms

Structures, Algorithm Analysis: CHAPTER 7: SORTING

InserDonSort. InserDonSort. SelecDonSort. MergeSort. Divide & Conquer? 9/27/12

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

CS102 Sorting - Part 2

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

SORTING. Comparison of Quadratic Sorts

07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31

Sorting. Bringing Order to the World

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

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

COS 226 Midterm Exam, Spring 2009

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

Lecture Notes on Quicksort

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

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

Sorting algorithms Properties of sorting algorithm 1) Adaptive: speeds up to O(n) when data is nearly sorted 2) Stable: does not change the relative

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Lecture 5: Sorting Part A

CS61BL. Lecture 5: Graphs Sorting

Sorting and Searching Algorithms

COSC242 Lecture 7 Mergesort and Quicksort

Lecture Notes on Quicksort

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Programming II (CS300)

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

Merge Sort Goodrich, Tamassia Merge Sort 1

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

Outline. Quadratic-Time Sorting. Linearithmic-Time Sorting. Conclusion. Bubble/Shaker Sort Insertion Sort Odd-Even Sort

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

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

Better sorting algorithms (Weiss chapter )

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

Transcription:

Chapter 7 Sorting Terminology Internal done totally in main memory. External uses auxiliary storage (disk). Stable retains original order if keys are the same. Oblivious performs the same amount of work regardless of the actual input. Sort by address uses indirect addressing so the record (structure) doesn t have to be moved. Inversion a pair of elements that is out of order. Important in determining a lower n( n 1) bound. The number of pairs of elements is (n ways to pick first, n-1 ways to pick the second, divide by two as order isn t important). On average, only half of these n( n 1) pairs are out of order, so number of inversions is. n swaps of adjacent elements 4 are required. Selection Sort Select elements one at a time and place in proper final position. Repeatedly find the smallest. 3 6 43 1 9 1 6 43 3 9 1 3 43 6 9 1 3 6 43 9 1 3 6 9 43 Code is shown in Figure 1. Analysis: n + n 1+ n + + 1 = n( n 1) / Only n moves use when records (structure) are long. Requires n / compares even when already sorted Oblivious. Unstable because it may swap the element in the last location past other instances of itself. void SelectionSort(T a[], int n) {// Sort the n elements a[0:n-1]. for (int size = n; size > 1; size--) { int j = Max(a, size); // loop Swap(a[j], a[size - 1]); } // for } // SelectionSort Figure 1 Selection Sort void bubble(data[] a, int size) { for (int i = 0; i < size - 1; i++) { bool hasswapped = false; for (int j = 0; j< size-1)-i; j++) if (a[j].value > a[j+1].value) { hasswapped = true; swap(a[j],a[j+1]); } if (!hasswapped)return; } } Figure Bubble Sort Sorting Page 1

Bubble Sort Compares adjacent elements exchanges if out of order. Lists get smaller each time at least one is placed in final order. Place of last swap is as much as you have to look at. Quit if you do no swaps Code is shown in Figure. Stable Oblivious if the flag is not included Analysis: n + n 1+ n + + 1 = n( n 1) / Insertion Sort Sorts by inserting records into an already existing sorted file. Two groups of keys - sorted and unsorted. Code is shown in Figure 3. 1 Insert n times - each time move elements to insert. The number of elements in the list changes so 1 1 1 ) 1 ( 1+ + 3 + + n ) = n( n + 1 = n 11 5 17 1 1 5 11 17 1 1 5 11 17 1 1 1 5 11 17 1 1 5 11 17 1 4 void InsertionSort(T a[], int n) {// Sort a[0:n-1]. for (int i = 1; i < n; i++) { T t = a[i]; for (int j = i; j > 0 && t < a[j-1]; j--) a[j] = a[j - 1]; a[j] = t; } // for } // InsertionSort Figure 3 Insertion Sort Performs better when the degree of unsortedness is low recommended for data that is nearly sorted. Non-Oblivious as if new element to be added is greater than last element of sorted part, we do not have to look at anything else. Stable as never swap with other than an adjacent element Improvements 1. Use binary search Ο ( nlogn) compares, but number of moves doesn t change so no real gain.. Linked list storage can t use binary search still Ο ( n ). Could use sentinel containing the key search until p->info.key >= q->info.key Sentinel is extra item added to one end of the list so ensure that the loop terminates without having to include a separate check. Shell Sort A subquadratic algorithm whose code is only slightly longer than insertion sort, making it the simplest of the faster algorithms. Sorting Page

Avoid large amounts of data movement by: o Comparing elements that are far apart. o Comparing elements that are less far apart. o Gradually shrinks toward insertion sort. Consider Figure 4, which shows an example of shell sort with the increment sequence of {5, 3, 1}. Original 81 94 11 96 1 35 17 95 8 58 41 75 15 After 5-sort 35 17 11 8 1 41 75 15 96 58 81 94 95 After 3-sort 8 1 11 35 15 41 58 17 94 75 81 96 95 After 1-sort 11 1 15 17 8 35 41 58 75 81 94 95 96 Shell sort is known as a diminishing gap sort. The code for Shell sort is shown in Figure 5. Worst case running time is Ο ( n ). Figure 4 Shell Sort When the gap goes to 1, the sort is essentially an insertion sort Can get bad running times if all big numbers are at even positions and all small numbers are at odd AND the gaps don t rearrange them as they are always even. 4 The average running time appears to be between ( n 5/ ) 6 Ο Ο ( n 7 / ) if we use a gap of.. Reasonable sort for large inputs. Complicated analysis. Unstable as when your gap is five, you swap elements five apart ignoring duplicates that lie between. Non-oblivious as builds off insertion sort, which is non-oblivious Heap Sort The process goes as follows: o Build a min heap o While the heap isn t empty, deletemin. Complexity Ο( nn log nn ) Unstable Non-oblivious void ShellSort (T a[], int length ) { for ( int gap = length / ; gap > 0; gap = gap ==? 1 : static_cast<int>( gap /. ) ) for ( int i = gap; i < length; i++ ) { T tmp = a[i]; for ( ; j >= gap && tmp < a[j-gap]; j -= gap ) a[j] = a[j-gap]; a[j] = tmp; } // for } // ShellSort Figure 5 Shell Sort Sorting Page 3

Divide and Conquer Divide and Conquer is a method of algorithm design that has created such efficient algorithms as Merge Sort. In terms or algorithms, this method has three distinct steps: o Divide: If the input size is too large to deal with in a straightforward manner, divide the data into two or more disjoint subsets. o Recur: Use divide and conquer to solve the subproblems associated with the data subsets. o Conquer: Take the solutions to the subproblems and merge these solutions into a solution for the original problem. The next two sorts are divide and conquer algorithms: MergeSort and QuickSort. Merge Sort Chop the lists into two sublists. Sort the two pieces. Combine by merging. (Some techniques just get sublists of larger and larger powers of two.) The pieces may also be sorted via MergeSort, so it is recursive. What would the code look like for MergeSort? The code for Merge is shown in Figure 6. In merging sublists of length n, clearly no more than n compares are required. Actually it is less than this, but it is easy to count this way. Each level takes exactly n compares and there are log n levels, the complexity is Ο ( n log n). A closer count is n log n 1.5n + 1 (by void Merge(T c[], T d[], int l, int m, int r) {// Merge c[l:m]] and c[m:r] to d[l:r]. int i = 1, // cursor for first segment j = m+1, // cursor for second k = 1; // cursor for result while ((i <= m) && (j <= r)) if (c[i] <= c[j]) d[k++] = c[i++]; else d[k++] = c[j++]; if (i > m) for (int q = j; q <= r; q++) d[k++] = c[q]; else for (int q = i; q <= m; q++) d[k++] = c[q]; } // Merge Figure 6 Merge running test cases) If linked lists, no problem with storage space. If we have an array of items to be stored, the MergeSort requires an auxiliary storage array. Hardly ever used for main memory sorts because of the extra memory. Works well for external sorts (not all data is in memory at the same time) as doesn t need to see entire data (like a quicksort does). Stable, as control order when merging Oblivious as it does the same amount of work regardless of initial order. Sorting Page 4

Quick Sort In practice, is the fastest sorting algorithm known. Better than merge sort as partitioning is faster than merging. Partition the set into two sets: those elements less than j and those elements greater than j. j is called the pivot. Often done as: Use two pointers top pointer looks for a value smaller than j, bottom pointer looks for a value larger than j. Then interchange. This does only a third as many swaps. Apply recursively. The code for QuickSort is shown in Figure 7. Analysis: At each level, all elements of the array are examined. The number of levels depends on how equally the pieces are divided. Best case: log n levels yielding Ο( n log n). Worst case: Divide unequally. let n be the size of the array to be sorted: C ( n) = n 1+ C( n 1) = n( n 1) / Space requirement: depends on recursive stacking. Unstable as you swap elements far apart during partition (without considering equal element between) Not only can quicksort NOT take advantage of partial ordering, it actually gets worse when the file is in order. This is because it doesn t divide the array in half, so the depth of recursion is n instead of log n. A void QuickSort( T a[], int l, int r ) { if ( l >= r ) return; int i = l, // left-to-right cursor j = r + 1; // right-to-left cursor T pivot = a[l]; while ( true ) { do {// find >= element on left side i = i + 1; } while (a[i] < pivot); do {// find <= element on right side j = j - 1; } while ( a[j] > pivot ); if ( i >= j ) break; // swap pair not found Swap( a[i], a[j] ); } // while a[l] = a[j]; a[j] = pivot; QuickSort( a, l, j-1 ); // sort left segment QuickSort( a, j+1, r ); // sort right segment } // QuickSort Figure 7 Quick Sort bad thing for complexity, right? This is worse than being oblivious. We could call it dangerously-oblivious it not only fails to take advantage of existing order, it gets worse!!! Improvements 1. Use median of three so don t get a bad pivot. (if sort in place, get sentinels). Use sentinel at each end so don t have to check (avoid left and right crossing) 3. Switch to insertion sort when size gets small - can do one big insertion sort on all, but it is recommended to sort the pieces separately because of caching. 4. Swap elements equal to pivot as partitioning tends to divide chunks of equal nodes in half. Sorting Page 5

Final Thought In Figure 8, notice that QuickSort and MergeSort have a similar program structure. Both have Ο (n) work to either 1. Divide into chunks or. Put the chunks back together. The pictures we draw (for expected case) look the same. The formula analysis looks the same. We see it doesn t matter whether we do the work before the recursion or after. The work is the same. QuickSort(a[],low,high) { pivot = partition(a,low,high) QuickSort(a,low,pivot-1) QuickSort(a,pivot+1,high) } // QuickSort MergeSort(a[],low,high) { mid=(low+high)/ MergeSort(a,low,mid) MergeSort(a,mid+1,high) Merge(a,low,mid,high) } // MergeSort Quickselect Figure 8 QuickSort and MergeSort Find the k th smallest element in an array of N items. We can do better than sorting the array first. The solution is to have an algorithm that is similar to QuickSort but with only one recursive call. The steps of the algorithm are as follows: 1. If the number of elements in S is 1, return the single element in S.. Pick any element v in S. It is the pivot. 3. Partition S {v} into L and R, exactly as was done with quicksort. 4. If k number of elements in L, the item we are searching for must be in L. Call Quickselect( L, k ) recursively. Otherwise, if k is exactly equal to 1 more than the number of items in L, the pivot is the k th smallest element, and we can return it as the answer. Otherwise, the k th th smallest element lies in R, and it is the ( k L 1) smallest element in R. Again, we can make a recursive call and return the result. Notice that only one recursive call is made. If the pivot is chosen correctly, it can be shown, that even in the worst case, the running time is linear. Indirect Sorting If we are sorting an array whose elements are large, copying the items can be very expensive. We can get around this by doing indirect sorting. o We have an additional array of pointers where each element of the pointer array points to an element in the original array. o When sorting, we compare keys in the original array but we swap the element in the pointer array. Sorting Page 6

This saves a lot of copying at the expense of indirect references to the elements of the original array. We must still rearrange the final array. The text has a clever way of doing that. It involves pointer arithmetic and shuffles cycles of shifts. Basically, o Find a ptr[i] which is not pointing to the i th element of a. o Save the thing in the i th element of a. o Move the correct a element into the i th location freeing up the nextj location o Now move the thing that wants to be in the nextj location to it. We know what that element is because it is p[nextj]. o Repeat until we reach something that is in its final position. This shuffling could rearrange every element, but likely a smaller cycle of elements is shifted. Sorting Page 7