Sorting Algorithms: Merge and Quick. Lecture Objectives Learn how to implement the simple advanced sorting algorithms (merge and quick) Learn how to implement.

Slides:



Advertisements
Similar presentations
Sorting Sorting places data in ascending or descending order, based on one or more sort keys E.g. A list of names could be sorted alphabetically, bank.
Advertisements

Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Sorting Algorithms and Average Case Time Complexity
CMPS1371 Introduction to Computing for Engineers SORTING.
CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting III.
CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick.
CS 171: Introduction to Computer Science II Quicksort.
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.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
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.
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.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Sorting Algorithms Bubble Sort Merge Sort Quick Sort Randomized Quick Sort.
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.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 14 – Sorting and Searching.
Chapter 14: Sorting and searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fourteen: Sorting and Searching.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 13 – Sorting and Searching.
Recursion A recursive computation solves a problem by using the solution of the same problem with simpler values The same computation occurs repeatedly.
Copyright © 2013 by John Wiley & Sons. All rights reserved. SORTING AND SEARCHING CHAPTER Slides by Rick Giles 14.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 14 – Sorting and Searching.
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.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Sorting and Searching.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 14 – Sorting and Searching.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 14 – Sorting and Searching.
CHAPTER 18 SORTING AND SEARCHING. CHAPTER GOALS To study the several searching and sorting algorithms To appreciate that algorithms for the same task.
Sorting. RHS – SWC 2 Sorting Searching in sorted data is much faster than searching in unsorted data Being able to sort data efficiently is thus a quite.
Chapter 15: Algorithms 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 15 Algorithms.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
EFFICIENCY & SORTING II CITS Scope of this lecture Quicksort and mergesort Performance comparison.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Big-O and Sorting February 6, Administrative Stuff Readings for today: Ch Readings for tomorrow: Ch 8.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Chapter 14 Sorting and Searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Sorting  Sorting places data in ascending or descending order, based on one or more sort.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
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)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Algorithms Lakshmish Ramaswamy. Merge Problem – Merge two sorted arrays such that the resultant array remains sorted Logic –Keep pointers to both arrays.
FASTER SORT using RECURSION : MERGE SORT COMP 103.
1 Overview Divide and Conquer Merge Sort Quick Sort.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Fibonacci Sequence Fibonacci sequence is a sequence of numbers defined by f 1 = 1 f 2 = 1 f n = f n-1 + f n-2 First ten terms – 1, 1, 2, 3, 5, 8, 13, 21,
Using recursion for Searching and Sorting
Fundamentals of Algorithms MCS - 2 Lecture # 11
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Chapter 19 Sorting and Searching
QuickSort Previous slides based on ones by Ethan Apter & Marty Stepp
14 SORTING AND SEARCHING CHAPTER
Algorithms Dr. Youn-Hee Han April-May 2013
Chapter 14 – Sorting and Searching
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Merge Sort (11.1) CSE 2011 Winter April 2019.
Presentation transcript:

Sorting Algorithms: Merge and Quick

Lecture Objectives Learn how to implement the simple advanced sorting algorithms (merge and quick) Learn how to implement the merge and quick sort algorithms To learn how to estimate and compare the performance of divide and conquer sorting algorithms To learn how to estimate and compare the performance of sorting algorithms

Merge Sort Algorithm The merge sort algorithm sorts an array by:  Cutting the array in half  Recursively sorting each half  Merging the sorted halves Dramatically faster than the selection, insertion and bubble sorts! Applies the divide and conquer strategy

Divide an array in half and sort each half Merge Sort Algorithm (Cont’d) Figure 1: An array list during the splitting process

The merging of two sub-arrays is carried out as follows Merge Sort Algorithm (Cont’d) Figure 2: An array list during the splitting process k1k2k k2k3 -5 k1k3 -4 k2k k1k3 35 k2 k1k3 6 k1k3 11 k1k3

