6-1 CM0551 Week 6 The Array Data Structure Properties of arrays and subarrays – recap. Sorting: insertion, quick-sort and their time and space complexity.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CS4413 Divide-and-Conquer
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.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists
Sorting. “Sorting” When we just say “sorting,” we mean in ascending order (smallest to largest) The algorithms are trivial to modify if we want to sort.
Data Structures 5 Sorting Prof A Alkhorabi Overview Why Sorting? Simple Sorting – Selection & Insertion Sorts Sorting Performance Shell Sort Quicksort.
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.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
Faster Sorting Methods Chapter 9 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Introduction to Algorithms Chapter 7: Quick Sort.
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.
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.
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.
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.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting III 1 An Introduction to Sorting.
Quicksort.
Sorting Chapter 10.
Unit 061 Quick Sort csc326 Information Structures Spring 2009.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Quicksort
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
Quick Sort By: HMA. RECAP: Divide and Conquer Algorithms This term refers to recursive problem-solving strategies in which 2 cases are identified: A case.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
© 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.
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.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Divide and Conquer Strategy
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.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Quick Sort Modifications By Mr. Dave Clausen Updated for Python.
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Algorithm Efficiency and Sorting
Quicksort
Quicksort 1.
Advanced Sorting Methods: Shellsort
slides adapted from Marty Stepp
Quicksort.
CSE 373 Data Structures and Algorithms
3 The Array Data Structure
Algorithm Efficiency and Sorting
Quicksort.
CSE 332: Sorting II Spring 2016.
Algorithm Efficiency and Sorting
Quicksort.
Advanced Sorting Methods: Shellsort
Presentation transcript:

6-1 CM0551 Week 6 The Array Data Structure Properties of arrays and subarrays – recap. Sorting: insertion, quick-sort and their time and space complexity.

6-2 Java object array (1) Suppose that a Date object has fields y, m, d. Code to create an array of Date objects: Date[] hols = new Date[3]; length0 hols 3 Date[] class tag12

6-3 Java object array (2) Code to update the array of Date objects: hols[0] = new Date(2002, 1, 1); hols[1] = new Date(2001, 5, 1); hols[2] = new Date(2001, 12, 25); length 01 hols 3 Date[] class tag Date Date y md Date y md class tag y md

a Subarrays A subarray is a sequence of consecutive components that forms a part of a larger array. Notation: let a[l…r] be the subarray consisting of components a[l], …, a[r].  Subarray notation is used here, but not supported by Java. Length of subarray a[l…r] is r – l + 1. subarray a[6…9]subarray a[1…3]

6-5 Sorted arrays A (sub)array is sorted if its components are in ascending order, i.e., each component is less than or equal to the component on its right. The meaning of the comparison “x is less than y” (or “y is greater than x”) must be defined for each data type. Meaning of less for numbers: x is numerically less than y, i.e., x < y. Conventional meaning of less for strings: x precedes y lexicographically. E.g.: “bat” is less than “bath”, which is less than “bay”.

6-6 Sorting Problem: Given an unsorted array of data, rearrange the data into ascending order. This is important because sorted data can be searched and merged efficiently. Choice of algorithms:  selection sort (done in programming 2)  insertion sort  merge-sort (not covered here)  quick-sort  shell-sort, radix sort, etc. (not covered here).

6-7 Insertion sort (1) Idea: We can sort a file of values by successively reading each value and inserting it into its correct position in an array. Use the same idea to sort an array of values in place.

6-8 Insertion sort (2) Insertion sort algorithm: To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate.

6-9 Insertion sort (3) Animation: To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. fox left = = right cowpigcatratliontiger a goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. fox left = = right cowpigcatratliontiger 1 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cow left = = right foxpigcatratliontiger 1 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cow left = = right foxpigcatratliontiger 2 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cow left = = right foxpigcatratliontiger 2 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cow left = = right foxpigcatratliontiger 3 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cat left = = right cowfoxpigratliontiger 3 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cat left = = right cowfoxpigratliontiger 4 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cat left = = right cowfoxpigratliontiger 4 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cat left = = right cowfoxpigratliontiger 5 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cat left = = right cowfoxlionpigrattiger 5 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cat left = = right cowfoxlionpigrattiger 6 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cat left = = right cowfoxlionpigrattiger 6 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cat left = = right cowfoxlionpigrattiger 7 a r goatdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. 7 r 7 r cat left = = right cowfoxgoatlionpigrat a tigerdog 67 cat left = = right cowfoxgoatlionpigrat a tigerdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cat left = = right cowfoxgoatlionpigrat 8 a r tigerdog 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cat left = = right cowdogfoxgoatlionpig 8 a r rattiger 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cat left = = right cowdogfoxgoatlionpig 9 a r rattiger 67 To sort a[left…right] into ascending order: 1.For r = left+1, …, right, repeat: 1.1.Let val = a[r]. 1.2.Insert val into its correct sorted position in a[left…r]. 2.Terminate. cat left = = right cowdogfoxgoatlionpig a rattiger 67

