CS2006 - Data Structures I Chapter 10 Algorithm Efficiency & Sorting III.

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.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
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.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Sorting Algorithms and Average Case Time Complexity
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
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.
1 Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort Determining Algorithm Efficiency Determining Algorithm Efficiency Substitution.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
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!
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
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 Algorithm Efficiency and Sorting (Walls & Mirrors - Remainder of Chapter 9)
Unit 281 Merge- and Quick Sort Merge Sort Quick Sort Exercises.
C++ Plus Data Structures
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.
CSCD 326 Data Structures I Sorting
CS202 - Fundamentals of Computer Science II
Sorting and Searching 7/2/2015CS202 - Fundamentals of Computer Science II1.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
Sorting Algorithms CENG 213 Data Structures.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
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.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Measuring the Efficiency of Algorithms Analysis of algorithms Provides tools for contrasting the efficiency of different methods of solution Time efficiency,
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 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
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.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
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.
Merge Sort. Stable vs. Non-Stable Sorts We frequently use sorting methods for items with multiple keys Sometimes we need to apply the sorting with different.
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.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
CENG 213 Data Structures Sorting Algorithms. CENG 213 Data Structures Sorting Sorting is a process that organizes a collection of data into either ascending.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Sorting and Searching 2/19/2016CS202 - Fundamentals of Computer Science II1.
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.
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.
CS 367 Introduction to Data Structures Lecture 11.
CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting II.
Figure 9.1 Time requirements as a function of the problem size n.
Algorithm Efficiency and Sorting
Sorting Algorithms CENG 213 Data Structures 1.
Sorting Algorithms CENG 213 Data Structures.
Week 12 - Wednesday CS221.
Algorithm Design Methods
Advanced Sorting Methods: Shellsort
Chapter 4.
CSE 373 Data Structures and Algorithms
Chapter 10 Sorting Algorithms
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Presentation transcript:

CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting III

2 Topics Sorting Merge Sort Quick Sort

3 Merge Sort - General Description Recursively divide list in half until each piece is of size one: then merge adjacent one element lists into a sorted two element list. each sorted part is merged with adjacent parts to give a larger sorted list. eventually the final two pieces are merged to give a sorted version of the original list.

4 Merge Sort Example: Divide array in two halves: Sort the halves: Merge the halves into temporary array: 1 < 2  Put 1 into Temporary array 1 2 < 4  Put 2 into TemArr < 4  Put 3 into TemArr Right half finished; add remaing elements Copy Temporary array back into original array

