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

Similar documents
Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

COMP Data Structures

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

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

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

Sorting Algorithms. + Analysis of the Sorting Algorithms

Overview of Sorting Algorithms

LECTURE 17. Array Searching and Sorting

Sorting Pearson Education, Inc. All rights reserved.

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

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

DATA STRUCTURES AND ALGORITHMS

Lecture 8: Mergesort / Quicksort Steven Skiena

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Measuring algorithm efficiency

We can use a max-heap to sort data.

Algorithmic Analysis and Sorting, Part Two

Lecture 6 Sorting and Searching

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

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

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

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

COMP 250. Lecture 7. Sorting a List: bubble sort selection sort insertion sort. Sept. 22, 2017

Divide and Conquer Algorithms: Advanced Sorting

CSE 373: Data Structures and Algorithms

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

Sorting. Weiss chapter , 8.6

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018

Sorting Algorithms Day 2 4/5/17

Data Structures and Algorithms Chapter 4

SCJ2013 Data Structure & Algorithms. Bubble Sort. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

SORTING. Comparison of Quadratic Sorts

COSC 311: ALGORITHMS HW1: SORTING

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Quicksort (Weiss chapter 8.6)

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

O(n): printing a list of n items to the screen, looking at each item once.

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

Sorting. Bringing Order to the World

1 a = [ 5, 1, 6, 2, 4, 3 ] 4 f o r j i n r a n g e ( i + 1, l e n ( a ) 1) : 3 min = i

Data Structures and Algorithms Week 4

Data Structures and Algorithms

BM267 - Introduction to Data Structures

Elementary Sorting Algorithms

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Better sorting algorithms (Weiss chapter )

CS 171: Introduction to Computer Science II. Quicksort

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

CSC Design and Analysis of Algorithms

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Lecture 19 Sorting Goodrich, Tamassia

Data Structures and Algorithms. Roberto Sebastiani

C/C++ Programming Lecture 18 Name:

Cpt S 122 Data Structures. Sorting

Lecture 5: Sorting Part A

Algorithmic Analysis and Sorting, Part Two. CS106B Winter

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

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

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

Data Structures and Algorithms

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Quick Sort. CSE Data Structures May 15, 2002

Data Structure Lecture#16: Internal Sorting (Chapter 7) U Kang Seoul National University

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

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Computers in Engineering COMP 208. Where s Waldo? Linear Search. Searching and Sorting Michael A. Hawker

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

CSE 2123 Sorting. Jeremy Morris

ECE 242 Data Structures and Algorithms. Advanced Sorting II. Lecture 17. Prof.

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

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

Simple Sorting Algorithms

Searching and Sorting

Chapter 7 Sorting. Terminology. Selection Sort

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

Practical Session #11 - Sort properties, QuickSort algorithm, Selection

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

1/ COP 3503 FALL 2012 SHAYAN JAVED LECTURE 16. Programming Fundamentals using Java

Hash Tables. Hash functions Open addressing. November 24, 2017 Hassan Khosravi / Geoffrey Tien 1

Data Structures and Algorithms 2018

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Data Structures and Algorithms Sorting

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

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

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

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

Sorting Algorithms. For special input, O(n) sorting is possible. Between O(n 2 ) and O(nlogn) E.g., input integer between O(n) and O(n)

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

Sorting. Bubble Sort. Selection Sort

1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

Final: CSS 342 SAMPLE. Data Structures, Algorithms, and Discrete Mathematics I

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

Sorting: Quick Sort. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

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

Transcription:

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

Quicksort analysis How long does Quicksort take to run? Let's consider the best and the worst case These differ because the partitioning algorithm may not always do a good job Let's look at the best case first Each time a sub-array is partitioned the pivot is the exact midpoint of the slice (or as close as it can get) So it is divided in half What is the running time? November 20, 2017 Hassan Khosravi / Geoffrey Tien 2

Quicksort best case Each sub-array is divided in half in each partition Each time a series of sub-arrays are partitioned n (approximately) comparisons are made The process ends once all the sub-arrays left to be partitioned are of size 1 How many times does n have to be divided in half before the result is 1? log 2 n times Running time Quicksort performs n log 2 n operations in the best case Same complexity as Merge sort November 20, 2017 Hassan Khosravi / Geoffrey Tien 3

Quicksort worst case Every partition step ends with no values on one side of the pivot The array has to be partitioned n times, not log 2 n times n comparisons in the first partition step n 1 comparisons in the second step n 2 comparisons in the third step n + n 1 + n 2 + + 2 + 1 = n(n 1) 2 So in the worst case Quicksort performs around n 2 operations The worst case usually occurs when the array is nearly sorted (in either direction) As bad as selection sort! November 20, 2017 Hassan Khosravi / Geoffrey Tien 4

