Quick Sort. As the name implies, it is quick, and it is the algorithm generally preferred for sorting. Quick Sort 1

Similar documents
Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Biostatistics 615/815

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Algorithms. Margaret M. Fleck. 18 October 2010

APP INVENTOR. Test Review

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

6. Standard Algorithms

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

Converting a Number from Decimal to Binary

For example, we have seen that a list may be searched more efficiently if it is sorted.

Analysis of Binary Search algorithm and Selection Sort algorithm

Class : MAC 286. Data Structure. Research Paper on Sorting Algorithms

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

CS473 - Algorithms I

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Randomized algorithms

Binary Heap Algorithms

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Closest Pair Problem

Data Structures. Algorithm Performance and Big O Analysis

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Section IV.1: Recursive Algorithms and Recursion Trees

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

Quick Sort. Implementation

Binary search algorithm

Chapter 7: Sequential Data Structures

CS473 - Algorithms I

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Row Echelon Form and Reduced Row Echelon Form

6 March Array Implementation of Binary Trees

28 Closest-Point Problems

recursion, O(n), linked lists 6/14

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Heaps & Priority Queues in the C++ STL 2-3 Trees

csci 210: Data Structures Recursion

TREE BASIC TERMINOLOGIES

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Computer Science 210: Data Structures. Searching

Lecture Notes on Linear Search

Loop Invariants and Binary Search

DESIGN AND ANALYSIS OF ALGORITHMS

IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE OPERATORS

A COMPARATIVE STUDY OF LINKED LIST SORTING ALGORITHMS

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

The Running Time of Programs

Pseudo code Tutorial and Exercises Teacher s Version

Data Structures and Data Manipulation

Zabin Visram Room CS115 CS126 Searching. Binary Search

Lecture 3: Finding integer solutions to systems of linear equations

Data storage Tree indexes

Sorting Algorithms. rules of the game shellsort mergesort quicksort animations. Reference: Algorithms in Java, Chapters 6-8

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

From Last Time: Remove (Delete) Operation

THIS CHAPTER studies several important methods for sorting lists, both contiguous

Sequential Data Structures

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

Linear Programming. March 14, 2014

Lecture 12 Doubly Linked Lists (with Recursion)

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

8 Primes and Modular Arithmetic

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Sample Questions Csci 1112 A. Bellaachia

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

HW4: Merge Sort. 1 Assignment Goal. 2 Description of the Merging Problem. Course: ENEE759K/CMSC751 Title:

7 Gaussian Elimination and LU Factorization

Exam study sheet for CS2711. List of topics

Dynamic Programming Problem Set Partial Solution CMPSC 465

Kenken For Teachers. Tom Davis June 27, Abstract

Node-Based Structures Linked Lists: Implementation

4.2 Sorting and Searching

Performance Tuning for the Teradata Database

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

3 Algorithms and Data Structures

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

Calculating interest rates

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Recursion vs. Iteration Eliminating Recursion

Dynamic Programming. Lecture Overview Introduction

Data Structures. Topic #12

Data Structures Fibonacci Heaps, Amortized Analysis

Lecture Notes on Binary Search Trees

Analysis of Micromouse Maze Solving Algorithms

Searching & Sorting. Contents. I. Searching 6

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Cpt S 223. School of EECS, WSU

from Recursion to Iteration

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

SQL Query Evaluation. Winter Lecture 23

MS SQL Performance (Tuning) Best Practices:

Chapter 13: Query Processing. Basic Steps in Query Processing

Transcription:

Quick Sort As the name implies, it is quick, and it is the algorithm generally preferred for sorting. Quick Sort 1

Basic Ideas (Another divide-and-conquer algorithm) Pick an element, say P (the pivot) Re-arrange the elements into 3 sub-blocks, 1. those less than or equal to ( ) P (the left-block S 1 ) 2. P (the only element in the middle-block) 3. those greater than or equal to ( ) P (the rightblock S 2 ) Repeat the process recursively for the left- and right- sub-blocks. Return {quicksort(s 1 ), P, quicksort(s 2 )}. (That is the results of quicksort(s 1 ), followed by P, followed by the results of quicksort(s 2 )) Quick Sort 2

Basic Ideas Pick a Pivot value, P Create 2 new sets without P Items smaller than or equal to P Items greater than or equal to P 0 31 13 43 26 57 65 75 92 81 quicksort(s 1 ) quciksort(s 2 ) 0 13 26 31 43 57 65 75 81 92 0 13 26 31 43 57 65 75 81 92 Quick Sort 3

Basic Ideas S is a set of numbers S 1 = {x S {P} x P} P S 2 = { x S {P} P x} Quick Sort 4

Note: Basic Ideas The main idea is to find the right position for the pivot element P. After each pass, the pivot element, P, should be in place. Eventually, the elements are sorted since each pass puts at least one element (i.e., P) into its final position. Issues: How to choose the pivot P? How to partition the block into sub-blocks? Quick Sort 5

Implementation Algorithm I: int partition(int A[ ], int left, int right); // sort A[left..right] void quicksort(int A[ ], int left, int right) { int q ; if (right > left ) { q = partition(a, left, right); // after partition // A[left..q-1] A[q] A[q+1..right] quicksort(a, left, q-1); quicksort(a, q+1, right); } } Quick Sort 6

