CSC 143 Java Sorting.

Slides:



Advertisements
Similar presentations
CSC 421: Algorithm Design & Analysis
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS4413 Divide-and-Conquer
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
(c) , University of Washington
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.
Computer Science Searching & Sorting.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
(c) , University of Washington18a-1 CSC 143 Java Searching and Recursion N&H Chapters 13, 17.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
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.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Searching and Sorting Searching algorithms with simple arrays
Prof. U V THETE Dept. of Computer Science YMA
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
CSCI 104 Sorting Algorithms
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Algorithm Efficiency and Sorting
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
CSC 421: Algorithm Design & Analysis
Divide-and-Conquer The most-well known algorithm design strategy:
Quicksort "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Divide and Conquer.
Data Structures and Algorithms
Divide and Conquer – and an Example QuickSort
Quicksort 1.
Algorithm Design Methods
Divide-and-Conquer The most-well known algorithm design strategy:
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Quicksort and Mergesort
CSC 143 Searching and Sorting [Chapter 9, pp ]
Chapter 4: Divide and Conquer
CSC212 Data Structure - Section RS
Unit-2 Divide and Conquer
8/04/2009 Many thanks to David Sun for some of the included slides!
Divide-and-Conquer The most-well known algorithm design strategy:
Sub-Quadratic Sorting Algorithms
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Chapter 4.
CSE 326: Data Structures Sorting
Quicksort.
Algorithms Dr. Youn-Hee Han April-May 2013
CSC 421: Algorithm Design & Analysis
CS 1114: Sorting and selection (part two)
CSE 373 Data Structures and Algorithms
Algorithms: Design and Analysis
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Chapter 10 Sorting Algorithms
Algorithm Efficiency and Sorting
Quicksort.
Searching/Sorting/Searching
CSC 380: Design and Analysis of Algorithms
CSC 143 Java Searching.
Quicksort and Randomized Algs
CSC 421: Algorithm Design & Analysis
Algorithm Efficiency and Sorting
Searching and Sorting Hint at Asymptotic Complexity
Divide and Conquer Merge sort and quick sort Binary search
Quicksort.
Presentation transcript:

CSC 143 Java Sorting

Sorting Binary search is a huge speedup over sequential search But requires the list be sorted Slight Problem: How do we get a sorted list? Maintain the list in sorted order as each word is added Sort the entire list when needed Many, many algorithms for sorting have been invented and analyzed Our algorithms all assume the data is already in an array Other starting points and assumptions are possible

Insert for a Sorted List Exercise: Assume that words[0..size-1] is sorted. Place new word in correct location so modified list remains sorted Assume that there is spare capacity for the new word (what kind of condition is this?) Before coding: Draw pictures of an example situation, before and after Write down the postconditions for the operation // given existing list words[0..size-1], insert word in correct place and increase size void insertWord(String word) { size++; }

