EFFICIENCY & SORTING II CITS1001. 2 Scope of this lecture Quicksort and mergesort Performance comparison.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

CS 112 Introduction to Programming Sorting of an Array Debayan Gupta Computer Science Department Yale University 308A Watson, Phone:
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
HST 952 Computing for Biomedical Scientists Lecture 9.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
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:
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
Copyright (C) Gal Kaminka Data Structures and Algorithms Sorting II: Divide and Conquer Sorting Gal A. Kaminka Computer Science Department.
QuickSort The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
CMPS1371 Introduction to Computing for Engineers SORTING.
A Introduction to Computing II Lecture 8: Sorting 2 Fall Session 2000.
Data Structures and Algorithms
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE 373: Data Structures and Algorithms
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
1 TCSS 342, Winter 2005 Lecture Notes Sorting Weiss Ch. 8, pp
Sorting Algorithms Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
Recursion CITS1001.
(c) , University of Washington
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
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.
Sort Algorithms.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting: Advanced Techniques Smt Genap
1 Sorting اعداد: ابوزيد ابراهيم حامد سعد صبرة حميده الشاذلي عبدالاه السيد محمد احمد.
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.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting 9/13/2010. Introduction In CS1 you covered Insertion Sort, Bubble Sort, and Selection Sort. – In these algorithms we end up making a significant.
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.
CS 367 Introduction to Data Structures Lecture 11.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Lecture 25: Searching and Sorting
Efficiency & SORTING CITS1001.
slides created by Marty Stepp and Hélène Martin
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
CSC215 Lecture Algorithms.
slides created by Marty Stepp
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
slides created by Marty Stepp
slides created by Marty Stepp
CSE 143 Sorting reading: 13.3, 13.4.
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
slides created by Marty Stepp and Hélène Martin
Algorithms: Design and Analysis
slides adapted from Marty Stepp
Application: Efficiency of Algorithms II
Workshop for CS-AP Teachers
Presentation transcript:

EFFICIENCY & SORTING II CITS1001

2 Scope of this lecture Quicksort and mergesort Performance comparison

Recursive sorting All of the algorithms so far build up the “sorted part” of the array one element at a time What if we take a completely different approach? Faster algorithms split the elements to be sorted into groups, sort the groups separately, then combine the results There are two principal approaches “Intelligent” splitting and “simple” combining Simple splitting and intelligent combining These are divide-and-conquer algorithms 3

Quicksort When sorting n items, Quick Sort works as follows Choose one of the items p to be the pivot Partition the items into L (items smaller than p) and U (items larger than p) L’ = sort(L) U’ = sort(U) The sorted array is then L’ + p + U’, in that order Intelligent splitting, and simple combining 4

5 Behaviour of quicksort Choose a pivot (7) Items smaller than the pivot Items larger than the pivot Sort Append

6 Second level Choose a pivot (9) Items smaller than the pivot Items larger than the pivot Sort Append

What if l == u ? 7 Code for quickSort public static void quickSort(int[] a) { qsort(a, 0, a.length – 1); } // sort a[l..u] inclusive private static void qsort(int[] a, int l, int u) { if (l < u) { int p = partition(a, l, u); qsort(a, l, p – 1); qsort(a, p + 1, u); }

8 Code for partition // put the pivot into si, // with smaller items on its left and larger items on its right private static int partition(int[] a, int l, int u){ // this code always uses a[u] as the pivot int si = l; for (int i = l; i < u; i++) if (a[i] <= a[u]) swap(a, i, si++); // swap small elements to the front swap(a, si, u); // swap the pivot to be between the smalls and larges return si; }

9 Behaviour of partition sia[u] sia[u] a[0] < a[u], so a[0] ↔ a[si] and si sia[u] a[2] < a[u], so a[2] ↔ a[si] and si sia[u] a[5] < a[u], so a[5] ↔ a[si] and si++ a[7] ↔ a[si], return si sia[u]

Mergesort When sorting n items, Merge Sort works as follows Let F be the front half of the array, and B be the back half F’ = sort(F) B’ = sort(B) Merge F’ and B’ to get the sorted list – repeatedly compare their first elements and take the smaller one Simple splitting, and intelligent combining 10

11 Behaviour of mergesort Front halfBack half Sort Merge

12 Second level Front halfBack half Sort Merge

Again, if l == u, there is only one element: no sorting is needed 13 Code for mergeSort public static void mergeSort(int[] a){ msort(a, 0, a.length - 1); } // sort a[l..u] inclusive private static void msort(int[] a, int l, int u){ if (l < u) {int m = (l + u) / 2; msort(a, l, m); msort(a, m + 1, u); merge(a, l, m, u);} }

14 Code for merge // merge a[l..m] with a[m+1..u] private static void merge(int[] a, int l, int m, int u) { while (l <= m && a[l] <= a[m + 1]) l++; // small elements on the 1st list needn't be moved if (l <= m) // if the 1st list is exhausted, we're done { while (u >= m + 1 && a[u] >= a[m]) u--; // large elements on the 2nd list needn't be moved int start = l; // record the start and finish points of the 1st list int finish = m++; int[] b = new int[u - l + 1]; // this is where we will put the sorted list int z = 0; while (m <= u) // while the 2nd list is alive, copy the smallest element to b if (a[l] <= a[m]) b[z++] = a[l++]; else b[z++] = a[m++]; while (z < b.length) b[z++] = a[l++]; // copy the rest of the 1st list for (int i = 0; i < b.length; i++) a[start + i] = b[i]; // copy the sorted list back from b }

Efficiency experiment Is there any difference between the performance of all these sorting algorithms? After all they all achieve the same result… Which one(s) are more efficient? Why? Experiment: use the provided Sorter class to estimate the execution time of each algorithm for sorting a large, disordered array Graph your results 15

Performance Comparison AlgorithmTime to sort (ms) 1,000 items 10,000 items 100,000 items 1,000,000 items Bubble220020,0002,052,560 Selection1515,925605,594 Insertion1232,575281,493 Quick Merge

Analysis Why are quicksort and mergesort so much faster? The first three algorithms all reduce the number of items to be sorted by one in each pass And each pass takes linear time Therefore their overall run-time is n 2, i.e. quadratic Multiplying the number of items by 10 multiplies run-time by 10 2 = 100 Quicksort and mergesort reduce the number of items by half at each level And each level takes linear time Therefore their overall run-time is nlog 2 n Multiplying the number of items by 10 multiplies run-time by 10 and a bit 17

A note on the accuracy of such tests Assessing the execution time of Java code this way is not completely accurate You will not always get the same results Activities such as garbage collection may affect the times Or just if your computer is running other applications concurrently We “average out” unrepresentative examples by using Random data Multiple runs 18

Summary We study sorting algorithms because they provide good examples of many of the features that affect the run-time of program code. When checking the efficiency of your own code, consider Number of loops, and depth of nesting Number of comparison operations Number of swap (or similar) operations 19