ADA: 3. Insertion Sort1 Objective o asymptotic analysis of insertion sort Algorithm Design and Analysis (ADA) , Semester Insertion Sort
ADA: 3. Insertion Sort2 1. What is Sorting? 2. Insertion SortOverview
ADA: 3. Insertion Sort3 Input: sequence of numbers. Output: permutation such that a'1 ≤ a'2 ≤ … ≤ a'n Example: Input: Output: What is Sorting?
ADA: 3. Insertion Sort4 o Sort a list of names. o Organize an MP3 library. o Display Google PageRank results. o List RSS feed in reverse chronological order. o Find the median. o Find the closest pair. o Binary search in a database. o Identify statistical outliers. o Find duplicates in a mailing list. o Data compression. o Computer graphics. o Computational biology. o Supply chain management. o Load balancing on a parallel computer. o... Sorting is Essential obvious applications problems become easy once items are in sorted order non-obvious applications
ADA: 3. Insertion Sort5 Applications have different sorting needs: o Stable? o Parallel? o Deterministic? o Keys all distinct? o Multiple key types? o Linked list or arrays? o Large or small items? o Is your array randomly ordered? o Need guaranteed performance? Different Sorting Needs
ADA: 3. Insertion Sort6 Internal sorts o Insertion sort, selection sort, bubblesort, shaker sort o Quicksort, mergesort, heapsort, samplesort, shellsort o Solitaire sort, red-black sort, splaysort,,... External sorts o Poly-phase mergesort, cascade-merge, oscillating sort String/radix sorts o Distribution, MSD, LSD, 3-way string quicksort Parallel sorts o Bitonic sort, Batcher even-odd sort o Smooth sort, cube sort, column sort o GPUsort Many Different Sorting Algorithms
2. Insertion Sort I NSERTION -S ORT (A, n) ⊳ A[1.. n] for j ← 2 to n dokey ← A[ j] i ← j – 1 while i > 0 and A[i] > key doA[i+1] ← A[i] i ← i – 1 A[i+1] = key “pseudocode” sorted ij key A:A: 1n
Example of Insertion Sort
824936
done
ADA: 3. Insertion Sort19 void insertionSort(int[] A) // A[0.. n-1] { (1) for (int j = 1; j < num.length; j++) { // start with 1 (not 2) (2) int key = num[j]; (3) int i = j - 1; (4) while((i >= 0) && (A[i] < key)) { (5) A[i+1] = A[i]; (6) i--; (7) } (8) A[i+1] = key; (9) } } Insertion Sort Java Code
ADA: 3. Insertion Sort20 Insertion Sort Structure Tree for block 1-9 while block
ADA: 3. Insertion Sort21 Lines 2, 3, 5, 6, 8: each is O(1) Block of 4-7 = O(1) + O(1) = O(1) For of 4-7 is: = O( (n-1) * 1) = O(n-1) = O(n), simplified Block of 1-9 = O(1) + O(1) + O(n) + O(1) = O(n) For of 1-8 is: = O( (n-1) * n) = O(n 2 - n) = O(n 2 ), simplified this is the hard part – assume the worse case where the loop has to move the most elements) this is the hard part – assume the worse case where the loop has to move the most elements)
ADA: 3. Insertion Sort22 What can T() be? o Best case -- inner loop body never executed T(n) is a linear function o Worst case -- inner loop body executed for all previous elements T(n 2 ) is a quadratic function o Average case tricky Analyzing Insertion Sort