Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
Advertisements

Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
Sorting Algorithms and Average Case Time Complexity
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.
Recursive sorting: Quicksort and its Complexity
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. 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.
CHAPTER 11 Sorting.
Quicksort.
Quicksort.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Sorting Algorithms and Analysis Robert Duncan. Refresher on Big-O  O(2^N)Exponential  O(N^2)Quadratic  O(N log N)Linear/Log  O(N)Linear  O(log N)Log.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
CSE 373 Data Structures Lecture 19
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.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Sorting CSIT 402 Data Structures II. 2 Sorting (Ascending Order) Input ›an array A of data records ›a key value in each data record ›a comparison function.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Heapsort, Mergesort, and Quicksort Sections 7.5 to 7.7.
The Sorting Methods Lecture Notes 10. Sorts Many programs will execute more efficiently if the data they process is sorted before processing begins. –
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
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.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Searching and Sorting Searching algorithms with simple arrays
Advanced Sorting.
Prof. U V THETE Dept. of Computer Science YMA
Searching and Sorting Algorithms
CSCI 104 Sorting Algorithms
Sorting Why? Displaying in order Faster Searching Categories Internal
Recitation 13 Searching and Sorting.
Quick Sort and Merge Sort
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Quicksort
Chapter 7 Sorting Spring 14
Description Given a linear collection of items x1, x2, x3,….,xn
Quicksort 1.
How can this be simplified?
Quick Sort (11.2) CSE 2011 Winter November 2018.
CO 303 Algorithm Analysis And Design Quicksort
Quicksort analysis Bubble sort
CSC215 Lecture Algorithms.
Unit-2 Divide and Conquer
Data Structures and Algorithms
8/04/2009 Many thanks to David Sun for some of the included slides!
Sorting … and Insertion Sort.
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Chapter 4.
EE 312 Software Design and Implementation I
Quicksort.
CSE 373 Data Structures and Algorithms
Data Structures & Algorithms
Quicksort.
CSE 326: Data Structures: Sorting
CSE 332: Sorting II Spring 2016.
Quicksort.
Presentation transcript:

Data Structures and Algorithms PLSD210 Sorting

Sorting ¶ « A « K « 10 « 2 ª J ª 2 · ¨ Q © 2 © 9 © 9 ¸ Card players all know how to sort … First card is already sorted With all the rest, Scan back from the end until you find the first card larger than the new one, Move all the lower ones up one slot insert it ¶ « A « K « 10 « 2 ª J ª 2 · ¨ Q © 2 © 9 © 9 ¸

Sorting - Insertion sort Complexity For each card Scan O(n) Shift up O(n) Insert O(1) Total O(n) First card requires O(1), second O(2), … For n cards operations ç O(n2) S i i=1 n

Sorting - Insertion sort Complexity For each card Scan O(n) O(log n) Shift up O(n) Insert O(1) Total O(n) First card requires O(1), second O(2), … For n cards operations ç O(n2) Use binary search! Unchanged! Because the shift up operation still requires O(n) time S i i=1 n

Insertion Sort - Implementation A challenge for you The code in the notes (and on the Web) has an error First person to email a correct version gets up to 2 extra marks added to their final mark if that would move them up a grade! ie if you had x8% or x9%, it goes to (x+1)0% To qualify, you need to point out the error in the original, as well as supply a corrected version!

Sorting - Bubble From the first element Exchange pairs if they’re out of order Last one must now be the largest Repeat from the first to n-1 Stop when you have only one element to check

Bubble Sort /* Bubble sort for integers */ #define SWAP(a,b) { int t; t=a; a=b; b=t; } void bubble( int a[], int n ) { int i, j; for(i=0;i<n;i++) { /* n passes thru the array */ /* From start to the end of unsorted part */ for(j=1;j<(n-i);j++) { /* If adjacent items out of order, swap */ if( a[j-1]>a[j] ) SWAP(a[j-1],a[j]); }

Bubble Sort - Analysis O(1) statement /* Bubble sort for integers */ #define SWAP(a,b) { int t; t=a; a=b; b=t; } void bubble( int a[], int n ) { int i, j; for(i=0;i<n;i++) { /* n passes thru the array */ /* From start to the end of unsorted part */ for(j=1;j<(n-i);j++) { /* If adjacent items out of order, swap */ if( a[j-1]>a[j] ) SWAP(a[j-1],a[j]); } O(1) statement

