Algorithm Design and Analysis (ADA) 242-535, Semester 1 2014-2015
3. Insertion Sort
• Objective o asymptotic analysis of insertion sort
242-535 ADA: 3. Insertion Sort 1 Overview
1. What is Sorting? 2. Insertion Sort
242-535 ADA: 3. Insertion Sort 2 1. What is Sorting?
Input: sequence
Output: permutation such that a'1 ≤ a'2 ≤ … ≤ a'n
Example: Input: 8 2 4 9 3 6 Output: 2 3 4 6 8 9
242-535 ADA: 3. Insertion Sort 3 Sorting is Essential
o Sort a list of names. o Organize an MP3 library. obvious applications 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. problems become easy once items are in sorted order o Identify statistical outliers. o Find duplicates in a mailing list. o Data compression. o Computer graphics. o Computational biology. non-obvious applications o Supply chain management. o Load balancing on a parallel computer. o . . . 242-535 ADA: 3. Insertion Sort 4 Different Sorting Needs
• 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?
242-535 ADA: 3. Insertion Sort 5 Many Different Sorting Algorithms
• 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
242-535 ADA: 3. Insertion Sort 6 2. Insertion Sort
INSERTION-SORT (A, n) A[1 . . n] for j ← 2 to n do key ⊳← A[ j] i ← j – 1 “pseudocode” while i > 0 and A[i] > key do A[i+1] ← A[i] i ← i – 1 A[i+1] = key 1 i j n A: key sorted Example of Insertion Sort 8 2 4 9 3 6 Example of Insertion Sort 8 2 4 9 3 6 Example of Insertion Sort 8 2 4 9 3 6 2 8 4 9 3 6