Sorting. Order in the court! sorting 1

Similar documents
Sorting. Order in the court! sorting 1

Sorting. Task Description. Selection Sort. Should we worry about speed?

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

Sorting and Searching

We can use a max-heap to sort data.

Sorting Algorithms Day 2 4/5/17

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Unit-2 Divide and conquer 2016

Chapter 7 Sorting. Terminology. Selection Sort

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

Sorting Algorithms. + Analysis of the Sorting Algorithms

Overview of Sorting Algorithms

SORTING. Comparison of Quadratic Sorts

Cpt S 122 Data Structures. Sorting

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

Faster Sorting Methods

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Lecture Notes on Quicksort

Mergesort again. 1. Split the list into two equal parts

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Sorting and Searching Algorithms

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

Active Learning: Sorting

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

10/21/ Linear Search The linearsearch Algorithm Binary Search The binarysearch Algorithm

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

Lecture Notes on Quicksort

BM267 - Introduction to Data Structures

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:

Searching and Sorting

Sorting & Searching (and a Tower)

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Quick Sort. CSE Data Structures May 15, 2002

Divide and Conquer Algorithms: Advanced Sorting

Mergesort again. 1. Split the list into two equal parts

Better sorting algorithms (Weiss chapter )

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

CS125 : Introduction to Computer Science. Lecture Notes #40 Advanced Sorting Analysis. c 2005, 2004 Jason Zych

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Chapter Contents. An Introduction to Sorting. Selection Sort. Selection Sort. Selection Sort. Iterative Selection Sort. Chapter 9

Lecture 7: Searching and Sorting Algorithms

Data Types. Operators, Assignment, Output and Return Statements

Data Structures and Algorithms

ITEC2620 Introduction to Data Structures

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

Sorting is a problem for which we can prove a non-trivial lower bound.

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Lecture 2: Divide&Conquer Paradigm, Merge sort and Quicksort

CSC 273 Data Structures

Divide and Conquer. Algorithm Fall Semester

CS 171: Introduction to Computer Science II. Quicksort

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

Lecture 8: Mergesort / Quicksort Steven Skiena

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

COSC 311: ALGORITHMS HW1: SORTING

Measuring algorithm efficiency

COMP Data Structures

Topic 17 Fast Sorting

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

CSE 373: Data Structures and Algorithms

Divide and Conquer Sorting Algorithms and Noncomparison-based

Programming II (CS300)

Intro to Algorithms. Professor Kevin Gold

Programming II (CS300)

L14 Quicksort and Performance Optimization

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 10

Sorting/Searching and File I/O. Sorting Searching Reading for this lecture: L&L

DATA STRUCTURES AND ALGORITHMS

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Sorting is ordering a list of objects. Here are some sorting algorithms

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Searching and Sorting Chapter 18. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

SORTING. How? Binary Search gives log(n) performance.

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Merge- and Quick Sort

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

algorithm evaluation Performance

Divide and Conquer. Algorithm D-and-C(n: input size)

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

The divide and conquer strategy has three basic parts. For a given problem of size n,

Data Structures And Algorithms

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

Transcription:

Sorting Order in the court! sorting 1

Importance of sorting Sorting a list of values is a fundamental task of computers - this task is one of the primary reasons why people use computers in the first place Many sorting algorithms have been developed over the history of computer science sorting 2

Quadratic sorting algorithms Algorithms with order of magnitude O(N 2 ) are known as quadratic algorithms Such algorithms are particularly sensitive to the size of the data set; sorting times increase geometrically as the size of the data set grows Their quadratic behavior is their chief disadvantage; however, they are easy to comprehend and code, and work reasonably well on relatively small data sets sorting 3

Quadratic sorting algorithms We will examine two examples of quadratic sorting algorithms: Selectionsort Insertionsort Each of these algorithms follows the same principle: sort a portion of the array, then progress through the unsorted portion, adding one element at a time to the sorted portion sorting 4