Bubble Sort - Analysis Inner loop O(1) statement /* Bubble sort for integers */ #define SWAP(a,b) { int t; t=a; a=b; b=t; } void bubble( int a[], int n ) { int i, j; for(i=0;i<n;i++) { /* n passes thru the array */ /* From start to the end of unsorted part */ for(j=1;j<(n-i);j++) { /* If adjacent items out of order, swap */ if( a[j-1]>a[j] ) SWAP(a[j-1],a[j]); } Inner loop n-1, n-2, n-3, … , 1 iterations O(1) statement

Bubble Sort - Analysis Outer loop n iterations /* Bubble sort for integers */ #define SWAP(a,b) { int t; t=a; a=b; b=t; } void bubble( int a[], int n ) { int i, j; for(i=0;i<n;i++) { /* n passes thru the array */ /* From start to the end of unsorted part */ for(j=1;j<(n-i);j++) { /* If adjacent items out of order, swap */ if( a[j-1]>a[j] ) SWAP(a[j-1],a[j]); } Outer loop n iterations

Bubble Sort - Analysis Overall n(n+1) S i = = O(n2) 2 /* Bubble sort for integers */ #define SWAP(a,b) { int t; t=a; a=b; b=t; } void bubble( int a[], int n ) { int i, j; for(i=0;i<n;i++) { /* n passes thru the array */ /* From start to the end of unsorted part */ for(j=1;j<(n-i);j++) { /* If adjacent items out of order, swap */ if( a[j-1]>a[j] ) SWAP(a[j-1],a[j]); } Overall 1 n(n+1) S i = = O(n2) 2 i=n-1 inner loop iteration count n outer loop iterations

Sorting - Simple Bubble sort Insertion sort But HeapSort is O(n log n) Very simple code Insertion sort Slightly better than bubble sort Fewer comparisons Also O(n2) But HeapSort is O(n log n) Where would you use bubble or insertion sort?

Simple Sorts Bubble Sort or Insertion Sort Use when n is small Simple code compensates for low efficiency!

Quicksort Efficient sorting algorithm Discovered by C.A.R. Hoare Example of Divide and Conquer algorithm Two phases Partition phase Divides the work into half Sort phase Conquers the halves!

Quicksort Partition < pivot pivot > pivot Choose a pivot Find the position for the pivot so that all elements to the left are less all elements to the right are greater < pivot pivot > pivot

Quicksort Conquer < pivot > pivot < p’ p’ > p’ pivot Apply the same algorithm to each half < pivot > pivot < p’ p’ > p’ pivot < p” p” > p”

Quicksort Implementation quicksort( void *a, int low, int high ) { int pivot; /* Termination condition! */ if ( high > low ) pivot = partition( a, low, high ); quicksort( a, low, pivot-1 ); quicksort( a, pivot+1, high ); } Divide Conquer

Quicksort - Partition int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--; if ( left < right ) SWAP(a,left,right); } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right;

Any item will do as the pivot, choose the leftmost one! Quicksort - Partition This example uses int’s to keep things simple! int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--; if ( left < right ) SWAP(a,left,right); } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; Any item will do as the pivot, choose the leftmost one! 23 12 15 38 42 18 36 29 27 low high

Set left and right markers Quicksort - Partition int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--; if ( left < right ) SWAP(a,left,right); } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; Set left and right markers left right 23 12 15 38 42 18 36 29 27 low pivot: 23 high

Quicksort - Partition Move the markers until they cross over left int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--; if ( left < right ) SWAP(a,left,right); } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; Move the markers until they cross over left right 23 12 15 38 42 18 36 29 27 low pivot: 23 high

Move the left pointer while it points to items <= pivot Quicksort - Partition int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--; if ( left < right ) SWAP(a,left,right); } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; Move the left pointer while it points to items <= pivot left right Move right similarly 23 12 15 38 42 18 36 29 27 low pivot: 23 high

on the wrong side of the pivot Quicksort - Partition int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--; if ( left < right ) SWAP(a,left,right); } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; Swap the two items on the wrong side of the pivot left right 23 12 15 38 42 18 36 29 27 pivot: 23 low high

Quicksort - Partition left and right have swapped over, so stop right int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--; if ( left < right ) SWAP(a,left,right); } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; left and right have swapped over, so stop right left 23 12 15 18 42 38 36 29 27 low pivot: 23 high

