Sorting Ordering data. Design and Analysis of Sorting Assumptions –sorting will be internal (in memory) –sorting will be done on an array of elements.

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.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
CSE 373: Data Structures and Algorithms
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
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.
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Quicksort Divide-and-Conquer. Quicksort Algorithm Given an array S of n elements (e.g., integers): If array only contains one element, return it. Else.
Topic 17 Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical.
1 TCSS 342, Winter 2005 Lecture Notes Sorting Weiss Ch. 8, pp
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
CS2420: Lecture 9 Vladimir Kulyukin Computer Science Department Utah State University.
1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).
Sorting Chapter 10. Chapter 10: Sorting2 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn how to implement.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting CS 202 – Fundamental Structures of Computer Science II Bilkent.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Sorting Text Read Shaffer, Chapter 7 Sorting O(N 2 ) sorting algorithms: – Insertion, Selection, Bubble O(N log N) sorting algorithms – HeapSort, MergeSort,
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.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
1 Chapter 7: Sorting (Insertion Sort, Shellsort) CE 221 Data Structures and Algorithms Izmir University of Economics Text: Read Weiss, § 7.1 – 7.4.
1 Sorting and Searching "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.
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.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
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.
Sorting: Advanced Techniques Smt Genap
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
CS 146: Data Structures and Algorithms July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Heapsort, Mergesort, and Quicksort Sections 7.5 to 7.7.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Fast Sorting COMP
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
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
Today’s Material Sorting: Definitions Basic Sorting Algorithms
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.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
CS 367 Introduction to Data Structures Lecture 11.
1 Chapter 7: Sorting (Insertion Sort, Shellsort) CE 221 Data Structures and Algorithms Izmir University of Economics Text: Read Weiss, § 7.1 – 7.4.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Chapter 5 Sorting There are several easy algorithms to sort in O(N 2 ), such as insertion sort. There is an algorithm, Shellsort, that is very simple to.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
Prof. U V THETE Dept. of Computer Science YMA
Sorting Mr. Jacobs.
Chapter 7 Sorting Spring 14
Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems."
Advanced Sorting Methods: Shellsort
Quick Sort (11.2) CSE 2011 Winter November 2018.
slides adapted from Marty Stepp
Topic 17 Faster Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical.
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Merge Sort (11.1) CSE 2011 Winter April 2019.
Sorting Chapter 10.
Advanced Sorting Methods: Shellsort
Presentation transcript:

Sorting Ordering data

Design and Analysis of Sorting Assumptions –sorting will be internal (in memory) –sorting will be done on an array of elements –the number of elements will be somewhat small (less than a few million)

Why should we sort? Searching –Searching a sorted array is much easier than searching an unsorted array Examples of sorted lists –Dictionaries –File directories –Card catalogues –Programs for graduation

Sorts we will consider Bubble Sort Selection Sort Insertion Sort Shell Sort Quick Sort Merge Sort

Performance There are two operations which affect the performance of sorting algorithms –Comparison comparing two objects to see which one comes first –Exchanges exchanging two objects which are ‘out of order’ Performance is affected by the number of each of these that must be done

Bubble Sort //a is the array to be sorted for(int p = 0; p < a.length-1; p++) for(int k = 1; k < a.length-p; k++) if (a[k].compareTo(a[k-1])< 0) exchange(a[k-1],a[k]);

Analysis of Bubble Sort Assume N is the number of elements in the array The number of comparisons is (N-1)+(N-2)+…+1 =(N-1)N/2 = O( N 2 ) The number of exchanges is between 0 and (N-1)N/2, depending on the data Poorest performance is on reversed data

Selection Sort for(int p = 0; p < a.length-1; p++) { int k = indexOfMin(a,p,a.length-1); exchange(a[p],a[k]); }

Analysis of Selection Sort indexOfMin() clearly performs O( N ) comparisons There are, thus, O( N 2 ) comparisons performed However, there are exactly N-1 exchanges performed (although some of these may exchange an object with itself!)

Insertion Sort for(int p = 1; p < a.length; p++) { tmp = a[p]; int j = p; while (j > 0 && tmp.compareTo(a[j-1])< 0) { a[j] = a[j-1]; j--; } a[j]=tmp; }

Analysis of Insertion Sort The outer (for) loop is O( N ) The inner (while) loop executes 0 to N-1 times, depending on the data Insertion Sort does O( N 2 ) comparisons and data moves If the data is sorted already, insertion sort performs N-1 comparisons and 0 exchanges.

Inversions An inversion in an array, a, is any pair (i,j), where i a[j] The average number of inversions in an array of N elements is N(N-1)/4 Any sort algorithm that sorts by exchanging adjacent elements performs N(N-1)/4=  ( N 2 ) exchanges on average.

Shell sort Developed in 1959 by Donald Shell, to improve the performance of insertion sort. Avoids large amounts of data movement by first comparing elements that are far apart, and then comparing ones less far apart, and so on, gradually shrinking toward insertion sort