Selectionsort Goal of the algorithm is to sort a list of values (for example, integers in an array) from smallest to largest The method employed comes directly from this statement of the problem find smallest value and place at front of array find next-smallest value and place in second position find next-next-smallest and place in third position and so on... sorting 5

Selectionsort The mechanics of the algorithm are simple: swap the smallest element with whatever is in the first position, then move to the second position and perform a similar swap, etc. In the process, a sorted subarray grows from the front, while the remaining unsorted subarray shrinks toward the back sorting 6

Implementation of Selectionsort public void selectionsort (int data[]) { int mindex, tmp; for (int x = 0; x <= data.length-2; x++) { mindex = x; for (int y = x+1; y <= data.length-1; y++) if (data [y] < data[mindex]) mindex = y; tmp = data[x]; data[x] = data[mindex]; data[mindex] = tmp; } } sorting 7

Time Analysis of Selectionsort The driving element of the selectionsort function is the set of for loops, each of which is O(N) Both loops perform the same number of operations no matter what values are contained in the array (even if all are equal, for example) -- so worst-case, average-case and best-case are all O(N 2 ) sorting 8

Insertionsort Although based on the same principle as Selectionsort (sorting a portion of the array, adding one element at a time to the sorted portion), Insertionsort takes a slightly different approach Instead of selecting the smallest element from the unsorted side, Insertionsort simply takes the first element and inserts it in place on the sorted side so that the sorted side is always in order sorting 9

Insertionsort algorithm Designate first element as sorted Take first element from unsorted side and insert in correct location on sorted side: copy new element shift elements from end of sorted side to the right (as necessary) to make space for new element sorting 10

Insertionsort algorithm Correct location for new element found when: front of array is reached or next element to shift is <= new element Continue process until last element has been put into place sorting 11

Implementation of Insertionsort public void insertionsort (int [] array) { int x, y, tmp; for (x=1; x<array.length; x++) { tmp = array[x]; for (y=x; y>0 && array[y-1] > tmp; y--) array[y] = array[y-1]; array[y] = tmp; } } sorting 12

Time analysis of Insertionsort In the worst case and in the average case, Insertionsort s order of magnitude is O(N 2 ) But in the best case (when the starting array is already sorted), the algorithm is O(N) because the inner loop doesn t go If an array is nearly sorted, Insertionsort s performance is nearly linear sorting 13

Recursive sorting algorithms Recursive algorithms are considerably more efficient than quadratic algorithms The downside is they are harder to comprehend and thus harder to code Two examples of recursive algorithms that use a divide and conquer paradigm are Mergesort and Quicksort sorting2 14

Divide and Conquer sorting paradigm Divide elements to be sorting into two (nearly) equal groups Sort each of these smaller groups (by recursive calls) Combine the two sorted groups into one large sorted list sorting2 15

Using pointer arithmetic to specify subarrays The divide and conquer paradigm requires division of the element list (e.g. an array) into two halves Pointer arithmetic facilitates the splitting task for array with n elements, for any integer x from 0 to n, the expression (data+x) refers to the subarray that begins at data[x] in the subarray, (data+x)[0] is the same as data[x], (data+x)[1] is the same as data[x+1], etc. sorting2 16

Mergesort Straightforward implementation of divide and conquer approach Strategy: divide array at or near midpoint sort half-arrays via recursive calls merge the two halves sorting2 17

Mergesort Two functions will be implemented: mergesort: simpler of the two; divides the array and performs recursive calls, then calls merge function merge: uses dynamic array to merge the subarrays, then copies result to original array sorting2 18

Mergesort in action 40 22 13 8 51 29 36 11 Original array 40 22 13 8 First 51 29 36 11 split 40 22 13 8 Second 51 29 36 11 split 40 22 13 8 Third 51 29 36 11 split Stopping case is reached for each array when array size is 1; once recursive calls have returned, merge function is called sorting2 19

