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

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

Introduction. Sorting. Table of Contents

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

Sorting (I) Hwansoo Han

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

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

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Cpt S 122 Data Structures. Sorting

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

COMP Data Structures

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

Chapter 7 Sorting. Terminology. Selection Sort

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.

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

CS240 Fall Mike Lam, Professor. Quick Sort

BM267 - Introduction to Data Structures

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

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

Overview of Sorting Algorithms

Question 7.11 Show how heapsort processes the input:

L14 Quicksort and Performance Optimization

Divide and Conquer Sorting Algorithms and Noncomparison-based

Sorting Pearson Education, Inc. All rights reserved.

SORTING. Comparison of Quadratic Sorts

C/C++ Programming Lecture 18 Name:

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

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

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

Copyright 2012 by Pearson Education, Inc. All Rights Reserved.

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

Sorting is ordering a list of objects. Here are some sorting algorithms

CS301 - Data Structures Glossary By

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

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

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

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

CS 112 Practice Midterm Solutions

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

CSE 2320 Section 002, Fall 2015 Exam 2 Time: 80 mins

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

Faster Sorting Methods

COT 5407: Introduction. to Algorithms. Giri NARASIMHAN. 1/29/19 CAP 5510 / CGS 5166

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

Sorting. Bringing Order to the World

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

CSE 2123 Sorting. Jeremy Morris

LECTURE 17. Array Searching and Sorting

Lecture 9: Sorting Algorithms

Sorting & Searching (and a Tower)

CS61BL. Lecture 5: Graphs Sorting

Lecture 19 Sorting Goodrich, Tamassia

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

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

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

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

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

Programming II (CS300)

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

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

Randomized Algorithms: Selection

CS 310 Advanced Data Structures and Algorithms

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

QuickSort

4. Sorting and Order-Statistics

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

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

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #12

Quick Sort. CSE Data Structures May 15, 2002

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

UNIT 7. SEARCH, SORT AND MERGE

Sorting Algorithms. + Analysis of the Sorting Algorithms

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

What is sorting? Lecture 36: How can computation sort data in order for you? Why is sorting important? What is sorting? 11/30/10

Sorting and Searching Algorithms

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

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

Searching & Sorting in Java Bubble Sort

COMP171 Data Structures and Algorithms Fall 2006 Midterm Examination

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

Sorting and Selection

4.4 Algorithm Design Technique: Randomization

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

II (Sorting and) Order Statistics

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

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

Sorting. Lecture10: Sorting II. Sorting Algorithms. Performance of Sorting Algorithms

Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then

1 a = [ 5, 1, 6, 2, 4, 3 ] 4 f o r j i n r a n g e ( i + 1, l e n ( a ) 1) : 3 min = i

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018

EECS 2011M: Fundamentals of Data Structures

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:

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

Data Structures and Algorithms

CS 106 Introduction to Computer Science I

Programming II (CS300)

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

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

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Transcription:

