Sorting: Implementation 15-211 Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
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:
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.
Chapter 7: Sorting Algorithms
1 Today’s Material Divide & Conquer (Recursive) Sorting Algorithms –QuickSort External Sorting.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
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.
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
Quicksort.
Quicksort
Data Structures Review Session 1
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Sorting Rearrange n elements into ascending order. 7, 3, 6, 2, 1  1, 2, 3, 6, 7.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Lecture 8 Sorting. Sorting (Chapter 7) We have a list of real numbers. Need to sort the real numbers in increasing order (smallest first). Important points.
Divide-And-Conquer Sorting Small instance.  n
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.
CIS 068 Welcome to CIS 068 ! Lesson 9: Sorting. CIS 068 Overview Algorithmic Description and Analysis of Selection Sort Bubble Sort Insertion Sort Merge.
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.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
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.
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.
CS 307 Fundamentals of Computer Science 1 bubble sort  Traverse a collection of elements –Move from the front to the end –“Bubble” the largest value to.
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.
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.
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
Sorting.
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.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
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.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
Quicksort Quicksort is a well-known sorting algorithm that, in the worst case, it makes Θ(n 2 ) comparisons. Typically, quicksort is significantly faster.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Sorting: Implementation Fundamental Data Structures and Algorithms Margaret Reid-Miller 24 February 2004.
Sorting Fundamental Data Structures and Algorithms Klaus Sutner February 17, 2004.
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.
Sorting and Lower Bounds Fundamental Data Structures and Algorithms Klaus Sutner February 19, 2004.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Sorting by Tammy Bailey
Divide-And-Conquer-And-Combine
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Chapter 7 Sorting Spring 14
Quick Sort (11.2) CSE 2011 Winter November 2018.
CO 303 Algorithm Analysis And Design Quicksort
Data Structures Review Session
EE 312 Software Design and Implementation I
CSE 332: Sorting II Spring 2016.
Presentation transcript:

Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004

Announcements  Homework #5  Midterm  March 4  Review: March 2

Today - Recall Sorting - Implementation Issues - Average case RT for quicksort - Timing Results

Total Recall: Sorting Algorithms

The Bible Robert Sedgewick Algorithms in C Parts 1-4 Fundamentals, Data Structures, Sorting, Searching Addison-Wesley 1998

Multiple Keys We could use a special comparator function (this would require a special function for each combination of keys). Easier is often to - first sort by name - stable sort by year Done!

Sorting Review Several simple, quadratic algorithms (worst case and average). - Bubble Sort - Selection Sort - Insertion Sort Only Insertion Sort of practical interest: running time linear in number of inversion of input sequence. Constants small. Also stable.

Sorting Review Asymptotically optimal O(n log n) algorithms (worst case and average). - Merge Sort - Heap Sort Merge Sort purely sequential and stable. But requires extra memory: 2n + O(log n).

Quick Sort Overall fastest. In place. BUT: Worst case quadratic. Not stable. Implementation details messy.

Picking An Algorithm First Question: Is the input short? Short means something like n < 500. In this case Insertion Sort is probably the best choice. Don't bother with asymptotically faster methods.

Picking An Algorithm Second Question: Does the input have special properties? E.g., if the number of inversions is small, Insertion Sort may be the best choice. Or linear sorting methods may be appropriate.

Otherwise: Quick Sort Large inputs, comparison based method, stability not required (recall our stabilizer trick, though). Quick Sort is worst case quadratic, why should it be the default candidate? On average, Quick Sort is O(n log ), and the constants are quite small.

Average ??? Average case analysis requires a probability distribution on the inputs: we have to average the running times. t(n) =  p x t(x) where the sum is over all instances of size n and p x is the probability of getting instance x. Often simply assume uniform distribution: every instance (of a certain size) is equally likely.

A Computation Can we write down a recurrence equation? Can we solve the equation? At least approximately? Is the solution (if any) practically relevant? (see handout from last time)

Implementing Quick Sort

Pivot Selection Ideally, the pivot should be the median. Much too slow to be of practical value. Instead either - pick the pivot at random, or - take the median of a small sample.

Partitioning Partitioning is easy if we use extra scratch space. But we would like to partition in place. Need to move elements within the same given block of the big array. Basic idea: use two pointers, sweep across block from left and right till an out-of-place element is encountered. Swap them.

1. Doing quicksort in place LR LR LR

1. Doing quicksort in place LR RL LR

Pseudo Code i = lo – 1; j = hi; while( true ) { while( A[++i] < p ); while( p < a[--j] ) if( j==lo ) break; if( i >= j ) break; swap( i, j ); } swap( i, hi ); return i;

Getting Out Using Quick Sort on very short arrays is a bad idea: the overhead becomes too large. So, when the block becomes short we should exit Quick Sort and switch to Insertion Sort. But not locally: quicksort( A, lo, hi ) { if( hi – lo < magic_number ) insertionsort( A, lo, hi ); else …

Getting Out Just do nothing when the block is short. Then do one global cleanup with insertion sort. quicksort( A, 0, n ) insertionsort( A, 0, n ); This is linear, since the number of inversions is linear.

Magic Number The best way to determine the magic number is to run real-world tests. It seems that for current architectures, some value in the range 5 to 20 will work best.

Equal Elements Note that ideally pivoting should produce three sub-blocks: left:< p middle:== p right:> p Then the recursion could ignore the middle part, possibly omitting many elements.

Equal Elements Three natural strategies: Both pointers stop. Only one pointer stops. Neither pointer stops. Fact: The first strategy works best overall.

Equal Elements There are clever implementations that partition into three sub-blocks. This is amazingly hard to get both right and fast. Try it!

Application: Quick Select

Selection (Order Statistics) A classical problem: given a list, find the k-th element in the ordered list. The brute-force approach sorts the whole list first, and thus produces more information than required. Can we get away with less than n log n work (in a comparison based world)?

Easy Cases Needless to say, when k is small there are easy answers. - Scan the array and keep track of the k smallest. - Use a Selection Sort approach. But how about general k?

Selection and Partitioning qselect( A, lo, hi, k ) { if( hi <= lo ) return; i = partition( A, lo, hi ); if( i > k ) qselect( A, lo, i-1, k ); if( i < k ) qselect( A, i+1, hi, k ); } This looks like a typo. What’s really going on here?

Quick Select What should we expect as running time? As usual, if there is a ghost in the machine, it could force quadratic behavior. But on average this algorithm is linear. Don’t get any ideas about using this to find the median in the pivoting step of Quick Sort!

Some Timing Results

The Real World Beyond asymptotic analysis, it is always a good idea to do some real world testing. Construct a small test-bed: - automate testing - flexible but simple - organize the data in a useful way