Mergesort method public static void mergesort (int data[], int first, int n) { int n1; int n2; if (n>1) { n1 = n/2; n2 = n-n1; mergesort(data, first, n1); mergesort(data, first+n1, n2); merge(data, first, n1, n2); } } sorting2 20

Mergesort method public static void mergesort (int data[], int first, int n) { int n1; int n2; if (n>1) { n1 = n/2; n2 = n-n1; mergesort(data, first, n1); mergesort(data, first+n1, n2); merge(data, first, n1, n2); } } sorting2 21

Merge method Uses temporary array (temp) that copies items from data array so that items in temp are sorted Before returning, the method copies the sorted items back into data When merge is called, the two halves of data are already sorted, but not necessarily with respect to each other sorting2 22

Merge method in action Two subarrays are assembled into a merged, sorted array with each call to the merge function; as each recursive call returns, a larger array is assembled 40 22 13 8 51 29 36 11 22 40 8 13 29 51 11 36 8 13 22 40 11 29 36 51 8 11 13 22 29 36 40 51 sorting2 23

How merge works As shown in the previous example, the first call to merge deals with several half-arrays of size 1; merge creates a temporary array large enough to hold all elements of both halves, and arranges these elements in order Each subsequent call performs a similar operation on progressively larger, presorted half-arrays sorting2 24

How merge works Each instance of merge contains the dynamic array temp, and three counters: one that keeps track of the total number of elements copied to temp, and one each to track the number of elements copied from each halfarray The counters are used as indexes to the three arrays: the first half-array (data), the second half-array (data + n1) and the dynamic array (temp) sorting2 25

How merge works Elements are copied one-by-one from the two half-arrays, with the smallest elements from both copied first When all elements of either half-array have been copied, the remaining elements of the other halfarray are copied to the end of temp Because the half-arrays are pre-sorted, these leftover elements will be the largest values in each half-array sorting2 26

The copying procedure from merge (pseudocode) while (both half-arrays have more elements to copy) { if (next element from first <= next element from second) { copy element from first half to next spot in temp add 1 to temp and first half counters } else { copy element from second half to next spot in temp add 1 to temp and second half counters } } sorting2 27

First refinement // temp counter = c, first counter = c1, second counter = c2 while (both 1st & 2nd half-arrays have more elements to copy) { if (data[c1] <= (data + n1)[c2]) { temp[c] = data[c1]; c++; c1++; } else { temp[c] = (data + n1)[c2]); c++; c2++; } } sorting2 28

Final version -- whole algorithm in pseudocode Initialize counters to zero while (more to copy from both sides) { if (data[c1] <= (data+n1)[c2]) temp[c++] = data[c1++]; else temp[c++] = (data+n1)[c2++]; } Copy leftover elements from either 1st or 2nd half-array Copy elements from temp back to data sorting2 29

Code for merge public static void merge (int [] data, int first, int n1, int n2) { int [] temp = new int[n1 + n2]; int copied = 0, copied1 = 0, copied2 = 0, i; while ((copied1 < n1) && (copied2 < n2)) { if (data[first + copied1] < data[first + n1 + copied2]) temp[copied++] = data[first + (copied1++)]; else temp[copied++] = data[first + n1 + (copied2++)]; } sorting2 30

Code for merge while (copied1 < n1) temp[copied++] = data[first + (copied1++)]; while (copied2 < n2) temp[copied++] = data[first + n1 + (copied2++)]; } for (i=0; i < n1+n2; i++) data[first+i] = temp[i]; sorting2 31

Time analysis of mergesort Start with array of N elements At top level, 2 recursive calls are made to sort subarrays of N/2 elements, ending up with one call to merge At the next level, each N/2 subarray is split into two N/4 subarrays, and 2 calls to merge are made At the next level, 4 N/8 subarrays are produces, and 4 calls to merge are made and so on... sorting2 32

Time analysis of mergesort The pattern continues until no further subdivisions can be made Total work done by merging is O(N) (for progressively smaller sizes of N at each level) So total cost of mergesort may be presented by the formula: (some constant) * N * (number of levels) sorting2 33