6-10 Insertion sort (4) Analysis (counting comparisons): Let n = right – left + 1 be the length of the array. Step 1.2 performs between 1 and r – left comparisons, say (r – left + 1)/2 comparisons on average. This is repeated with r = left+1, left+2, …, right. Average no. of comparisons= 2/2 + 3/2 + … + n/2 = (n – 1)(n + 2)/4 = (n 2 + n – 2)/4 Time complexity is O(n 2 ). Think of this as visiting each array element and then visiting them all again to insert the element in the right position (that’s n x n) and hence it’s time complexity is of O(n 2 ).

6-11 Quick-sort (1) Idea: Choose any value from the array (called the pivot). Then partition the array into three subarrays such that:  the left subarray contains only values less than (or equal to) the pivot;  the middle subarray contains only the pivot;  the right subarray contains only values greater than (or equal to) the pivot. Finally sort the left subarray and the right subarray separately. This is another application of the divide-and-conquer strategy.

6-12 Quick-sort (2) Quick-sort algorithm: To sort a[left…right] into ascending order: 1.If left < right: 1.1.Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2.Sort a[left…p–1] into ascending order. 1.3.Sort a[p+1…right] into ascending order. 2.Terminate.

6-13 Quick-sort (Animation) (3) To sort a[left…right] into ascending order: 1.If left < right: 1.1.Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2.Sort a[left…p–1] into ascending order. 1.3.Sort a[p+1…right] into ascending order. 2.Terminate. fox left = = right cowpigcatratliontiger a goatdog 67 To sort a[left…right] into ascending order: 1.If left < right: 1.1.Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2.Sort a[left…p–1] into ascending order. 1.3.Sort a[p+1…right] into ascending order. 2.Terminate. fox left = = right cowpigcatratliontiger a goatdog 67 To sort a[left…right] into ascending order: 1.If left < right: 1.1.Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2.Sort a[left…p–1] into ascending order. 1.3.Sort a[p+1…right] into ascending order. 2.Terminate. cow left = = right catdogfoxpigratlion a tigergoat 67 3 p To sort a[left…right] into ascending order: 1.If left < right: 1.1.Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2.Sort a[left…p–1] into ascending order. 1.3.Sort a[p+1…right] into ascending order. 2.Terminate. cat left = = right cowdogfoxpigratlion a tigergoat 67 3 p To sort a[left…right] into ascending order: 1.If left < right: 1.1.Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2.Sort a[left…p–1] into ascending order. 1.3.Sort a[p+1…right] into ascending order. 2.Terminate. cat left = = right cowdogfoxgoatlionpig a rattiger 67 3 p To sort a[left…right] into ascending order: 1.If left < right: 1.1.Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2.Sort a[left…p–1] into ascending order. 1.3.Sort a[p+1…right] into ascending order. 2.Terminate. cat left = = right cowdogfoxgoatlionpig a rattiger 67

6-14 Quick-sort (4) Analysis (counting comparisons): Let n be the no. of values to be sorted. Step 1.1 takes about n–1 comparisons to partition the array. (NB: partition, not sort!)

6-15 Quick-sort (5) In the best case, the pivot turns out to be the middle value in the array. So the left and right subarrays both have length about n/2. So we have half the problem and we repeat this again … Divide and conquer again, so: Best-case time complexity is O(n log n).

6-16 Quick-sort (6) In the worst case, the pivot turns out to be the smallest value. So we only reduce the array by one each time just as in the case of insertion sort, so; Worst-case time complexity is O(n 2 ). The worst case arises if the array is already sorted !

6-17 Quick-sort (6) Implementation in Java: static void quickSort ( Comparable[] a, int left, int right) { // Sort a [ left … right ] into ascending order. if (left < right) { int p = partition(a, left, right); quickSort(a, left, p-1); quickSort(a, p+1, right); } }

6-18 Quick-sort (7) Partitioning algorithm: To partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]: 1.Let pivot be the value of a[left], and set p = left. 2.For r = left+1, …, right, repeat: 2.1.If a[r] is less than pivot: Copy a[r] into a[p], a[p+1] into a[r], and pivot into a[p+1] Increment p. 3.Terminate with answer p. Note that other (and better) partitioning algorithms exist.

6-19 Quick-sort (8) Implementation in Java: static int partition ( Comparable[] a, int left, int right) { // Partition a [ left … right ] such that // a [ left … p-1 ] are all less than or equal to a [ p ], and // a [ p+1 … right ] are all greater than or equal to a [ p ]. // Return p. Comparable pivot = a[left]; int p = left;

6-20 Quick-sort (9) Implementation (continued): for (int r = left+1; r <= right; r++) { int comp = a[r].compareTo(pivot); if (comp < 0) { a[p] = a[r]; a[r] = a[p+1]; a[p+1] = pivot; p++; } } return p; }

6-21 Comparison of sorting algorithms AlgorithmNo. of comparisons Time complexity Space complexity Selection sort~ n 2 /2O(n2)O(n2)O(1) Insertion sort~ n 2 /4O(n2)O(n2)O(1) Merge-sort~ n log 2 nO(n log n)O(n)O(n) Quick-sort~ n log 2 n ~ n 2 /2 O(n log n) O(n 2 ) O(log n) O(n) best worst