CS 704 Introduction to Data Structures and Software Engineering Sorting Terms & Definitions Internal sorts holds all data in RAM External sorts use Files Ascending : Low to High Descending : High to Low Stable Sort Maintains the relative order of equal elements, in situ. Desirable if list is almost sorted or if items with equal values are to also be ordered on a secondary field. Comparing Sorting Algorithms Program efficiency Overall program efficiency may depend entirely upon sorting algorithm => clarity must be sacrificed for speed. Sorting Algorithm Analysis Performed upon the overriding operation in the algorithm: Comparisons Swaps void swap( int& x, int& y) { int tmp = x; x = y; y = tmp; Bubble Sort Bubble elements down (up) to their location in the sorted order. for (i = 0; i < n-; i++) for (j = n-; j > i; j--) if (A[j] < A[j-]) swap(a[j],a[j-]); Bubble Sort: Graphical Trace Bubble Sort: Analysis if-statement: compare and in worst case, swap inner for-loop: body executed for j-values from n- down to i+, or n-i- times each execution of body involves compare and up to swap outer for-loop: body executed for i-values from 0 up to n- (or to n-) each execution of body involves n-i- compares and up to n-i- swaps

Bubble Sort: Analysis So in the worst case, the number of swaps equals the number of compares, and is: n i= ( n i ) = n( n ) ( n )( n ) ( n ) Which is clearly O(n ). Selection Sort In the i th pass, select the element with the lowest value among A[i],..., A[n-], & swap it with A[i]. Results after i passes: the i lowest elements will occupy A[0],..., A[i] in sorted order. for (Begin = 0; Begin < Size - ; Begin++) { SmallSoFar = Begin; for (Check = Begin + ; Check < Size; Check++) { if (alist[check] < alist[smallsofar]) SmallSoFar = Check; swap(alist[begin], alist[smallsofar]); Selection Sort: Graphical Trace Selection Sort: Analysis if-statement: compare inner for-loop: body executed n-i- times (i is Begin and n is Size) each execution of body involves compare and no swaps outer for-loop: body executed n- times each execution of body involves n-i- compares and swap Selection Sort: Analysis So in the worst case, the number of swaps is n, and the number of compares is: n i= ( n i ) = n( n ) ( n )( n ) ( n ) which is clearly O(n) and the same as for BubbleSort. Duplex Selection Sort Min / Max Sorting algorithm passes thru the array locating the min and max elements in the array A[i],..., A[n-i+]. Swapping the min with A[i] and the max with A[n-i+]. Results after the ith pass: the elements A[],..., A[i] and A[n-i+],..., A[n] are in sorted order. What would be the Big Oh?

Duplex Selection Sort Analysis Without going through the figures, Duplex Selection Sort is another O(N^) sort algorithm. But, the coefficient IS better than for BubbleSort! Sorting Thoughts Comparison-Based Sorting Algorithms which compare element values to each other What is the minimum number of comparisons, required to sort N elements using a comparison-based sort? Is a Queue a type of sorting? Comparison Tree Comparison Tree for 3 Elements Binary tree (hierarchical graph < branches per node) which contains comparisons between elements at each non-leaf node & containing element orderings at its leaf (terminal) nodes. a < b < c b < c T a < c a < b T F T b < a < c a < c b < c T F T F F F Depth 0 a < c < b c < a < b b < c < a c < b < a 3 Comparison Tree Continued Tree for sorting N Any of the 3 elements (a, b, c) could be first in the final order. Thus there are 3 distinct ways the final sorted order could start. After choosing the first element, there are two possible selections for the next sorted element. After choosing the first two elements there is only remaining selection for the last element. Therefore selecting the first element one of 3 ways, the second element one of ways and the last element way, there are 6 possible final sorted orderings = 3 * * = 3! Any of the N elements (... N) could be first in the final order. Thus there are N distinct ways the final sorted order could start. After choosing the first element, there are N- possible selections for the next sorted element. After choosing the first two elements there N- possible selections for the next sorted element, etc. Therefore selecting the first element one of N ways, the second element one of N- ways, etc., there are N * (N-) * (N-) *... * * possible final sorted orderings which = N!

Comparisons for Sorting N Depth vs. N The comparison tree for N elements must have N! leaf nodes. Each leaf node contains one of the possible orderings of all of the N elements. Consider the previous comparison tree for 3 elements, all of the leaf nodes are at a depth of either or 3 > log 3! The comparison tree for 4 elements must contain 4!=4 leaf nodes, all of which would be at a depth of either 4 or 5 > log 4! The floor symbol means the largest whole number that is less than the number Depth: 0 3 4 5 General Comparison Trees Minimal # of Comparisons The comparison tree for N elements must contain N! leaf nodes, all of which would be at a depth > log N! The minimal number of comparisons required to sort a specific (unsorted) ordering is equal to the depth from the root to a leaf. N log N Since the depth of all leaf nodes is > log N! in a comparison tree, the minimal number of comparisons to sort a specific initial ordering of N elements is > log N! Stirling s Approximation for log (N!) can be used to determine a lower bound for log (N!) which is O(N log N ) No comparison based sorting algorithm can sort faster than O(N log N) Quick Sort Select an item in the array as the pivot key. Divide the array into two partitions: a left partition containing elements < the pivot key and a right partition containing elements > the pivot key.

Quicksort Trace Quicksort Start with i and j pointing to the first & last elements, respectively. Select the pivot (3): [3 4 5 9 6 5 8] L R Swap the end elements, then move L, R inwards. [8 4 5 9 6 5 3] L R Swap, and repeat: [ 4 5 9 8 6 5 3] L R int Partition(Item A[], int start, int end, const Item& pivot ){ int L = start, R = end; do { swap( A[L], A[R] ); while (A[L] < pivot ) L++; while (!(A[R] < pivot)) R--; //< overloaded while (R > L); return (L); Swap, and repeat: [ 4 5 9 8 6 5 3] R L Pivoting Find Pivot Partitioning test requires at least key with a value < that of the pivot, and value > to that of the pivot, to execute correctly. Therefore, pick the greater of the first two distinct values (if any). OR Try and pick a pivot such that the list is split into equal size sublists, (a speedup that should cut the number of partition steps to about /3 that of picking the first element for the pivot). Choose the middle (median) of the first 3 elements. Pick k elements at random from the list, sort them & use the median. const int MISSING = -; int FindPivot(const Item A[], int start, int end ) { Item firstkey; //value of first key found int pivot; //pivot index int k; //run right looking for other key firstkey = A[start]; //return - if different keys are not found pivot = MISSING; k = start + ; //scan for different key while ( (k <= end) && (pivot == MISSING) ) if (firstkey < A[k]) //select key pivot = k; else if (A[k] < firstkey) pivot = start; else k++; return pivot; Quick Sort Function const int MISSING = -; void QuickSort( Item A[], int start, int end ) { // sort the array from start... end Item pivotkey; int pivotindex; int k; //index of partition >= pivot pivotindex = FindPivot( A, start, end ); if (pivotindex!= MISSING) { pivotkey = A[pivotIndex]; k = Partition( A, start, end, pivotkey ); QuickSort( A, start, k- ); QuickSort( A, k, end ); Average Case quicksort is based upon the intuition that swaps, (moves), should be performed over large distances to be most effective. quicksort's average running time is faster than any currently known O(n log n) internal sorting algorithms (by a constant factor). For very small n (e.g., n < 6) a simple O(n ) algorithm is actually faster than Quicksort. Optimization: When the sublist is small, use another sorting algorithm, (selection).

Worst Case Iterative Version is Posted In the worst case, every partition might split the list of size j - i + into a list with element, and a list with j - i elements. A partition is split into sublists of size & j-i when one of the first two items in the sublist is the largest item in the sublist which is chosen by findpivot. When will this worst case partitioning always occur? O(N^) Iterative implementation requires using a stack to store the partition bounds remaining to be sorted. struct StackItem { int low, hi; ; At the end of any given partition, only one subpartition need be stacked. Other Quicksort Optimizations Special Case: Mapping All function calls should be replaced by inline code to avoid function overhead. Current partition bounds should be held in register variables. With large data records, swap pointers instead of copying records We re accepting the cost of additional pointer dereferences to avoid the cost of some data copying. Carefully investigate the average data arrangement in order to select the optimal sorting algorithm. For example, to identify special cases within Quicksort Better than N log N If sort key (member) consists of consecutive (unique) N integers they can be easily mapped onto the range 0.. N- & sorted. If the N elements are initially in array A, then: Item A[], B[]; for (int i = 0; i < N; i++ ) B[A[i].GetKey() % N] = A[i]; Mapping takes O(n) time. Requires exactly record with each key value! Of course, this is a very special circumstance Special case of Bin Sorting. (If integers are not consecutive, but within a reasonable range, bit flags can be used to denote empty array slots.) Bin Sorting Assume we need to sort an array of integers in the range 0-99: Assume we have an array of 0 linked lists (bins) for storage. First make a pass through the list of integers, and place each into the bin that matches its s digit. Then, make a second pass, taking each bin in order, and place each integer into the bin that matches its s digit, etc.

Bin Sorting Now if you just read the bins, in order, the elements will appear in ascending order. Assuming no Bin with > element Otherwise, use another sort technique to sort bins Each pass takes O(N) work, and the number of passes is just the number of digits in the largest integer in the original list. That beats QuickSort, but only in a somewhat special case. (When each bin has element) Binsort Implementation will be posted online.