Time analysis of mergesort Since the size of the array fragments is halved at each step, the total number of levels is approximately equal to the number of times N can be divided by two Given this, we can refine the formula: (some constant) * N * log 2 N Since the big-o form ignores constants, we can state that mergesort s order of magnitude is O(N log N) in the worst case sorting2 34

Comparing O(N 2 ) to O(N log N) N N log N N 2 2 2 4 8 24 64 32 160 1024 128 896 16,384 512 4,608 262,144 2,048 22,528 4,194,304 sorting2 35

Application of Mergesort Although more efficient than a quadratic algorithm, mergesort is not the best choice for sorting arrays because of the need for a temporary dynamic array in the merge step The algorithm is often used for sorting files With each call to mergesort, the file is split into smaller and smaller pieces; once a piece is small enough to fit into an array, a more efficient sorting algorithm can be applied to it sorting2 36

Quicksort Similar to Mergesort in its use of divide and conquer paradigm In Mergesort, the divide part was trivial; the array was simply halved. The complicated part was the merge In Quicksort, the opposite is true; combining the sorted pieces is trivial, but Quicksort uses a sophisticated division algorithm sorting2 37

Quicksort Select an element that belongs in the middle of the array (called the pivot), and place it at the midpoint Place all values less than the pivot before the pivot, and all elements greater than the pivot after the pivot sorting2 38

Quicksort At this point, the array is not sorted, but it is closer to being sorted -- the pivot is in the correct position, and all other elements are in the correct array segment Because the two segments are placed correctly with respect to the pivot, we know that no element needs to be moved from one to the other sorting2 39

Quicksort Since we now have the two segments to sort, can perform recursive calls on these segments Choice of pivot element important to efficiency of algorithm; unfortunately, there is no obvious way to do this For now, we will arbitrarily pick a value to use as pivot sorting2 40

Code for Quicksort public static void quicksort (int data[ ], int first, int n) { int pivotindex; // array index of pivot int n1, n2; // number of elements before & after pivot if (n > 1) { pivotindex = partition (data, first, n); n1 = pivotindex - first; n2 = n - n1-1; quicksort (data, first, n1); quicksort (data, pivotindex+1, n2); } } sorting2 41

Partition method Moves all values <= pivot toward first half of array Moves all values > pivot toward second half of array Pivot element belongs at boundary of the two resulting array segments sorting2 42

Partition method Starting at beginning of array, algorithm skips over elements smaller than pivot until it encounters element larger than pivot; this index is saved (too_big_index) Starting at the end of the array, the algorithm similarly seeks an element on this side that is smaller than pivot -- the index is saved (too_small_index) sorting2 43

Partition method Swapping these two elements will place them in the correct array segments The technique of locating and swapping incorrectly placed elements at the the two ends of the array continues until the two segments meet This point is reached when the two indexes (too_big and too_small) cross -- in other words, too_small_index < too_big_index sorting2 44

Partition method: pseudocode Initialize values for pivot, indexes Repeat the following until the indexes cross: while (too_big_index < n && data[too_big_index} <= pivot) too_big_index ++ while (data[too_small_index] > pivot) too_small_index -- if (too_big_index < too_small_index) // both segments still have room to grow swap (data[too_big_index], data[too_small_index]) sorting2 45

Partition method: pseudocode Finally, move the pivot element to its correct position: pivot_index = too_small_index; swap (data[0], data[pivot_index]); sorting2 46

Time analysis of Quicksort In the worst case, Quicksort is quadratic (O(N 2 )) But Quicksort is O(N log N) in both the best and the average cases Thus, Quicksort compares well with Mergesort and has the advantage of not needing extra memory allocated sorting2 47

Choosing a good pivot element The worst case of Quicksort comes about if the element chosen for pivot is at either extreme - in other words, the smallest or largest element -- this will occur if the array is already sorted! To decrease the likelihood of this occurring, pick three values at random from the array, and choose the middle of the three as the pivot sorting2 48