1 / 21

Sorting

Sorting. Sorting Terminology. Sort Key each element to be sorted must be associated with a sort key which can be compared with other keys e.g. for any two keys k i and k j , k i > k j , k i < k j , or k i = k j. Sorting Terminology. The Sorting Problem

quang
Télécharger la présentation

Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting

  2. Sorting Terminology • Sort Key • each element to be sorted must be associated with a sort key which can be compared with other keys e.g. for any two keys ki and kj, ki > kj , ki < kj ,or ki = kj

  3. Sorting Terminology • The Sorting Problem Arrange a set of records so that the values of their key fields are in non-decreasing order.

  4. Sorting Algorithms(Running Time Analysis) • Things to measure • comparisons bet. keys • swaps The measure of these things usually approximate fairly accurately the running time of the algorithm.

  5. Sorting Algorithms • Insertion Sort O( n2 ) • Bubble Sort O( n2 ) • Selection Sort O( n2 ) • Shellsort O( n1.5 ) • Quicksort O( nlog2n) • Mergesort O( nlog2n) • Heapsort O( nlog2n) • Binsort O( n) w/ qualified input • Radix Sort O( n) w/ qualified input

  6. Insertion Sort: Algorithm void insertionSort( Elem[] a, int n ) { for( int i = 1; i < n; i++ ) { for( int j = i; (j > 0) && ( a[ j].key < a[ j-1].key); j-- ) { swap( a[ j], a[ j-1] ) } } }

  7. Insertion Sort: Illustration

  8. Insertion Sort: Time complexity • outer for loop executed n-1 times • inner for loop depends on how many keys before element i are less than it. • worst case: reverse sorted (each ith element must travel all the way up) • running time: (n-1)(n)/2 => O( n2 ) • best case: already sorted (each ith element does not need to travel at all) • running time: (n-1) => O( n ) • average case: { (n-1)(n)/2 } / 2 => O( n2 )

  9. Bubble Sort: Algorithm void bubbleSort( Elem[] a, int n ) { for( int i = 0; i < n-1; i++ ) { for( int j = n-1; j > i; j-- ) { if( a[ j].key < a[j-1].key ) { swap( a[ j], a[ j-1] ) } } } }

  10. Bubble Sort: Illustration

  11. Bubble Sort: Time complexity • number of comparisons in inner for loop for the ith iteration is always equals to i • running time: i = n(n+1)/2  O( n2 ) n i = 1

  12. Selection Sort: Algorithm void selectionSort( Elem[] a, int n ) { for( int i = 0; i < n-1; i++ ) { int lowindex = i for( int j = n-1; j > i; j-- ) { if( a[ j].key < a[ lowindex ].key ) { lowindex = j; } } swap( a[i], a[ lowindex ] ) } }

  13. Selection Sort: Illustration

  14. Selection Sort: Time complexity • number of comparisons in inner for loop for the ith iteration is always equals to i • running time: i = n(n+1)/2  O( n2 ) n i = 1

  15. Shellsort: Algorithm void shellsort( Element[] a ) { for( int i = a.length/2; i >= 2; i /=2 ) { for( int j = 0; j < i; j++ ) { insertionSort2( a, j, a.length-j, i ); } } insertionSort2( a, 0, a.length, 1 ); } void insertionSort2( Element[] a, int start, int n, int incr ) { for( int i=start+incr; i<n; i+=incr) { for( j=i; (j>=incr) && (a[ j].key < a[ j-incr].key); j-=incr) { swap( a[j], a[j-incr] ); } } }

  16. Shellsort: Illustration

  17. Shellsort: Time complexity • O( n1.5 )

  18. Quicksort: Algorithm void quicksort( Elem[] a, int I, int j ) { int pivotindex = findpivot( a, i, j ) swap( a[pivotindex], array[j] ) // stick pivot at the end int k = partition( a, i-1, j, a[j].key ) // k will be the first position in the right subarray swap( a[k], a[j] ) // put pivot in place if( k-i > 1 ) quicksort( a, i, k-1 ) // sort left partition if( j-k > 1 ) quicksort( a, k+1, j ) // sort right partition } int findpivot( Elem[] A, int i, int j ){ return ( (i+j) / 2 ) } int partition( Elem[] A, int l, int r, Key pivot ) { do // move the bounds inward until they meet { while( a[++l ].key < pivot ) // move left bound right while( r && a[--r].key > pivot ) // move right bound left swap( a[l], a[r] ) // swap out-of-place values }while( l < r ) // stop when they cross swap( a[l], a[r] ) // reverse last, wasted swap return l // return the first position in right position }

  19. Quicksort: Illustration

  20. Quicksort: Illustration

  21. Quicksort: Time complexity • findpivot() takes constant time: 0(1) • partition() depends on the length of the sequence to be partitioned: • O(s) for sequence of length s • Worst-case: when pivot splits the array of size n into partitions of size n-1 and 0. O(n2) • Best case: when pivot always splits the array into two equal halves. • There will be log2n levels (1st level: one n sequence, 2nd level: two n/2 sequences, 3rd level: four n/4 sequences, …): O(nlog2n) • Average case: O( n log2n ) • given by the recurrence relation T(n) = cn + 1/n (T(k) +T(n - 1 - k)), T(0) = c, T(1) = c n-1 k = 0

More Related