DS-chapter6(sorting)

# DS-chapter6(sorting) - CHAPTER 6 SORTING 6.1 Preliminaries...

This preview shows pages 1–8. Sign up to view the full content.

CHAPTER 6 SORTING 6.1 Preliminaries void X_Sort ( ElementType A[ ], int N ) /* N must be a legal integer */ /* Assume integer array for the sake of simplicity */ /* ‘>’ and ‘<’ operators exist and are the only operations allowed on the input data */ Comparison- based sorting /* Consider internal sorting only */ The entire sort can be done in main memory

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6.2 Insertion Sort void InsertionSort ( ElementType A[ ], int N ) { int j, P; ElementType Tmp; for ( P = 1; P < N; P++ ) { Tmp = A[ P ]; /* the next coming card */ for ( j = P; j > 0 && A[ j - 1 ] > Tmp; j-- ) A[ j ] = A[ j - 1 ]; /* shift sorted cards to provide a position for the new coming card */ A[ j ] = Tmp; /* place the new card at the proper position */ } /* end for-P-loop */ } The worst case: Input A[ ] is in reverse order. T ( N ) = O( N 2 ) The best case: Input A[ ] is in sorted order. T ( N ) = O( N )
Insertion Sort H 34 8 64 51 32 21 H p=1 H 8 34 64 51 32 21 1 H p=2 H 8 34 64 51 32 21 0 H p=3 H 8 34 51 64 32 21 1 H p=4 H 8 32 34 51 64 21 3 H p=5 H 8 21 32 34 51 64 4

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6.3 A Lower Bound for Simple Sorting Algorithms H Definition O An inversion in an array of numbers is any ordered pair ( i , j ) having the property that i < j but A[ i ] > A[ j ]. HExample H Input list 34, 8, 64, 51, 32, 21 has inversions. 9 (34, 8) (34, 32) (34, 21) (64, 51) (64, 32) (64, 21) (51, 32) (51, 21) (32, 21) There are swaps needed to sort this list by insertion sort. 9 Swapping two adjacent elements that are out of place removes exactly one inversion. T ( N , I ) = O( ) where I is the number of inversions in the original array. I + N Fast if the list is almost sorted .
A Lower Bound H Theorem O The average number of inversions in an array of N distinct numbers is N ( N - 1 ) / 4 . H Theorem O Any algorithm that sorts by exchanging adjacent elements requires ( N 2 ) time on average. What does this theorem tell you? Is that all? How can you speed it up? Uhhh… hashing? Hey! We are talking about comparison-based sorting. You must do comparisons, and? … and swaps elements that are far apart? Smart guy! To run faster, we just have to eliminate more than just one inversion per exchange.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6.4 Shellsort ---- by Donald Shell HExample HSort: 81 94 11 96 12 35 17 95 28 58 41 75 15 96 28 12 58 81 35 41 94 17 75 11 95 15 5 -sort 96 41 94 11 28 58 35 75 95 12 81 17 15 3 -sort 1 -sort 96 41 94 11 28 58 35 75 95 12 81 17 15 Define an increment sequence h 1 < h 2 < … < h t ( h 1 = 1 ) Define an h k -sort at each phase for k = t , t - 1, …, 1 Note: An h k -sorted file that is then h k - 1 -sorted remains h k -sorted.
Shellsort

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 22

DS-chapter6(sorting) - CHAPTER 6 SORTING 6.1 Preliminaries...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online