120 likes | 504 Views
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.
E N D
Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Chapter 7: Sorting Algorithms Insertion Sort Lydia Sinapova, Simpson College
Sorting Algorithms • Insertion Sort • Shell Sort • Heap Sort • Merge Sort • Quick Sort
Assumptions • Elements to sort are placed in arrays of length N • Can be compared • Sorting can be performed in main memory
Complexity • Simple sorting algorithms : O(N2) • Shellsort:o(N2) • Advanced sorting algorithms: O(NlogN) • In general:Ω(NlogN)
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
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
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)
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)
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?
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