Quicksort - Partition right left 23 12 15 18 42 38 36 29 27 low int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--; if ( left < right ) SWAP(a,left,right); } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; right left 23 12 15 18 42 38 36 29 27 low pivot: 23 high Finally, swap the pivot and right

Quicksort - Partition right pivot: 23 18 12 15 23 42 38 36 29 27 low int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] >= pivot_item ) right--; if ( left < right ) SWAP(a,left,right); } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; right pivot: 23 18 12 15 23 42 38 36 29 27 low high Return the position of the pivot

Quicksort - Conquer pivot pivot: 23 18 12 15 23 42 38 36 29 27 Recursively sort left half Recursively sort right half

Quicksort - Analysis Partition Conquer Total Check every item once O(n) Conquer Divide data in half O(log2n) Total Product O(n log n) Same as Heapsort quicksort is generally faster Fewer comparisons Details later (and assignment 2!) But there’s a catch …………….

Quicksort - The truth! What happens if we use quicksort on data that’s already sorted (or nearly sorted) We’d certainly expect it to perform well!

Quicksort - The truth! Sorted data pivot ? 1 2 3 4 5 6 7 8 9

Quicksort - The truth! Sorted data Each partition produces pivot 1 2 3 a problem of size 0 and one of size n-1! Number of partitions? pivot 1 2 3 4 5 6 7 8 9 > pivot pivot 2 3 4 5 6 7 8 9 > pivot

Quicksort - The truth! Sorted data pivot 1 2 3 4 5 6 7 8 9 > pivot Each partition produces a problem of size 0 and one of size n-1! Number of partitions? n each needing time O(n) Total nO(n) or O(n2) Quicksort is as bad as bubble or insertion sort pivot 1 2 3 4 5 6 7 8 9 > pivot pivot 2 3 4 5 6 7 8 9 > pivot

Quicksort - The truth! Quicksort’s O(n log n) behaviour Depends on the partitions being nearly equal there are O( log n ) of them On average, this will nearly be the case and quicksort is generally O(n log n) Can we do anything to ensure O(n log n) time? In general, no But we can improve our chances!!

Quicksort - Choice of the pivot Any pivot will work … Choose a different pivot … so that the partitions are equal then we will see O(n log n) time pivot 1 2 3 4 5 6 7 8 9 > pivot < pivot

Quicksort - Median-of-3 pivot Take 3 positions and choose the median say … First, middle, last median is 5 perfect division of sorted data every time! O(n log n) time Since sorted (or nearly sorted) data is common, median-of-3 is a good strategy especially if you think your data may be sorted! 1 2 3 4 5 6 7 8 9

Quicksort - Random pivot Choose a pivot randomly Different position for every partition On average, sorted data is divided evenly O(n log n) time Key requirement Pivot choice must take O(1) time

Quicksort - Guaranteed O(n log n)? Never!! Any pivot selection strategy could lead to O(n2) time Here median-of-3 chooses 2 One partition of 1 and One partition of 7 Next it chooses 4 One of 1 and One of 5 1 4 9 6 2 5 7 8 3 1 2 4 9 6 5 7 8 3

Lecture 8 - Key Points Sorting Bubble, Insert O(n2) sorts Simple code May run faster for small n, n ~10 (system dependent) Quick Sort Divide and conquer O(n log n)

Lecture 8 - Key Points Quick Sort Better but not guaranteed O(n log n) but …. Can be O(n2) Depends on pivot selection Median-of-3 Random pivot Better but not guaranteed

Quicksort - Why bother? Use Heapsort instead? Quicksort is generally faster Fewer comparisons and exchanges Some empirical data

Quicksort - Why bother? Divide by n log n Divide by n2 Reporting data Normalisation works when you have a hypothesis to work with! Divide by n log n Divide by n2

Quicksort vs Heap Sort Quicksort Heap Sort Generally faster Sometimes O(n2) Better pivot selection reduces probability Use when you want average good performance Commercial applications, Information systems Heap Sort Generally slower Guaranteed O(n log n) … Can design this in! Use for real-time systems Time is a constraint

Quicksort - library implementation POSIX standard void qsort( void *base, size_t n, size_t size, int (*compar)( const void *, const void * ) ); base address of array n number of elements size size of an element compar comparison function

Quicksort - library implementation POSIX standard Comparison function C allows you to pass a function to another function! void qsort( void *base, size_t n, size_t size, int (*compar)( const void *, const void * ) ); base address of array n number of elements size size of an element compar comparison function