Unit 281 Merge- and Quick Sort Merge Sort Quick Sort Exercises.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
The Substitution method T(n) = 2T(n/2) + cn Guess:T(n) = O(n log n) Proof by Mathematical Induction: Prove that T(n)  d n log n for d>0 T(n)  2(d  n/2.
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.
Sorting Algorithms and Average Case Time Complexity
Faster Sorting Methods Chapter Chapter Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Iterative Merge Sort.
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.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting III.
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.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Merge- and Quick Sort Reading p Merge Sort Quick Sort.
Faster Sorting Methods Chapter 9. 2 Chapter Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Merge Sort in the Java.
Sorting21 Recursive sorting algorithms Oh no, not again!
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting III 1 An Introduction to Sorting.
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
Quicksort.
Quicksort.
Unit 061 Quick Sort csc326 Information Structures Spring 2009.
Cmpt-225 Sorting – Part two. Idea of Quick Sort 1) Select: pick an element 2) Divide: partition elements so that x goes to its final position E 3) Conquer:
Unit 281 Merge- and Quick Sort Merge Sort Quick Sort Exercises.
Unit 271 Searching and Sorting Linear Search Binary Search Selection Sort Insertion Sort Bubble (or Exchange) Sort Exercises.
CSCD 326 Data Structures I Sorting
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and 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.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
CENG 213 Data Structures Sorting Algorithms. CENG 213 Data Structures Sorting Sorting is a process that organizes a collection of data into either ascending.
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.
Quicksort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
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.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Sorting divide and conquer. Divide-and-conquer  a recursive design technique  solve small problem directly  divide large problem into two subproblems,
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
Review 1 Insertion Sort Insertion Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Merge Sort. In plain English: if the size of the array > 1, split the array into two halves, and recursively sort both halves; when the sorts return,
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Nothing is particularly hard if you divide it into small jobs. Henry Ford Nothing is particularly hard if you divide it into small jobs. Henry Ford.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
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 Ordering data. Design and Analysis of Sorting Assumptions –sorting will be internal (in memory) –sorting will be done on an array of elements.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Algorithm Efficiency and Sorting
Chapter 7 Sorting Spring 14
CSE 143 Lecture 23: quick sort.
Advanced Sorting Methods: Shellsort
Unit-2 Divide and Conquer
slides adapted from Marty Stepp
Chapter 4.
CSE 373 Data Structures and Algorithms
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Presentation transcript:

Unit 281 Merge- and Quick Sort Merge Sort Quick Sort Exercises

Unit 282 Merge Sort Merge Sort uses the algorithmic paradigm of divide and conquer: Divide the block into two subblocks of “equal” size; Merge Sort each block recursively Merge the two sorted sub-blocks (next to each other) to give a sorted block. The following is the merge sort algorithm to sort a sub-array from index low to index high: if(high > low){ Split the sub-array into two halves merge sort the left half merge sort the right half merge the two sorted halves into one sorted array } The above algorithm translates to the following pseudo-code: mergeSort(array, low, high){ middle = (low + high) / 2; mergeSort(array, low, middle); mergeSort(array, middle + 1, high); merge(array, low, middle, middle + 1, high); }

Unit 283 Merge Sort (cont’d) Note that in a merge sort, the splitting and merging process are intermingled. However, we can view merge sort as: 1. Continually splitting the original array of size n until it has created n one element subarrays (each of which is a sorted subarray). 2. Adjacent sorted subarrays are then merged into larger and larger sorted subarrays, until the entire array is merged back. Example: merging two adjacent, sorted portions of an array