Implementation // select A[left] be the pivot element) int partition(int A[], int left, int right); { P = A[left]; i = left; j = right + 1; for(;;) //infinite for-loop, break to exit { while (A[++i] < P) if (i >= right) break; // Now, A[i] P while (A[--j] > P) if (j <= left) break; // Now, A[j] P if (i >= j ) break; // break the for-loop else swap(a[i], A[j]); } if (j == left) return j ; swap(a[left], A[j]); return j; } Quick Sort 7

Example Input: 65 70 75 80 85 60 55 50 45 P: 65 i Pass 1: 65 70 75 80 85 60 55 50 45 (i) i j swap (A[i], A[j]) 65 45 75 80 85 60 55 50 70 (ii) i j swap (A[i], A[j]) 65 45 50 80 85 60 55 75 70 (iii) i j swap (A[i], A[j]) 65 45 50 55 85 60 80 75 70 (iv) i j swap (A[i], A[j]) 65 45 50 55 60 85 80 75 70 (v) j i if (i>=j) break 60 45 50 55 65 85 80 75 70 swap (A[left], A[j]) Items smaller than or equal to 65 Items greater than or equal to 65 Quick Sort 8

Example Result of Pass 1: 3 sub-blocks: 60 45 50 55 65 85 80 75 70 Pass 2a (left sub-block): 60 45 50 55 (P = 60) i j 60 45 50 55 j i if (i>=j) break 55 45 50 60 swap (A[left], A[j]) Pass 2b (right sub-block): 85 80 75 70 (P = 85) i j 85 80 75 70 j i if (i>=j) break 70 80 75 85 swap (A[left], A[j]) Quick Sort 9

Running time analysis The advantage of this quicksort is that we can sort in-place, i.e., without the need for a temporary buffer depending on the size of the inputs. (cf. mergesort) Partitioning Step: Time Complexity is θ(n). Recall that quicksort involves partitioning, and 2 recursive calls. Thus, giving the basic quicksort relation: T(n) = θ(n) + T(i) + T(n-i-1) = cn+ T(i) + T(n-i-1) where i is the size of the first sub-block after partitioning. We shall take T(0) = T(1) = 1 as the initial conditions. To find the solution for this relation, we ll consider three cases: 1. The Worst-case (?) 2. The Best-case (?) 3. The Average-case (?) All depends on the value of the pivot!! Quick Sort 10

Running time analysis Worst-Case (Data is sorted already) When the pivot is the smallest (or largest) element at partitioning on a block of size n, the result yields one empty sub-block, one element (pivot) in the correct place and one sub-block of size (n-1) takes θ(n) times. Recurrence Equation: T(1) = 1 T(n) = T(n-1) + cn Solution: θ(n 2 ) Worse than Mergesort!!! Quick Sort 11

Best case: Running time analysis The pivot is in the middle (median) (at each partition step), i.e. after each partitioning, on a block of size n, the result yields two sub-blocks of approximately equal size and the pivot element in the middle position takes n data comparisons. Recurrence Equation becomes T(1) = 1 T(n) = 2T(n/2) + cn Solution: θ(n logn) Comparable to Mergesort!! Quick Sort 12

Running time analysis Average case: It turns out the average case running time also is θ(n logn). We will wait until COMP 271 to discuss the analysis. Quick Sort 13

So the trick is to select a good pivot Different ways to select a good pivot. First element Last element Median-of-three elements Pick three elements, and find the median x of these elements. Use that median as the pivot. Random element Randomly pick a element as a pivot. Quick Sort 14

Different sorting algorithms Sorting Algorithm Worst-case time Averagecase time Space overhead Bubble Sort Θ (n 2 ) Θ (n 2 ) Θ(1) Insertion Sort Θ (n 2 ) Θ (n 2 ) Θ(1) Merge Sort Θ (n log n) Θ (n log n) Θ(n) Quick Sort Θ (n 2 ) Θ (n log n) Θ(1) Quick Sort 15

Something extra : Selection problem. Problem statement. You are given a unsorted array A[1..n] of (distinct) numbers, find a element from the array such that its rank is i, i.e., there are exactly (i-1) numbers less than or equal to that element. Example : A={5, 1, 2, 3, 12, 20, 30, 6, 14, -1, 0}, i=8. Output = 12, since 6, 1, -1, 0, 2, 3, 5 (8-1=7 numbers) are all less than or equal to 12. Quick Sort 16

Selection problem : a easy answer. A Easy algorithm. Sort A, and return A[i]. Obviously it works, but it is slow!!! Θ (n log n) in average. We want something faster. Quick Sort 17

Selection problem : a faster answer. We can borrow the idea from the partition algorithm. Suppose we want to find a element of rank i in A[1..n]. After the 1 st partition call (use a random element as pivot): 1. If the return index q = i, then A[q] is the element we want. (Since there is exactly i-1 elements smaller than or equal to A[q]). 2. If the return index q > i, then the target element can NOT be in A[q.. right]. The target element is rank i in A[1.. q-1]. Recursive call with parameters (A, 1, q-1, i). 3. If the return index q < i, then the target element can NOT be in A[1.. q]. The target element is rank i-q in A[q+1..n]. Recursive call with parameters (A, q+1,n, i-q). Quick Sort 18

Quick Sort 19

A faster selection algorithm : Codes return a element of rank i in A[p..r] A[p..q-1] A[q] A[q+1..r] size of A[p..q]= k Quick Sort 20

Analysis the faster answer. Though we claim it is a fast algorithm, the worst-case running time is O(n 2 ) (see if you can prove it). But the average-case running time is only O(n). (Again, we will see the analysis in COMP 271). There is an algorithm that runs in O(n) in the worst case, again, we will talk about that in COMP 271. Quick Sort 21