Insertion Sort Once we have insertWord working... We can sort a list in place by repeating the insertion operation void insertionSort( ) { int finalSize = size; size = 1; for (int k = 1; k < finalSize; k++) { insertWord(words[k]); }

Insertion Sort As A Card Game Operation A bit like sorting a hand full of cards dealt one by one: Pick up 1st card – it's sorted, the hand is sorted Pick up 2nd card; insert it after or before 1st – both sorted Pick up 3rd card; insert it after, between, or before 1st two … Each time: Determine where new card goes Make room for the newly inserted member.

Insertion Sort As Invariant Progression sorted unsorted

Insertion Sort Code (C++) sorted unsorted sorted unsorted Insertion Sort Code (C++) void insert(int list[], int n) { int i; for (int j=1 ; j < n; ++j) { // pre: 1<=j && j<n && list[0 ... j-1] in sorted order int temp = list[j]; for ( i = j-1 ; i >= 0 && list[i] > temp ; --i ) { list[i+1] = list[i] ; } list[i+1] = temp ; // post: 1<=j && j<n && list[0 ... j] in sorted order

Insertion Sort Trace Initial array contents 0 pear 1 orange 2 apple 3 rutabaga 4 aardvark 5 cherry 6 banana 7 kumquat

Insertion Sort Performance Cost of each insertWord operation: Number of times insertWord is executed: Total cost: Can we do better?

Analysis Why was binary search so much more effective than sequential search? Answer: binary search divided the search space in half each time; sequential search only reduced the search space by 1 item Why is insertion sort O(n2)? Each insert operation only gets 1 more item in place at cost O(n) O(n) insert operations Can we do something similar for sorting?

Where are we on the chart? N log2N 5N N log2N N2 2N =============================================================== 8 3 40 24 64 256 16 4 80 64 256 65536 32 5 160 160 1024 ~109 64 6 320 384 4096 ~1019 128 7 640 896 16384 ~1038 256 8 1280 2048 65536 ~1076 10000 13 50000 105 108 ~103010

Divide and Conquer Sorting Idea: emulate binary search in some ways divide the sorting problem into two subproblems; recursively sort each subproblem; combine results Want division and combination at the end to be fast Want to be able to sort two halves independently This is a an algorithm strategy known as “divide and conquer”

Quicksort Invented by C. A. R. Hoare (1962) Idea Pick an element of the list: the pivot Place all elements of the list smaller than the pivot in the half of the list to its left; place larger elements to the right Recursively sort each of the halves Before looking at any code, see if you can draw pictures based just on the first two steps of the description

Code for QuickSort // Sort words[0..size-1] void quickSort( ) { qsort(0, size-1); } // Sort words[lo..hi] void qsort(int lo, int hi) { // quit if empty partition if (lo > hi) { return; } int pivotLocation = partition(lo, hi); // partition array and return pivot loc qsort(lo, pivotLocation-1); qsort(pivotLocation+1, hi);

Recursion Analysis Base case? Yes. Recursive cases? Yes // quit if empty partition if (lo > hi) { return; } Recursive cases? Yes qsort(lo, pivotLocation-1); qsort(pivotLocation+1, hi); Observation: recursive cases work on a smaller subproblem, so algorithm will terminate

A Small Matter of Programming Partition algorithm Pick pivot Rearrange array so all smaller element are to the left, all larger to the right, with pivot in the middle Partition is not recursive Fact of life: partition is tricky to get right How do we pick the pivot? For now, keep it simple – use the first item in the interval Better strategies exist

Partition design We need to partition words[lo..hi] Pick words[lo] as the pivot Picture:

A Partition Implementation Use first element of array section as the pivot Invariant: lo L R hi A x <=x unprocessed >x pivot

Partition Algorithm: PseudoCode The two-fingered method // Partition words[lo..hi]; return location of pivot in range lo..hi int partition(int lo, int hi)

Partition Test Check: partition(0,7) 0 orange 1 pear 2 apple 3 rutabaga 4 aardvark 5 cherry 6 banana 7 kumquat

Complexity of QuickSort Each call to Quicksort (ignoring recursive calls): One call to partition = O(n), where n is size of part of array being sorted Note: This n is smaller than the N of the original problem Some O(1) work Total = O(n) for n the size of array part being sorted Including recursive calls: Two recursive calls at each level of recursion, each partitions “half” the array at a cost of O(N/2) How many levels of recursion?

QuickSort (Ideally) ... ... ... N N/2 N/2 N/4 N/4 N/4 N/4 1 1 1 1 All boxes are executed (except some of the 0 cases) Total work at each level is O(N) N N/2 N/2 N/4 N/4 N/4 N/4 ... ... 1 1 1 1 ...

QuickSort Performance (Ideal Case) Each partition divides the list parts in half Sublist sizes on recursive calls: n, n/2, n/4, n/8…. Total depth of recursion: __________________ Total work at each level: O(n) Total cost of quicksort: ________________ ! For a list of 10,000 items Insertion sort: O(n2): 100,000,000 Quicksort: O(n log n): 10,000 log2 10,000 = 132,877

Best Case for QuickSort Assume partition will split array exactly in half Depth of recursion is then log2 N Total work is O(N)*O(log N ) = O(N log N), much better than O(N2) for selection sort Example: Sorting 10,000 items: Selection sort: 10,0002 = 100,000,000 Quicksort: 10,000 log2 10,000  132,877

Worst Case for QuickSort If we’re very unlucky, then each pass through partition removes only a single element. In this case, we have N levels of recursion rather than log2N. What’s the total complexity? 1 2 3 4 1 2 3 4 2 3 4 3 4 1 2 3 4

QuickSort Performance (Worst Case) Each partition manages to pick the largest or smallest item in the list as a pivot Sublist sizes on recursive calls: Total depth of recursion: __________________ Total work at each level: O(n) Total cost of quicksort: ________________ !

Worst Case vs Average Case QuickSort has been shown to work well in the average case (mathematically speaking) In practice, Quicksort works well, provided the pivot is picked with some care Some strategies for choosing the pivot: Compare a small number of list items (3-5) and pick the median for the pivot Pick a pivot element randomly in the range lo..hi

QuickSort as an Instance of Divide and Conquer Generic Divide and Conquer QuickSort 1. Divide Pick an element of the list: the pivot Place all elements of the list smaller than the pivot in the half of the list to its left; place larger elements to the right 2. Solve subproblems separately (and recursively) Recursively sort each of the halves 3. Combine subsolutions to get overall solution Surprise! Nothing to do

Another Divide-and-Conquer Sort: Mergesort 1. Split array in half just take the first half and the second half of the array, without rearranging 2. Sort the halves separately 3. Combining the sorted halves (“merge”) repeatedly pick the least element from each array compare, and put the smaller in the resulting array example: if the two arrays are 1 12 15 20 5 6 13 21 30 The "merged" array is 1 5 6 12 13 15 20 21 30 note: we will need a temporary result array

Summary Recursion Divide and Conquer Methods that call themselves Need base case(s) and recursive case(s) Recursive cases need to progress toward a base case Often a very clean way to formulate a problem (let the function call mechanism handle bookkeeping behind the scenes) Divide and Conquer Algorithm design strategy that exploits recursion Divide original problem into subproblems Solve each subproblem recursively Can sometimes yield dramatic performance improvements