1 / 10

Chapter 7: Sorting A lgorithms

Mark Allen Weiss: Data Structures and Algorithm Analysis in Java. Chapter 7: Sorting A lgorithms. Insertion Sort. Lydia Sinapova, Simpson College. Sorting Algorithms. Insertion Sort Shell Sort Heap Sort Merge Sort Quick Sort. Assumptions.

ventana
Télécharger la présentation

Chapter 7: Sorting A lgorithms

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. Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Chapter 7: Sorting Algorithms Insertion Sort Lydia Sinapova, Simpson College

  2. Sorting Algorithms • Insertion Sort • Shell Sort • Heap Sort • Merge Sort • Quick Sort

  3. Assumptions • Elements to sort are placed in arrays of length N • Can be compared • Sorting can be performed in main memory

  4. Complexity • Simple sorting algorithms : O(N2) • Shellsort:o(N2) • Advanced sorting algorithms: O(NlogN) • In general:Ω(NlogN)

  5. Insertion Sort PRE: array of N elements (from 0 to N-1) POST: array sorted 1. An array of one element is sorted 2.Assume that the first p elements are sorted.   for j = p to N-1 Take the j-th element and find a place for it among the first j sorted elements

  6. Insertion Sort int j, p; comparable tmp; for ( p = 1; p < N ; p++) { tmp = a[p]; for ( j=p; j>0 && tmp < a[ j-1 ] ; j- - ) a[ j ] = a[ j-1 ]; a[ j ] = tmp; } Animation

  7. Analysis of the Insertion Sort Insert the N-th el.: at most N-1 comparisons N-1 movements  Insert the N-1st el. at most N-2 comparisons N-2 movements Insert the 2nd el.: 1 comparison 1 movement 2*(1 + 2 +… N - 1) = 2*N * (N-1) / 2 = N(N-1) = Θ (N2) Almost sorted array: O(N) Average complexity:Θ (N2)

  8. A lower bound for simple sorting algorithms • An inversion : • an ordered pair (Ai, Aj) such that • i < j but Ai > Aj • Example: 10, 6, 7, 15, 3,1 • Inversions are: • (10,6), (10,7), (10,3), (10,1), (6,3), (6,1) • (7,3), (7,1) (15,3), (15,1), (3,1)

  9. Swapping Swapping adjacent elements that are out of order removes one inversion. A sorted array has no inversions. Sorting an array that contains iinversions requires at least iswaps of adjacent elements How many inversions are there in an average unsorted array?

  10. Theorems Theorem 1:The average number of inversions in an array of N distinct elements is N (N - 1) / 4 Theorem 2:Any algorithm that sorts by exchanging adjacent elements requiresΩ (N2)time on average. For a sorting algorithm to run in less than quadratic time it must do something other than swap adjacent elements

More Related