File MergeSorter.java 01: /** 02: This class sorts an array, using the merge sort algorithm. 03: */ 04: public class MergeSorter 05: { 06: /** 07: Constructs a merge sorter. anArray the array to sort 09: */ 10: public MergeSorter(int[] anArray) 11: { 12: a = anArray; 13: } 14: 15: /** 16: Sorts the array managed by this merge sorter. 17: */

18: public void sort() 19: { 20: if (a.length <= 1) return; 21: int[] first = new int[a.length / 2]; 22: int[] second = new int[a.length - first.length]; 23: System.arraycopy(a, 0, first, 0, first.length); 24: System.arraycopy(a, first.length, second, 0, second.length); 25: MergeSorter firstSorter = new MergeSorter(first); 26: MergeSorter secondSorter = new MergeSorter(second); 27: firstSorter.sort(); 28: secondSorter.sort(); 29: merge(first, second); 30: } 31: File MergeSorter.java (Cont’d)

32: /** 33: Merges two sorted arrays into the array managed by 34: this merge sorter. first the first sorted array second the second sorted array 37: */ 38: private void merge(int[] first, int[] second) 39: { 40: // Merge both halves into the temporary array 41: 42: int iFirst = 0; 43: // Next element to consider in the first array 44: int iSecond = 0; 45: // Next element to consider in the second array 46: int j = 0; 47: // Next open position in a 48: File MergeSorter.java (Cont’d)

49: // As long as neither iFirst nor iSecond past the 50: // end, move the smaller element into a 51: while (iFirst < first.length && iSecond < second.length) 52: { 53: if (first[iFirst] < second[iSecond]) 54: { 55: a[j] = first[iFirst]; 56: iFirst++; 57: } 58: else 59: { 60: a[j] = second[iSecond]; 61: iSecond++; 62: } 63: j++; 64: } 65: File MergeSorter.java (Cont’d)

66: // Note that only one of the two calls to arraycopy 67: // below copies entries 68: 69: // Copy any remaining entries of the first array 70: System.arraycopy(first, iFirst, a, j, first.length - iFirst); 71: 72: // Copy any remaining entries of the second half 73: System.arraycopy(second, iSecond, a, j, second.length - iSecond); 74: } 75: 76: private int[] a; 77: } File MergeSorter.java (Cont’d)

File MergeSortTester.java 01: /** 02: This program tests the merge sort algorithm by 03: sorting an array that is filled with random numbers. 04: */ 05: public class MergeSortTester 06: { 07: public static void main(String[] args) 08: { 09: int[] a = ArrayUtil.randomIntArray(20, 100); 10: ArrayUtil.print(a); 11: MergeSorter sorter = new MergeSorter(a); 12: sorter.sort(); 13: ArrayUtil.print(a); 14: } 15: } 16:

File MergeSortTester.java (Cont’d) Output:

Analyzing the Merge Sort Algorithm N Merge Sort (milliseconds) Selection Sort (milliseconds) 10, ,000473,051 30,000626,846 40, ,188 50, ,015 60, ,359

Merge Sort Timing vs. Selection Sort Figure 3: Merge Sort Timing (blue) versus Selection Sort (red)

Analyzing the Merge Sort Algorithm In an array of size N, count how many times an array element is visited Assume N is a power of 2: N = 2 m Calculate the number of visits to create the two sub- arrays and then merge the two sorted arrays  3 visits to merge each element or 3N visits  2N visits to create the two sub-arrays  total of 5N visits Optional!

Analyzing the Merge Sort Algorithm (Cont’d) Let T(n) denote the number of visits to sort an array of n elements then  T(n) = T(n/2) + T(n/2) + 5n or  T(n) = 2T(n/2) + 5n The visits for an array of size n/2 is: T(n/2) = 2T(n/4) + 5n/2  So T(n) = 2 × 2T(n/4) +5n + 5n The visits for an array of size n/4 is: T(n/4) = 2T(n/8) + 5n/4  So T(n) = 2 × 2 × 2T(n/8) + 5n + 5n + 5n Optional!

Repeating the process k times: T(n) = 2 k T(n/2 k ) +5nk since N = 2 m, when k = m: T(n) = 2 m T(n/2 m ) +5Nm T(n) = N T(1) +5Nm T(n) = N + 5N log 2 (N) Analyzing the Merge Sort Algorithm (Cont’d) Optional!

To establish growth order:  Drop the lower-order term N  Drop the constant factor 5  Drop the base of the logarithm since all logarithms are related by a constant factor  We are left with N log(N) Analyzing the Merge Sort Algorithm (Cont’d) Optional!

Merge Sort Versus Selection Sort Selection sort is a quadratic algorithm, i.e., number of comparisons depends on N 2. In the merge sort algorithm, the number of comparisons depends on Nlog(N). The log(N) function grows much more slowly than N 2.

Sorting Using Java API The Arrays class (Package: java.util ) implements a sorting method To sort an array of integers That sort() method uses a sophisticated fast sorting algorithm (is it the merge or quick sort?) int[] a = {2, -5, 3, 0, 7, -11}; Arrays.sort(a);

The Quick Sort Algorithm Divide and conquer 1.Partition the range 2.Sort each partition

public void sort(int from, int to) { if (from >= to) return; int p = partition(from, to); sort(from, p); sort(p + 1, to); } The Quick Sort Algorithm (Cont’d)

Figure 4: Partitioning a Range The Quick Sort Algorithm (Cont’d)

Figure 5: Extending the Partitions The Quick Sort Algorithm (Cont’d)

Figure 6: Array list used with the quick sort algorithm The Quick Sort Algorithm (Cont’d) LR [0] [1] [9] list Quick Sort Steps 1.Select the pivot index = (L + R) / 2 2.Keep on the left partition all elements <= list[pivot] 3.Keep on the right partition all elements > list[pivot] Pivot element <= list[pivot]> list[pivot]

Figure 6: Array list used with the quick sort algorithm The Quick Sort Algorithm (Cont’d) LR [0] [1] [9] list Pivot element <= list[pivot] > list[pivot] -7 6 LRL 11-6 L 5 0 RLRLLL Partition Point! LRLR

private int partition(int from, int to) { int pivot = a[from]; int i = from - 1; int j = to + 1; while (i pivot) j--; if (i < j) swap(i, j); } return j; } The Quick Sort Algorithm (Cont’d)

Sorting Real Data Arrays.sort sorts objects of classes that implement Comparable interface The call a.compareTo(b) returns  A negative number is a should come before b  0 if a and b are the same  A positive number otherwise public interface Comparable { int compareTo(Object otherObject); }

Several classes in Java (e.g. String and Date ) implement Comparable You can implement Comparable interface for your own classes public class Coin implements Comparable {... public int compareTo(Object otherObject) { Coin other = (Coin) otherObject; if (value < other.value) return -1; if (value == other.value) return 0; return 1; }... } Sorting Real Data (Cont’d)

Once your class implements Comparable, simply use the Arrays.sort method: Coin[] coins = new Coin[n]; // Add coins... Arrays.sort(coins); Sorting Real Data (Cont’d)