Shell Sort algorithm for(int gap = a.length/2; gap > 0; gap = gap == 2 ? 1: (int)(gap/2.2)) for(int i = gap; i < a.length; i++) { tmp = a[i]; int j = i; while(j>= gap && tmp.compareTo(a[j-gap])<0) { a[j]= a[j-gap]; j-= gap; } a[j] = tmp; }

Analysis of Shell Sort It can be shown that the worst case for Shell sort is O( N 2 ) With appropriate choice of decreasing gaps, this performance can be improved [dividing the previous gap by 2.2 performs better in practice than dividing by 2, for example] Shell sort is acceptable in practice, even with N in the tens of thousands

Divide & Conquer Divide and conquer sorting algorithms can possibly break the O( N 2 ) barrier There are two phases of these algorithms –divide array into two smaller arrays –sort and then combine these subarrays The algorithms –QuickSort –MergeSort

MergeSort Algorithm –if the number of elements to sort is 0 or 1, return –recursively sort the left and right halves –merge these two halves into a sorted list

MergeSort private static void mergeSort( Comparable [ ] a, Comparable [ ] tmpArray, int left, int right ) { if( left < right ) { int center = ( left + right ) / 2; mergeSort( a, tmpArray, left, center ); mergeSort( a, tmpArray, center + 1, right ); merge( a, tmpArray, left, center + 1, right ); } }

MergeSort Continued private static void merge( Comparable [ ] a, Comparable [ ] tmpArray, int leftPos, int rightPos, int rightEnd ) { int leftEnd = rightPos - 1; int tmpPos = leftPos; int numElements = rightEnd - leftPos + 1; // Main loop while( leftPos <= leftEnd && rightPos <= rightEnd ) if( a[ leftPos ].compareTo( a[ rightPos ] ) <= 0 ) tmpArray[ tmpPos++ ] = a[ leftPos++ ]; else tmpArray[ tmpPos++ ] = a[ rightPos++ ];

MergeSort Continued while( leftPos <= leftEnd ) // Copy rest of first half tmpArray[ tmpPos++ ] = a[ leftPos++ ]; while( rightPos <= rightEnd ) // Copy rest of right half tmpArray[ tmpPos++ ] = a[ rightPos++ ]; // Copy tmpArray bac for( int i = 0; i < numElements; i++, rightEnd-- ) a[ rightEnd ] = tmpArray[ rightEnd ]; }

Analysis of MergeSort The division phase is done in constant time. Merging is a O( N ) operation It takes log N divisions to get to size 1 Thus, MergeSort is O( N log N ) The major drawback of MergeSort –merging requires twice the space

QuickSort Algorithm to sort S –if the number of elements in S is 0 or 1, return –pick any element v in S, call it the pivot –Partition S into three sets L = { x  S | x < v } M = { x  S | x = v } U = { x  S | x > v } –return QuickSort(L), M, QuickSort(U)

QuickSort private static void quicksort( Comparable [ ] a, int low, int high) { if( low + CUTOFF > high ) insertionSort( a, low, high ); else { // Sort low, middle, high int middle = ( low + high ) / 2; if( a[ middle ].compareTo( a[ low ] ) < 0 ) swapReferences( a, low, middle ); if( a[ high ].compareTo( a[ low ] ) < 0 ) swapReferences( a, low, high ); if( a[ high ].compareTo( a[ middle ] ) < 0 ) swapReferences( a, middle, high );

QuickSort Continued / / Place pivot at position high - 1 swapReferences( a, middle, high - 1 ); Comparable pivot = a[ high - 1 ]; // Begin partitioning int i, j; for( i = low, j = high - 1; ; ) { while( a[ ++i ].compareTo( pivot ) = j ) break; swapReferences( a, i, j ); }

QuickSort Continued // Restore pivot swapReferences( a, i, high - 1 ); quicksort( a, low, i - 1 ); // Sort small elements quicksort( a, i + 1, high ); // Sort large elements } //else }

Analysis of QuickSort In the worst case, one of the sets L or U is empty, and the performance is O( N 2 ) In the best case (L and U are the same size at every stage), the performance is O( N log N ) In the average case, the performance can be shown to be O( N log N )

choosing the pivot Naïve choice – choose the first element –leads to the worst case if data is almost sorted or almost reverse order Another bad choice – choose the element at index (low +hi)/2 –easy to construct data where worst case performance happens

choosing the pivot (2) A better choice - median of three –choose the median of elements at indices low, (low+high)/2, and high and use this as the pivot. –The chance that one of the sets L or U is empty is reduced by this technique

Any algorithm that sorts by using element comparisons only must use at least  log( N !)  comparison for some input sequence  log( N !)  is approximately N log N N The best sort of this type, thus, has performance O( N log N ) in the average case