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

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

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

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

View Full Document
§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
§3 A Lower Bound for Simple Sorting Algorithms Definition 【 An inversion in an array of numbers is any

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.

## This note was uploaded on 10/20/2011 for the course COMPUTER S 10586 taught by Professor Jilinwang during the Spring '09 term at Zhejiang University.

### 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
Ask a homework question - tutors are online