CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Analysis of Algorithms
CSE 3101: Introduction to the Design and Analysis of Algorithms
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
MS 101: Algorithms Instructor Neelima Gupta
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Internal Sorting A brief review and some new ideas CS 400/600 – Data Structures.
CMPS1371 Introduction to Computing for Engineers SORTING.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
Data Structures and Algorithms PLSD210 Sorting. Card players all know how to sort … First card is already sorted With all the rest, ¶Scan back from the.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
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.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
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.
1 Algorithm Efficiency and Sorting (Walls & Mirrors - Remainder of Chapter 9)
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
Quicksort.
Quicksort.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
CSE 373 Data Structures Lecture 19
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
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.
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.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
CSC 211 Data Structures Lecture 13
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.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
Sort Algorithms.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
CS 61B Data Structures and Programming Methodology Aug 4, 2008 David Sun.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
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.
CS 367 Introduction to Data Structures Lecture 11.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Sorting.
Data Structures and Algorithms
Algorithm design and Analysis
8/04/2009 Many thanks to David Sun for some of the included slides!
Linear Sorting Sorting in O(n) Jeff Chastine.
CSE 326: Data Structures Sorting
Data Structures & Algorithms
The Selection Problem.
CSE 332: Sorting II Spring 2016.
Presentation transcript:

CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun

Announcements Midterm II on Wed 7/31, 11:00am – 12:30pm – In class open book. No computational devices allowed. – Covers everything up and including today’s lecture. Revision class tomorrow. – 45 minute mini-mock exam. Project 3 will be out later today. – Check newsgroup and course website. – Option to work in pairs or groups of three. It’s your responsibility to stay up-to-date with the newsgroup!

Space Complexity Not-in-place version: – Partitioning uses an additional Theta(n) storage space (best case) andTheta(n 2 ) (worst case). To partition in-place: – Given an array a and sort all elements between l(eft) and r(ight). – Choose a pivot v and swap with a[r]. – Initialize i to l - 1, and j to r so i and j sandwich the items to be sorted (not including the pivot). – Enforce the following invariants. All items at or left of index i have a key <= the pivot's key. All items at or right of index j have a key >= the pivot's key.

Quicksort on Arrays (continued) – Advance i to the first a[i] greater than or equal to the pivot. – Decrement j until the first a[j] less than or equal to the pivot. – a[i] and a[j] are on the wrong side of the partition, so swap a[i] and a[j]. – Repeat until the indices i and j meet in the middle. – Move the pivot back into the middle – swapping the last item with a[i].

