CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

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.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 1 Class 15 - Recursive sorting methods r Processing arrays by recursion r Divide-and-conquer.
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
1 Today’s Material Divide & Conquer (Recursive) Sorting Algorithms –QuickSort External Sorting.
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
CMPS1371 Introduction to Computing for Engineers SORTING.
CSE 1302 Lecture 22 Quick Sort and Merge Sort Richard Gesick.
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.
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.
Sorting Algorithms: Merge and Quick. Lecture Objectives Learn how to implement the simple advanced sorting algorithms (merge and quick) Learn how to implement.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
Sorting21 Recursive sorting algorithms Oh no, not again!
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
1 TCSS 342, Winter 2005 Lecture Notes Sorting Weiss Ch. 8, pp
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
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.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 14 – Sorting and Searching.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
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.
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Recursion A recursive computation solves a problem by using the solution of the same problem with simpler values The same computation occurs repeatedly.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 14 – Sorting and Searching.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Sorting and Searching.
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.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
Sort Algorithms.
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.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Big-O and Sorting February 6, Administrative Stuff Readings for today: Ch Readings for tomorrow: Ch 8.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 146: Data Structures and Algorithms July 9 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.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
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)
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.
CS 367 Introduction to Data Structures Lecture 11.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
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, ]
Sorting Chapter 14.
Sorting Mr. Jacobs.
Chapter 7 Sorting Spring 14
CMPE Data Structures and Algorithms in C++ November 9 Class Meeting
CMPE 180A Data Structures and Algorithms in C++ April 19 Class Meeting
slides adapted from Marty Stepp
Chapter 14 – Sorting and Searching
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Workshop for CS-AP Teachers
CS 144 Advanced C++ Programming May 9 Class Meeting
Presentation transcript:

CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Quizzes for July 7  Quiz 12 July

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak This Friday’s Lab  School is closed on Friday, July 3 for holiday.  Therefore, do the following labs on your own: Lab 2 Lab 10  See the lab instructor Thong Nguyen’s message in Canvas. 3

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Selection Sort  Find the smallest and swap it with the first element.  Find the next smallest. It is already in the correct place.  Find the next smallest and swap it with first element of unsorted portion.  Repeat.  When the unsorted portion is of length 1, we are done. 4

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Selection Sort Performance  How long does it take the selection sort algorithm to sort an array of n elements? 5 Doubling n increases the sorting time about four times.

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Selection Sort Performance, cont’d  The number of visits is of the order n 2  The number of visits is “order n 2 ”: O ( n 2 )  Multiplying the number of elements in an array by 2 multiplies the processing time by 4 6

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Merge Sort  To sort an array recursively:  Base case An array with only one element: Do nothing.  A simpler but similar case: A smaller array, such as one that is half the size.  After sorting both halves, merge them back together as the whole sorted array.  Dramatically faster than selection sort! 7

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Merge Sort, cont’d  Divide the array into two halves and sort each half.  Merge the two sorted halves into a single sorted array. 8

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Merge Sort, cont’d 9 public static void sort(int[] a) { if (a.length <= 1) return; int[] first = new int[a.length/2]; int[] second = new int[a.length - first.length]; for (int i = 0; i < first.length; i++) { first[i] = a[i]; } for (int i = 0; i < second.length; i++) { second[i] = a[first.length + i]; } sort(first); sort(second); merge(first, second, a); } Base case First half of the array. Second half. Recursively sort each half. Merge the sorted halves back into a single sorted array.

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Merge Sort, cont’d 10 private static void merge(int[] first, int[] second, int[] a) { int iFirst = 0; // Next element to consider in the first array int iSecond = 0; // Next element to consider in the second array int j = 0; // Next open position in a while (iFirst < first.length && iSecond < second.length) { if (first[iFirst] < second[iSecond]) { a[j] = first[iFirst]; iFirst++; } else { a[j] = second[iSecond]; iSecond++; } j++; }... Merge an element from the first array. Merge an element from the second array.

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Merge Sort, cont’d while (iFirst < first.length) { a[j] = first[iFirst]; iFirst++; j++; } while (iSecond < second.length) { a[j] = second[iSecond]; iSecond++; j++; } } Merge remaining elements from the first array. Merge remaining elements from the second array. Only one of these two while loops will execute.

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Clicker Question July 2 #1  Look at the first while loop of the merge() method. Which of the following is true when the loop is complete? a. All elements of first have been added to array a b. All elements of second have been added to array a c. All elements of first or second have been added to array a d. Both first and second contain some elements that haven't been added to array a 12

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Merge Sort, cont’d 13 public class MergeSortDemo { public static void main(String[] args) { int[] a = ArrayUtil.randomIntArray(20, 100); System.out.println(Arrays.toString(a)); MergeSorter.sort(a); System.out.println(Arrays.toString(a)); } Demo

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Merge Sort vs. Selection Sort  Recall that the time it takes to sort n elements using selection sort is O(n 2 ).  We can prove that the time it takes to sort n elements using merge sort is O(n log 2 n).  n log 2 n grows much more slowly than n 2.  For large arrays, you want to do merge sort rather than selection sort. 14

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Merge Sort vs. Selection Sort, cont’d 15

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Proof that Merge Sort is O ( n log 2 n )  Assume that the array size n is a power of 2:  How many visits? 2 n : to create the two subarrays 3 n : 3 to merge each element 5 n total  Therefore: 16

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Proof that Merge Sort is O(n log 2 n), cont’d  Evaluate T(n/2) :  Substitute into (a):  Evaluate T(n/4) :  Substitute into (b): 17 (a) (b)

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Proof that Merge Sort is O(n log 2 n), cont’d  In general for arbitrary powers of 2:  We assumed that so for k = m, Because, 18

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Proof that Merge Sort is O(n log 2 n), cont’d 19  O(n log 2 n)

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Break 20

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Quicksort  Divide and conquer, like merge sort.  Better than merge sort, because there is no copying into temporary arrays to sort and then merge. 21

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Quicksort, cont’d  First partition the range:  Then recursively sort each partition. Sort in place – no temporary arrays needed. 22

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Quicksort, cont’d 23 public static void sort(int[] a, int from, int to) { if (from >= to) return; // base case int p = partition(a, from, to); sort(a, from, p); sort(a, p+1, to); }

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Quicksort Partitioning Strategy  The basic idea of partitioning a range:  Pick a pivot in the range.  Partition the range into groups: numbers less than the pivot value numbers greater than the pivot value 24

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Quicksort Partitioning Strategy, cont’d  Once we’ve picked a pivot within a range: We must push values in the range that are less than the pivot to the left. We must push values in the range that are greater than the pivot to the right.  The pivot value then will have (unsorted) lesser values before it, and (unsorted) greater values after it.  Insight: The pivot will already be in its proper position, so sorting operations will not move it. 25

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak 26 Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak How to Partition  Given a range, pick an element to be the pivot. Various strategies: The simplest is to pick the first element of the range to be the pivot.  Create two index variables, i and j. i starts at the left end of the range. j starts at the right end of the range. 27

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak How to Partition  Keep incrementing i while a[i] < pivot  Keep decrementing j while a[j] > pivot 28

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak How to Partition, cont’d  When both i and j can’t go any further: a[i] > pivot a[j] < pivot  Swap a[i] and a[j] 29

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak How to Partition, cont’d  Resume moving i and j towards each other as long as i < j  When you’re done moving i and j, the range will be partitioned. 30

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak How to Partition, cont’d 31 private static int partition(int[] a, int from, int to) { int pivot = a[from]; int i = from - 1; int j = to + 1; while (i < j) { i++; while (a[i] < pivot) { i++; } j--; while (a[j] > pivot) { j--; } if (i < j) { ArrayUtil.swap(a, i, j); } } return j; } Move i to the right. Move j to the left. Swap. Index of the pivot. Demo

Computer Science Dept. Summer 2015: July 2 CS 46B: Introduction to Data Structures © R. Mak Sorting Animations  omparisonSort.html omparisonSort.html 