Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Sorting Chapter 8 CSCI 3333 Data Structures.
CSC 2300 Data Structures & Algorithms March 16, 2007 Chapter 7. Sorting.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Using Divide and Conquer for Sorting
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
Chapter 7: Sorting Algorithms
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]
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Quicksort CSC 172 SPRING 2002 LECTURE 13. Quicksort The basic quicksort algorithm is recursive Chosing the pivot Deciding how to partition Dealing with.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Quicksort CSC 172 SPRING 2004 LECTURE 10. Reminders  Project 2 (polynomial linked-list) is due, tomorrow  Wed, 18 th 5PM  Computer Science Office –
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L18 (Chapter 23) Algorithm.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting CS 202 – Fundamental Structures of Computer Science II Bilkent.
CSE 373 Data Structures Lecture 19
Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS.
DIVIDE & CONQUR ALGORITHMS Often written as first as a recursive algorithm Master’s Theorem: T(n) = aT(n/b) + cn i, for some constant integer i, and constants.
CSE 373 Data Structures Lecture 15
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.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
Sorting Fun1 Chapter 4: Sorting     29  9.
Sorting Algorithms 2. Quicksort General Quicksort Algorithm: Select an element from the array to be the pivot Select an element from the array to be the.
Chapter 7: Sorting Algorithms Insertion Sort. Sorting Algorithms  Insertion Sort  Shell Sort  Heap Sort  Merge Sort  Quick Sort 2.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
1 CSE 326: Data Structures A Sort of Detour Henry Kautz Winter Quarter 2002.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Quicksort CSC 172. Quicksort The basic quicksort algorithm is recursive Chosing the pivot Deciding how to partition Dealing with duplicates Wrong decisions.
Sorting Ordering data. Design and Analysis of Sorting Assumptions –sorting will be internal (in memory) –sorting will be done on an array of elements.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Sorting.
Fundamental Data Structures and Algorithms
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Objectives Introduce different known sorting algorithms
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
Data Structures Review Session
Sub-Quadratic Sorting Algorithms
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
CS 3343: Analysis of Algorithms
Copyright © Aiman Hanna All rights reserved
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
CSE 373 Data Structures and Algorithms
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Sorting We have actually seen already two efficient ways to sort:
Presentation transcript:

Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS

Insertion Sort Algorithm – Conceptually, incremental add element to sorted array or list, starting with an empty array (list). –Incremental or batch algorithm. Analysis –In best case, input is sorted: time is O(N) –In worst case, input is reverse sorted: time is O(N 2 ). –Average case is (loose argument) is O(N 2 ) Inversion: elements out of order –critical variable for determining algorithm time-cost –each swap removes exactly 1 inversion

Inversions What is average number of inversions, over all inputs? Let A be any array of integers Let revA be the reverse of A Note: if (i,j) are in order in A they are out of order in revA. And vice versa. Total number of pairs (i,j) is N*(N-1)/2 so average number of inversions is N*(N-1)/4 which is O(N 2 ) Corollary: any algorithm that only removes a single inversion at a time will take time at least O(N 2 )! To do better, we need to remove more than one inversion at a time.

BubbleSort Most frequently used sorting algorithm Algorithm: for j=n-1 to 1 …. O(n) for i=0 to j ….. O(j) if A[i] and A[i+1] are out of order, swap them (that’s the bubble) …. O(1) Analysis –Bubblesort is O(n^2) Appropriate for small arrays Appropriate for nearly sorted arrays Comparision versus swaps ?

Shell Sort: 1959 by Shell Motivated by inversion result - need to move far elements Still quadratic Only in text books Historical interest and theoretical interest - not fully understood. Algorithm: (with schedule 1, 3, 5) –bubble sort things spaced 5 apart –bubble sort things 3 apart –bubble sort things 1 apart Faster than insertion sort, but still O(N^2) No one knows the best schedule

Divide and Conquer: Merge Sort Let A be array of integers of length n define Sort (A) recursively via auxSort(A,0,N) where Define array[] Sort(A,low, high) –if (low == high) return –Else mid = (low+high)/2 temp1 = sort(A,low,mid) temp2 = sort(A,mid,high) temp3 = merge(temp1,temp2)

Merge Int[] Merge(int[] temp1, int[] temp2) –int[] temp = new int[ temp1.length+temp2.length] –int i,j,k –repeat if (temp1[i]<temp2[j]) temp[k++]=temp1[i++] else temp[k++] = temp2[j++] –for all appropriate i, j. Analysis of Merge: – time: O( temp1.length+temp2.length) – memory: O(temp1.length+temp2.length)

Analysis of Merge Sort Time –Let N be number of elements –Number of levels is O(logN) –At each level, O(N) work –Total is O(N*logN) –This is best possible for sorting. Space –At each level, O(N) temporary space –Space can be freed, but calls to new costly –Needs O(N) space –Bad - better to have an in place sort –Quick Sort (chapter 8) is the sort of choice.

Quicksort: Algorithm QuickSort - fastest algorithm QuickSort(S) –1. If size of S is 0 or 1, return S –2. Pick element v in S (pivot) –3. Construct L = all elements less than v and R = all elements greater than v. –4. Return QuickSort(L), then v, then QuickSort(R) Algorithm can be done in situ (in place). On average runs in O(NlogN), but can take O(N 2 ) time –depends on choice of pivot.

Quicksort: Analysis Worst Case: –T(N) = worst case sorting time –T(1) = 1 –if bad pivot, T(N) = T(N-1)+N –Via Telescope argument (expand and add) –T(N) = O(N^2) Average Case (text argument) –Assume equally likely subproblem sizes Note: chance of picking ith is 1/N –T(N) average cost to sort

Analysis continued –T(left branch) = T(right branch) (average) so –T(N) = 2* ( T(0)+T(1)….T(N-1) )/N + N, where N is cost of partitioning –Multiply by N: NT(N) = 2(T(0)+…+T(N-1)) +N^2 (*) –Subtract N-1 case of (*) NT(N) - (N-1)T(N-1) = 2T(N-1) +2N-1 –Rearrange and drop -1 NT(N) = (N+1)T(N-1) + 2N -1 –Divide by N(N+1) T(N)/(N+1) = T(N-1)/N + 2/(N+1)

Last Step Substitute N-1, N-2,... 3 for N –T(N-1)/N = T(N-2)/(N-1) + 2/N –… – T(2)/3 = T(1)/2 +2/3 Add –T(N)/(N+1) = T(1)/2+ 2(1/3+1/ /(N+1) – = 2( 1+1/2 +…) -5/2 since T(1) = 0 – = O(logN) Hence T(N) = N logN In literature, more accurate proof. For best results, choose pivot as median of 3 random values from array.

Lower Bound for Sorting Theorem: if you sort by comparisons, then must use at least log(N!) comparisons. Hence Ω(N logN) algorithm. Proof: –N items can be rearranged in N! ways. –Consider a decision tree where each internal node is a comparison. –Each possible array goes down one path –Number of leaves N! –minimum depth of a decision tree is log(N!) –log(N!) = log1+log2+…+log(N) is O(N logN) –Proof: use partition trick sum log(N/2) + log(N/2+1)….log(N) >N/2*log(N/2)

Summary For online sorting, use heapsort. –Online : get elements one at at time –Offline or Batch: have all elements available For small collections, bubble sort is fine For large collections, use quicksort You may hybridize the algorithms, e.g –use quicksort until the size is below some k –then use bubble sort Sorting is important and well-studied and often inefficiently done. Libraries often contain sorting routines, but beware: the quicksort routine in Visual C++ seems to run in quadratic time. Java sorts in Collections are fine.