public static void quicksort(Comparable[] a, int left, int right) { // If there's fewer than two items, do nothing. if (left < right) { int pivotIndex = random number from left to right; //Swap pivot with last item Comparable pivot = a[pivotIndex]; a[pivotIndex] = a[right]; a[right] = pivot; int i = left - 1; int j = right; do { do { i++; } while (a[i].compareTo(pivot) < 0); do { j--; } while ((a[j].compareTo(pivot) > 0) && (j > left)); if (i< j) { swap a[i] and a[j]; } } while (i< j); a[right] = a[i]; a[i] = pivot; // Put pivot in the middle where it belongs quicksort(a, left, i - 1); // Recursively sort left list quicksort(a, i + 1, right); // Recursively sort right list }

Some Details Can the " do { i++ } " loop walk off the end of the array and generate an out-of- bounds exception? – No, because a[right] contains the pivot, so i will stop advancing when i == right (if not sooner). There is no such assurance for j, though, so the " do { j-- } " loop explicitly tests whether "j > left" before decrementing.

Comparison of O(n log n) Algorithms Worst-caseSpaceComments QuicksortTheta(n 2 )Theta(logn) (in- place partitioning) HeapsortTheta(nlogn)Theta(1)Heapsort requires efficient random access. MergesortTheta(nlogn)Theta(n) (on arrays) Works well with linked lists (eg.. disk storage).

Selection The selection problem: we want to find the kth smallest key in a list, i.e. what’s the kth item in the list when it’s in sorted order? One approach: sort the list, then look up the item at index k. But what if we don’t care if the rest of the list is in sorted order or not, is there a faster way?

Quickselct In quicksort observe that: – after the partitioning step, we have three lists: L1, Iv, and L2. – We know which of the three lists contains index j, because we know the lengths of I1 and I2. – Therefore, we only need to search one of the three lists.

Quickselect Start with an unsorted list I of n input items. Choose a pivot item v from I. Partition I into three unsorted lists I1, Iv, and I2. I1 contains all items whose keys are smaller than v's key. I2 contains all items whose keys are larger than v's. Iv contains the pivot v. if (j < |I1|) { Recursively find the item with index j in I1; return it. } else if (j < |I1| + |Iv|) { Return the pivot v. } else { // j >= |I1| + |Iv|. Recursively find the item with index j - |I1| - |Iv| in I2; return it. }

Comparison Sort All the sorting algorithms we’ve seen so far are comparison sorts: – the ordering of the elements can be determined by comparison of their keys. – All actions taken by the sorting algorithm are based on the results of a sequence of true/false questions (a two way decision). If all you can do is comparison, then it can be proven that you can do no better than Ω (n log n) in the worst case to sort n elements – In this sense, merge sort and heapsort are asymptotically optimal.

Lower Bound of Comparison Sort Given a random scrambling of n numbers in an array, with each number from 1...n occurring once. How many possible orders (or permutations) can the numbers be in? – The answer is n!, where n! = 1 x 2 x 3 x... x (n-2) x (n-1) x n. – Observe that if n > 0, n! = 1 x 2 x... x (n-1) x n = n/2 x (n/2 + 1) x... x (n-1) x n >= (n/2) (n/2) – So (n/2) (n/2) <= n! <= n n. – Let's look at the logarithms of both these numbers: log((n/2) (n/2 ) = (n/2) log (n/2), which is in Theta(n log n), and log(n n ) = n log n. – Hence, log(n!) is also in Theta(n log n).

Lower Bound of Comparison Sort Given n! of the input, a correct sorting algorithm must do n! different permutations of comparisons and swap operations. Therefore, there must be n! possible permutations of true/false answers for the n! permutations of inputs. If a sorting algorithm never asks more than k true/false questions, it generates less than or equal to 2 k different sequences of true/false answers. – If it correctly sorts every permutation of 1...n, then n! <= 2 k, so log 2 (n!) <= k, and k is in Ω(n log n).

Linear Sorting Algorithms But suppose can do more than comparing keys. What if we know something about the keys that are being sorted: – For example, how can we sort a set of n integer keys whose values range from 0 to kn, for some small constant k? One technique: for each element x, determine the number of elements less than x. We can place x immediately into its right position. – If M p = #items with value < p, then in sorted order, the jth item with value p must be #M p + j. Gives linear-time algorithm.

Counting Sort Suppose all items are between 0 and 9: “Counts” line gives # occurrences of each key. “Running sum” gives cumulative count of keys each value which tells us where to put each key: – The first instance of key k goes into slot m, where m is the number of key instances that are < k.

Running Time of Counting Sort Theta(n + k) where n is the size of the input and k is the length of the counting array. – In order for this algorithm to be efficient, k must not be much larger than n. The indices of the counting array must run from the minimum to the maximum value in the input to be able to index directly with the values of the input. Otherwise, the values of the input will need to be translated (shifted), so that the minimum value of the input matches the smallest index of the counting array

Bucket Sort Again, uses the fact that – the keys are distributed with in some small range of values. e.g. from 0 to q-1, and – the number of items n is larger than, or nearly as large as, q, i.e. q is in O(n). Allocate an array of q queues, numbered from 0 to q-1, called buckets. We walk through the list of input items, and enqueue each item in the appropriate queue: – an item with key i goes into queue i. When we're done, we concatenate all the queues together in order.

Running Time of Bucket Sort Theta(q + n) time - in the best case and in the worst case. – It takes Theta(q) time to initialize the buckets in the beginning – It takes Theta(q) to concatenate the queues in the buckets together in the end. – It takes Theta(n) time to put all the items in their buckets. If q is in O(n) - that is, the number of possible keys isn't much larger than the number of items we're sorting - then bucket sort takes Theta(n) time.

Stability of Sorting A sort is stable if items with equal keys come out in the same order they went in. Bucket sort is a stable sort. Previously seen sorting algorithms: insertion sort, selection sort, and mergesort can all be implemented so that they are stable. The linked list version of quicksort we have seen can be stable, but the array version is decidedly not. Heapsort is never stable.