Unit 284 Merge Sort (cont’d) The merge Sort algorithm is implemented for sorting an array of objects based on their natural ordering. Another version can be implemented for sorting according to a comparator object. public static void mergeSort(Object[] array, Comparator comp){ mergeSort(array, 0, array.length - 1, comp); } private static void mergeSort(Object[] array, int low, int high, Comparator comp){ if(last > first){ int mid = (low + high)/2; mergeSort(array, low, mid, comp); mergeSort(array, mid + 1, high, comp); merge(array, low, mid, mid + 1, high, comp); }

Unit 285 Merge Sort (cont’d) In Merge Sort algorithm, the main work is done by the merge method as shown below. private static void merge(Object[] array,int leftLowIndex, int leftHighIndex,int rightLowIndex, int rightHighIndex, Comparator comp){ int low = leftLowIndex; int index = leftLowIndex; Object[] tempArray = new Object[array.length]; // temporary array while(leftLowIndex <= leftHighIndex && rightLowIndex <= rightHighIndex){ if(comp.compare(array[leftLowIndex], array[rightLowIndex]) < 0) tempArray[index++] = array[leftLowIndex++]; else tempArray[index++] = array[rightLowIndex++]; } while (leftLowIndex <= leftHighIndex) tempArray[index++] = array[leftLowIndex++]; while (rightLowIndex <= rightHighIndex) tempArray[index++] = array[rightLowIndex++]; for(int k = low; k <= rightHighIndex; k++) array[k] = tempArray[k]; }

Unit 286 Merge Sort (cont’d) A recursive-tree for merge sorting an array x with 7 mergeSort(array, low, high){ if(high > low){ middle = (low + high) / 2; mergeSort(array,low,middle); mergeSort(array, middle + 1, high); merge(array, low, middle, middle + 1, high); }

Unit 287 Quick Sort Quick Sort also uses the algorithmic paradigm of divide and conquer. The following is a quick sort algorithm to sort an array from index low to high: if(high > low){ // number of elements in the sub-array greater then 1 (condition for recursive step) 1. Select a pivot element p to be used to partition the array into two partitions. 2. Scan through the array, moving all elements smaller than p to the lower partition, and all elements equal to or greater than p to the upper partition. 3. Sort the low and upper partitions (without the pivot element) recursively using quick sort. } The above algorithm can be translated to the following pseudo-code: quickSort(array, low, high){ if(high > low){ elect a pivotValue partition the array so that: array[low]...array[pivotIndex – 1] < pivotValue array[pivotIndex] = pivotValue array[pivotIndex + 1]...array[high] >= pivotValue quickSort(array, low, pivotIndex – 1); quickSort(array, pivotIndex + 1, high); }

Unit 288 Quick Sort (cont’d) Any array element can be selected as pivot; but ideally the pivot should be the median value. Here the pivot is taken as the middle value of the sub-array. Quick sort is most efficient when the pivot is as close to the median as possible; otherwise the depth of recursion increases resulting in a decrease in efficiency. Computing median values in each partition degrades the performance of quick sort. The method implemented here sorts based on the natural ordering. The version using the comparator object can be implemented too. The Quick sort method is: public static void quickSort(Comparable[] array ){ quickSort(array, 0, array.length - 1, comp); } private static void quickSort(Comparable[] array, int low,int high ){ if(low < high){ int pivotIndex = partition(array, low, high, comparator); quickSort(array, low, pivotIndex - 1); quickSort(array, pivotIndex + 1, high); }

Unit 289 Quick Sort (cont’d) private static int partition(Comparable[] array, int firstIndex, int lastIndex ){ int middleIndex = (firstIndex + lastIndex)/2; Comparable pivotValue = array[middleIndex]; /* Temporarily place pivotValue into first position of current partition */ swap(array, firstIndex, middleIndex); //implementation not shown int pivotIndex = firstIndex; int index = firstIndex + 1; while(index = to pivot if(array[index]. compareTo(pivotValue) < 0){ ++pivotIndex; swap(array, pivotIndex, index); } index++; } // Return pivotValue to partition point swap(array, firstIndex, pivotIndex); return pivotIndex; // return location of pivot after partitioning done }

Unit 2810 Quick Sort (cont’d)

Unit 2811 Exercises 1.What is the advantage of the bubble sort algorithm we have studied over all the other sorting methods we have studied? 2.When are bubble, insertion, and selection sort methods more efficient than merge sort and quicksort? 3.What is the disadvantage of merge sort as compared to quicksort? 4.What role, if any, does the size of data objects play in a sort? 5.Give the merge sort recursion-tree for the array: 43, 7, 10, 23, 18, 4, 19, 5, 66, 14, 2 6.A merge sort is used to sort an array of 1000 integers in descending order. Which of the following statements is true? (a) The sort is fastest if the original integers are sorted in ascending order. (b)The sort is fastest if the original integers are sorted in descending order. (c)The sort is fastest if the original integers are completely in random order. (d)The sort is the same, no matter what the order of the original integers. 7.A different method for choosing the pivot for each partition in quicksort is to take the median of the first, last and central keys. Implement a quicksort algorithm using this approach. 8. A different approach to the selection of a pivot in quicksort is to take the average of all the keys in a partition (assuming that the keys are numeric) as the pivot for that partition. The resulting algorithm is called meansort. Implement a quicksort algorithm using this approach. Note: The average of the keys is not necessarily one of the keys in the array.

Unit 2812 Exercises (cont’d) 9.In quicksort, why is it better to choose the pivot from the center of the array rather from one of the ends? 10.What is the property of the best pivot in a quicksort? 11.What is the property of the worst pivot in a quicksort? 12.Suppose that, instead of sorting, we wish only to find the m th quicksort can be adapted to this problem, doing much less work than a complete sort. 13.Implement a non-recursive mergesort, where the length of the array is a power of 2. First merge adjacent regions of size 1, then adjacent regions of size 2, then adjacent regions of size 4, and so on. 14.Implement a non-recursive mergesort, where the length of the array is an arbitrary number. Hint: Use a stack to keep track of which subarrays have been sorted. 15. Implement each of the sorting algorithms we have studied such that each displays the number of swaps and comparisons. Each of the implementations should be used to sort integer arrays with the same 100 random integers.

Unit 2813 Exercises (cont’d) 16.Design a test program to compare the execution times of bubble sort, selection sort, insertion sort, merge sort, and quick sort on integers arrays of equal lengths with similar random integers. For shorter lengths, sort many arrays and obtain the average execution time.