DS12_Ch06a - CHAPTER 6 SORTING 1 Preliminaries void X_Sort...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 6 SORTING §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 1/4
Background image of page 1

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

View Full DocumentRight Arrow Icon
§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 ) 2/4
Background image of page 2
§3 A Lower Bound for Simple Sorting Algorithms Definition 【 An inversion in an array of numbers is any
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

DS12_Ch06a - CHAPTER 6 SORTING 1 Preliminaries void X_Sort...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online