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.

Slides:



Advertisements
Similar presentations
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
Advertisements

Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CPS120: Introduction to Computer Science Searching and Sorting.
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
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.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
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
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.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting III 1 An Introduction to Sorting.
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.
Merge sort, Insertion sort
Quicksort.
Sorting Chapter 10.
Quicksort.
Unit 061 Quick Sort csc326 Information Structures Spring 2009.
Quicksort
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
CSE 373 Data Structures Lecture 19
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
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.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Sorting 2 Taking an arbitrary permutation of n items and rearranging them into total order Sorting is, without doubt, the most fundamental algorithmic.
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.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
3 – SIMPLE SORTING ALGORITHMS
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
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.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
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
Prof. U V THETE Dept. of Computer Science YMA
Searching and Sorting Algorithms
Quick Sort and Merge Sort
Chapter 7 Sorting Spring 14
Data Structures and Algorithms
Advanced Sorting Methods: Shellsort
Quick Sort (11.2) CSE 2011 Winter November 2018.
CSC215 Lecture Algorithms.
Data Structures and Algorithms
slides adapted from Marty Stepp
EE 312 Software Design and Implementation I
Presentation transcript:

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 which the order is based  There are many algorithms for sorting a list of items  These algorithms vary in efficiency

3 SORTING  We will examine several algorithms:  Selection Sort  Bubble Sort  Insertion Sort

4 Selection Sort  The approach of Selection Sort:  select one value and put it in its final place in the sort list  repeat for all other values

5 In more detail:  find the smallest value in the list  switch it with the value in the first position  find the next smallest value in the list  switch it with the value in the second position  repeat until all values are placed

6 Selection Sort  An example: original: smallest is 1: smallest is 2: smallest is 3: smallest is 6:  Pick any item and insert it into its proper place in a sorted sublist  repeat until all items have been inserted

7 In more detail Selection Sort:  consider the first item to be sorted as a sub list (of one item)  insert the second item into the sorted sub list, shifting items as necessary to make room for the new addition  insert the third item into the sorted sublist (of two items), shifting as necessary  repeat until all values are inserted into their proper position

8 Insertion Sort  An example of an insertion sort occurs in everyday life while playing cards.  To sort the cards in your hand you extract a card, shift the remaining cards, and then insert the extracted card in the correct place.  This process is repeated until all the cards are in the correct sequence. Both average and worst-case time is O(n 2 ).

9 Sorting  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 down one slot ¸ insert it «A«A «K«K « 10 ªJªJ «Q«Q ¸

Insertion Sort– Another n^2 sort  An example, : original: insert 9: insert 6: insert 1: insert 2:  Each loop iteration produces a sorted sub list.

11 Insertion Sort

12 INSERTION SORT n elements.  Assuming there are n elements. n - 1 other entries, resulting in a  For each entry, we may need to examine and shift up to n - 1 other entries, resulting in a  O(n 2 ) algorithm.  The insertion sort is an in-place sort. That is, we sort the array in-place, no needed helper array.

13 INSERTION SORT  No extra memory is required.  The insertion sort is also a stable sort.  Stable sorts retain the original ordering of keys when identical keys are present in the input data.

14 Comparing Sorts  Both Selection and Insertion sorts are similar in efficiency  The both have outer loops that scan all elements,  and inner loops that compare the value of the outer loop with almost all values in the list

15 Comparing Selection & Insertion Sort n 2 number of comparisons are made to sort a list of size n  Therefore approximately n 2 number of comparisons are made to sort a list of size n order n 2  We therefore say that these sorts are of order n 2  Other sorts are more efficient: order n log 2 n

16 Sorting - Insertion sort  Complexity  For each card  Scan for smaller card O(n)  Shift cards down O(n)  Insert O(1)  Total O(n)  First card requires O(1), second O(2), …  For n cards operations ç O(n 2 )  i i=1 n

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

18 Sorting - Bubble  From the first element Exchange pairs if they’re out of order  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

19 Bubble Sort 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]); }