Quicksort average case With a large array we would have to be very, very unlucky to get the worst case Unless there was some reason for the array to already be partially sorted The average case is much more like the best case than the worst case There is an easy way to fix a partially sorted array to that it is ready for Quicksort Randomize the positions of the array elements! What is the complexity of performing a random scramble of the array? November 20, 2017 Hassan Khosravi / Geoffrey Tien 5

Quicksort average case Let s assume that pivot choice is random Half the time the pivot will be from the centre half of the array Thus at worst the split will be n 4 and 3n 4 We can apply this to the notion of a good split Every "good" split: 2 partitions of size n/4 and 3n/4 Or, divides n by 4/3 Expected number of partitions is at most 2 log 4/3 n O log n Intuition Given n comparisons at each partitioning step, we have Θ n log n Same best-case complexity as Merge sort, but in practice, Quicksort tends to be slightly faster. Why? November 20, 2017 Hassan Khosravi / Geoffrey Tien 6

Merge sort vs Quicksort and Quicksort summary If Quicksort worst case is so bad, why use it? worst case is exceedingly rare and can be easily avoided in practice, faster than Merge sort. can be sorted in-place using O log n stack space also highly parallelizable Name Best Average Worst Stable Memory Selection sort O n 2 O n 2 O n 2 challenging O 1 Insertion sort O n O n 2 O n 2 Yes O 1 Merge sort O n log n O n log n O n log n Yes O n Quicksort O n log n O n log n O n 2 challenging O log n November 20, 2017 Hassan Khosravi / Geoffrey Tien 7

Bubble sort Bubble sort is a simple sorting algorithm with a good best case performance, but generally poor performance in average and worst case Scans the array from left to right checks adjacent pairs and swaps if out of order If any swap was performed during a scan, re-scan the array The next largest item is put into place during each scan, so each scan does not need to go all the way to the end of the array Array is sorted if a scan can complete without performing any swaps November 20, 2017 Hassan Khosravi / Geoffrey Tien 8

Bubble sort Example swapped = TRUE FALSE swapped = TRUE FALSE swapped = TRUE FALSE swapped = TRUE FALSE swapped = TRUE FALSE swapped = TRUE FALSE 6 5 3 1 8 7 2 4 i 5 3 1 6 7 2 4 8 i 3 1 5 6 2 4 7 8 i 1 3 5 2 4 6 7 8 i 1 3 2 4 5 6 7 8 i 1 2 3 4 5 6 7 8 i Completed scan with no swaps November 20, 2017 Hassan Khosravi / Geoffrey Tien 9

Bubble sort algorithm #define TRUE 1 #define FALSE 0 void bubblesort(int arr[], int size) { int i, j, temp; int swapped = FALSE; for (i = 0; i < n; i++) { swapped = FALSE; // reset flag for this scan iteration for (j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { temp = arr[j+1]; arr[j+1] = arr[j]; arr[j] = temp; swapped = TRUE; // a swap occurred during this scan } } if (!swapped) return; } } November 20, 2017 Hassan Khosravi / Geoffrey Tien 10

Bubble sort analysis Inner loop increments and swaps Outer loop repeats if a swap occurred if no swap occurred, algorithm terminates Best case everything already in order! O n An item out of place can only move to the left by one position per outer loop iteration if the smallest item is at the rightmost position, it requires n 1 outer loop iterations worst case! O n 2 Average case is more similar to worst case November 20, 2017 Hassan Khosravi / Geoffrey Tien 11

When do we use Bubble sort Rarely, in practice When we want to show some "bad" (but easy to implement) sorting algorithms for comparison in a second-year computer science class but it is far from the worst, e.g. Bogosort Name Best Average Worst Stable Memory Selection sort O n 2 O n 2 O n 2 challenging O 1 Insertion sort O n O n 2 O n 2 Yes O 1 Merge sort O n log n O n log n O n log n Yes O n Quicksort O n log n O n log n O n 2 challenging O log n Bubble sort O n O n 2 O n 2 Yes O 1 Heapsort O n log n O n log n O n log n No O 1 November 20, 2017 Hassan Khosravi / Geoffrey Tien 12

Exercise Suppose we implemented Merge sort, to split the array into 3 (roughly) equally-sized subarrays, and the Merge function repeatedly copies in the smallest of the three values in each subarray index Perform a recurrence analysis to see how the (asymptotic) running time will be affected. For the following sequence of values, perform a single round of Quicksort partition using the implementation described in these notes; identify which element becomes the pivot value 9, 6, 7, 17, 11, -1, -5, 19, 1, 2 November 20, 2017 Hassan Khosravi / Geoffrey Tien 13

Readings for this lesson Thareja Chapter 14.11, 14.7 (Quicksort, Bubble sort) Next class: Thareja Chapter 15.1 15.3 (Hash tables, hash functions) November 20, 2017 Hassan Khosravi / Geoffrey Tien 14