5 mergeSort -method public static void mergeSort(Comparable[ ] theArray, int first, int last) { // // Sorts the items in an array into ascending order. // Calls: merge. // if (first < last) { // sort each half int mid = (first + last)/2; // index of midpoint // sort left half theArray[first..mid] mergeSort(theArray, first, mid); // sort right half theArray[mid+1..last] mergeSort(theArray, mid+1, last); // merge the two halves merge(theArray, first, mid, last); } // end if } // end mergesort

6 merge - method private static void merge(Comparable[ ] theArray, int first, int mid, int last) { // // Merges two sorted array segments theArray[first..mid] and // theArray[mid+1..last] into one sorted array. // Implementation note: This method merges the two subarrays into a temporary array // and copies the result into the original array anArray. // int maxSize = theArray.length; // temporary array Comparable[ ] tempArray = new Comparable[maxSize]; // initialize the local indexes to indicate the subarrays int first1 = first; // beginning of first subarray int last1 = mid; // end of first subarray int first2 = mid + 1; // beginning of second subarray int last2 = last; // end of second subarray

7 merge - method (2) // while both subarrays are not empty, copy the // smaller item into the temporary array int index = first1; // next available location in tempArray while ((first1 <= last1) && (first2 <= last2)) { // Invariant: tempArray[first1..index-1] is in order if (theArray[first1].compareTo(theArray[first2])<0) { tempArray[index] = theArray[first1]; first1++; } else { tempArray[index] = theArray[first2]; first2++; } // end if index++; } // end while

8 merge - method (3) // finish off the nonempty subarray // finish off the first subarray, if necessary while (first1 <= last1) { // Invariant: tempArray[first1..index-1] is in order tempArray[index] = theArray[first1]; first1++; index++; } // end while // finish off the second subarray, if necessary while (first2 <= last2) { // Invariant: tempArray[first1..index-1] is in order tempArray[index] = theArray[first2]; first2++; index++; } // end while // copy the result back into the original array for (index = first; index <= last; ++index) { theArray[index] = tempArray[index]; } // end for } // end merge

9 mergeSort -driver public static void main(String[ ] args) { Integer[ ] values = new Integer[10]; values[0] = new Integer(9); values[1] = new Integer(15); values[2] = new Integer(13); values[3] = new Integer(20); values[4] = new Integer(5); values[5] = new Integer(0); values[6] = new Integer(7); values[7] = new Integer(10); values[8] = new Integer(3); values[9] = new Integer(2); mergeSort(values, 0, values.length -1); }

10 Merge Sort Analysis: O(n * log 2 n) Self study

11 Quicksort Overview A recursive sorting algorithm which divides an array into two smaller partitions. All small elements in one partition and large elements in another Calls itself recursively with each partition - a divide and conquer algorithm Requires use of an array since random access is necessary.

12 Quicksort Partitioning Scheme Choose the median element in the array as the "pivot" element (ideally). All elements smaller than the pivot go into the part of the array below the pivot. All elements larger than the pivot go into the part of the array above the pivot.

13 Quicksort Choosing the "pivot": Finding the true median element is more time consuming than quicksort itself so - Choose (guess) the pivot element quickly. Commonly used methods are to choose the first or the middle element as pivot. More on pivot choosing in the analysis section.

14 Quick Sort Choosing the pivot: < PP  P FPivot Index L S1 S2 P< P  P ? F Pivot L S1S2Unknown LastS1 FirstUnknown

15 Quick Sort Swapping elements:

16 Quick Sort Example: Original array First partition Pivot Unknown S2 S1 S2 Unknown Swap 16 & 38 Swap Pivot & 16

17 Quicksort Method public static void quicksort (Comparable[] theArray, int first, int last) { // // sorting the items in an array into ascending order // Precondition: theArray[first..last] is an array; first <= last. // Postcondition: theArray[first..last] is an sorted array. // int pivotIndex; if (first<last) { pivotIndex = partition (theArray, first, last); // sort region S1 and S2 quicksort(theArray, first, pivotIndex-1); quicksort(theArray, pivotIndex+1, last); } // end of if } //end quicksort

18 Partition Method private static int partition(Comparable[] theArray, int first, int last) { // // Partitions an array for quicksort. // Precondition: theArray[first..last] is an array; first <= last. // Postcondition: Returns the index of the pivot element of theArray [first..last]. // Upon completion of the method, this will be the index value lastS1 such that // S1 = theArray[first..lastS1-1] < pivot // theArray[lastS1] == pivot // S2 = theArray[lastS1+1..last] >= pivot // // tempItem is used to swap elements in the array Comparable tempItem; // place pivot in theArray[first] // choosePivot(theArray, first, last); Comparable pivot = theArray[first]; // reference pivot // initially, everything but pivot is in unknown int lastS1 = first; // index of last item in S1

19 Partition Method (2) // move one item at a time until unknown region is empty for (int firstUnknown = first + 1; firstUnknown <= last; ++firstUnknown) { // move item from unknown to proper region if (theArray[firstUnknown].compareTo(pivot) < 0) { // item from unknown belongs in S1 ++lastS1; tempItem = theArray[firstUnknown]; theArray[firstUnknown] = theArray[lastS1]; theArray[lastS1] = tempItem; } // end if // else item from unknown belongs in S2 } // end for // place pivot in proper position and mark its location tempItem = theArray[first]; theArray[first] = theArray[lastS1]; theArray[lastS1] = tempItem; return lastS1; } // end partition

20 Quicksort Analysis Best Case Assumptions: All array elements are in random order. We can choose a perfect median value. thus the array is cut exactly in half on each recursive call. Partition takes O(n) time since the loop runs from the second to the last element. for (int firstUnknown = first + 1; firstUnknown <= last; ++firstUnknown)

21 Quicksort Analysis (2) Determine the total number of calls to quickSort. A call to quicksort with an array partition size (n) of 1 (first == last) generates no further recursive calls. Each call to quicksort cuts the array size in half so partition deals with 1/2 the previous number of elements. n = 8 n = 4 n = 2 n = 1 n = 2

22 Quicksort Analysis (3) Find the growth rate function f(n): On each level of this tree the partition call(s) deal with a sum total of n elements. So the question becomes: how many levels are there in the tree? Since the number of elements is cut in half on each level there are log 2 n + 1 levels. So f(n) = n ( log 2 n + 1 ) = n log 2 n + n The time complexity of quickSort is O(nlog 2 n)

23 Quicksort Analysis (4) Worst case: ?

24 Quicksort Analysis (4) Worst case: data is already sorted. the first (rather than middle) element is chosen as the pivot. only one element (the first) gets partitioned off each call to partition has to deal with one less element and there are n recursive calls f(n) = n + (n-1) + (n-2) + (n-3) = n(n+1)/2 = n 2 /2 + n/2 The time complexity becomes O(n 2 ) which is the same as bubblesort or insertion sort.

25 Quicksort Analysis (4) Worst case: Insertion sort has a time complexity which approaches O(n) for nearly sorted (or small arrays) while quicksort approaches O(n 2 ) Thus, insertion sort is better than quicksort for small or nearly sorted arrays.

26 Review Each merge step of the mergesort requires ______ major operations. 3 * n – 1 4 * n – 1 (n – 1)/2 n – 1

27 Review The quicksort is ______ in the worst case. O(n 2 ) O(n 3 ) O(n * log 2 n) O(log 2 n)

28 Review A bubble sort requires at most ______ passes to sort an array of n items. n/2 n – 2 n – 1 n

29 Review Assuming a linked list of n nodes, the code fragment: Node curr = head; while (curr != null) { System.out.println(curr.getItem()); curr.setNext(curr.getNext()); } // end while requires ______ write operations. n n – 1 n + 1 1

30 Review An algorithm’s execution time is related to the number of ______ it requires. parameters test data sets data fields operations