20 Bubble Sort - Analysis void SWAP(a,b) { int t; t=a; a=b; b=t; // see below} 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

21 Bubble Sort - Analysis void bubble( int a[], int n ) { int i, j; for(i=0;i<n;i++) n passes thru the array 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++) { 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, …, i iterations O(1) statement

22 Bubble Sort - Analysis Overall  i i=n-1 1 = n(n+1) 2 = O(n 2 ) n outer loop iterations inner loop iteration count  BubbleSort

23 Complexity of the Bubble Sort  Thus:  ( N-1) + (N-2) = N*(N-2)/2 = O(N^2)  Thus Bubble Sort is an O(N^2) algorithm.

24 Sorting - Simple  Bubble sort  O(n 2 )  Very simple!!! code – slow, should be retired.  Insertion sort  Slightly better than bubble sort  Fewer comparisons  Also O(n 2 ) Selection sort is similar  But HeapSort is O(n log n)  Where would you use selection or insertion sort?

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

26 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!

27 Quicksort  Partition  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> pivotpivot

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

29 QuickSort quick_sort(left, right) { Boundary Condition: when left >= right -- do nothing // Boundary Condition: when left >= right -- do nothing // Place first item of current sublist in its correct position, C, in the sublist itself, such that // List[i] <= List[C], for all i, First <= i < C // List[J] > List[C], for all J, C List[C], for all J, C < J <= Last quick_sort (left, C-1); quick_sort (C+1, right); }

30 QuickSort code int partition(int array [], int left, int right) { int i = left, j = right; int i = left, j = right; int tmp; int tmp; int pivot = array[(left + right) / 2]; int pivot = array[(left + right) / 2]; while (i <= j) { while (array[i] < pivot) while (array[i] < pivot) i++; while (array[j] > pivot) while (array[j] > pivot) j--; if (i <= j) if (i <= j) { tmp = array[i]; tmp = array[i]; array[i] = arrar[j]; array[i] = arrar[j]; array[j] = tmp; array[j] = tmp; i++; j--; } }; return i; return i; }

31 Quicksort  Implementation quicksort( int [] 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

32 QuickSort void quickSort(int []array, int left, int right) { int index = partition(array, left, right); // sorts the left side of the array if (left < index - 1) quickSort(array, left, index - 1); // sorts the right side of the array if (index < right) quickSort(array, index, right); }

33 Quicksort - Partition int partition( int []a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; // pivot is first element in the array 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; }

34 Quicksort - Partition This example uses int ’s to keep things simple! lowhigh Any item will do as the pivot, choose the leftmost one!

35 Quicksort - Partition Set left and right markers lowhigh pivot : 23 leftright

36 Quicksort - Partition Move the markers until they cross over lowhigh pivot : 23 leftright

37 Quicksort - Partition Move the left pointer while items <= pivot lowhigh pivot : 23 leftright Move right Items >=pivot

38 Quicksort - Partition Swap the two items on the wrong side of the value of pivot lowhigh pivot : 23 leftright

39 Quicksort - Partition left and right have swapped over, so stop lowhigh pivot : 23 leftright

40 Quicksort - Partition Finally, swap the pivot and a[ right} lowhigh pivot : 23 left right

41 Quicksort - Partition Return the position of the pivot lowhigh pivot : 23 right

42 Quicksort - Conquer pivot pivot : 23 Recursively sort left half Recursively sort right half

43 Quicksort - Analysis  Partition  Check every item once O(n)  Conquer  Divide data in half O(log 2 n)  Total  Product O(n log n)  But there’s a catch …………….

44 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!

45 Quicksort - The truth!  Sorted data pivot < pivot ? > pivot

46 Quicksort - The truth!  Sorted data  Each partition produces  a problem of size 0  and one of size n-1 !  Number of partitions? > pivot pivot

47 Quicksort - The truth!  Sorted data  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(n 2 ) ? Quicksort is as bad as bubble or insertion sort > pivot pivot

48 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!!

49 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 < pivot > pivot

50 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!

51 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

52 Quicksort - Guaranteed O(n log n)?  Never!!  Any pivot selection strategy could lead to O(n 2 ) 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

53 Sorting- Key Points  Sorting  Bubble, Insertion, selection sort  O(n 2 ) sorts  Simple code  May run faster for small n, n ~10 (system dependent)  Quick Sort  Divide and conquer  O(n log n)

54 Sorting - Key Points  Quick Sort  O(n log n) but ….  Can be O(n 2 )  Depends on pivot selection  Median-of-3  Random pivot  Better but not guaranteed

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