Lecture 6COMPSCI.220.FS.T Data Sorting Ordering relation: places each pair , of countable items in a fixed order denoted as ( ) or Order notation: ≤ ( less than or equal to ) Countable item: labelled by a specific integer key Comparable objects in Java: if an object can be less than, equal to, or greater than another object: object1.compareTo( object2 ) 0
Lecture 6COMPSCI.220.FS.T Order of Data Items Numerical order - by value: 5 ≤ 5 ≤ 6.45 ≤ ≤ … ≤ Alphabetical order - by position in an alphabet: a ≤ b ≤ c ≤ d ≤ … ≤ z Such ordering depends on the alphabet used: look into any bilingual dictionary... Lexicographic order - by first differing element: 5456 ≤ 5457 ≤ 5500 ≤ 6100 ≤ … pork ≤ ward ≤ word ≤ work ≤ …
Lecture 6COMPSCI.220.FS.T Features of ordering Relation on an array A = {a, b, c, …} is: – reflexive: a ≤ a – transitive: if a ≤ b and b ≤ c, then a ≤ c – symmetric: if a ≤ b and b ≤ a, then a b Linear order if for any pair of elements a and b either a ≤ b or b ≤ a : a ≤ b ≤ c ≤ … Partial order if there are incomparable elements
Lecture 6COMPSCI.220.FS.T Insertion Sort Splits an array into a unordered and ordered parts Sequentially contracts the unordered part, one element per stage: ordered part unordered part a 0, …, a i 1 a i, …, a n 1 Stage i = 1, …, n 1 : n i unordered and i ordered elements
Lecture 6COMPSCI.220.FS.T Example of Insertion Sort N c - number of comparisons per insertion N m - number of moves per insertion NcNc NmNm 1544<→ 1535<→ 1518<→ 15≥
Lecture 6COMPSCI.220.FS.T Example of Insertion Sort NcNc NmNm 1044<→ 1035<→ 1018<→ 1015<→ 1013<→
Lecture 6COMPSCI.220.FS.T Implementation of Insertion Sort begin InsertionSort ( integer array a of size n ) 1. for i ← 1 while i n step i ← i + 1 do 2. s tmp ← a[ i ]; k ← i 1 3. while k ≥ 0 AND s tmp < a[k] do 4. a[ k +1 ] ← a[ k ]; k ← k 1 5. end while 6. a[ k+1 ] ← s tmp 7. end for end InsertionSort
Lecture 6COMPSCI.220.FS.T Average Complexity at Stage i i + 1 positions to place a next item: … i -1 i i j + 1 comparisons and i j moves for each position j = i, i 1, …, 1 i comparisons and i moves for position j = 0 Average number of comparisons:
Lecture 6COMPSCI.220.FS.T Total Average Complexity n 1 stages for n input items: the total average number of comparisons: H n ln n when n is the n- th harmonic number
Lecture 6COMPSCI.220.FS.T Analysis of Inversions An inversion in an array A = [a 1,a 2, …, a n ] is any ordered pair of positions (i, j) such that i a j : e.g., […, 2,…, 1] or [100, …, 35, …] A# invers.A reverse # invers.Total # 3,2,515,2,323 3,2,5,141,5,2,326 1,2,3,4,707,4,3,2,110
Lecture 6COMPSCI.220.FS.T Analysis of Inversions Total number of inversions both in an arbitrary array A and its reverse A reverse is equal to the total number of the ordered pairs ( i < j ): A sorted array has no inversions A reverse sorted array has inversions
Lecture 6COMPSCI.220.FS.T Analysis of Inversions Exactly one inversion is removed by swapping two neighbours a i 1 a i An array with k inversions results in O(n + k) running time of insertionSort Worst-case time: Average-case time:
Lecture 6COMPSCI.220.FS.T More Efficient Shell's Sort Efficient sort must eliminate more than just one inversion between the neighbours per exchange! (insertion sort eliminates one inversion per exchange) D.Shell (1959): compare first the keys at a distance of gap T, then of gap T-1 < gap T, and so on until of gap 1 =1 After a stage with gap t, all elements spaced gap t apart are sorted; it can be proven that any gap t -sorted array remains gap t -sorted after being then gap t-1 -sorted
Lecture 6COMPSCI.220.FS.T Implementation of ShellSort begin Shell Sort ( integer array a of size n ) 1. for gap ← n ⁄ 2 while gap 0 step gap ← ( if gap 2 then 1 else gap ⁄ 2.2 ) do 2. for i ← gap while i n step i ← i 1 do 3. s t mp ← a[i]; k ← i 4. while( k ≥ gap AND s t mp < a [ k gap ] do 5. a [ k ] ← a [ k gap ]; k ← k gap 6. end while 7. a [ k ] ← s t mp ; 8. end for; 9. end for end Shell Sort
Lecture 6COMPSCI.220.FS.T Example of ShellSort : step 1 gap i :C:MData to be sorted :1: :1:0820 7:1:0231 8:1: :1:
Lecture 6COMPSCI.220.FS.T Example of ShellSort : step 2 gap i :C:M :1: :1:0815 4:1: :1: :3: :2: :1: :1:05070
Lecture 6COMPSCI.220.FS.T Example of ShellSort : step 3 gap i :C:M :1:028 2:1:0820 3:2: :1: :1: :1: :2: :1: :2:
Lecture 6COMPSCI.220.FS.T Time complexity of ShellSort Heavily depends on gap sequences Shell's sequence: n / 2, n / 4, …, 1 : O(n 2 ) worst; O(n 1.5 ) average “Odd gaps only” (if even: gap / 2 + 1): O(n 1.5 ) worst; O(n 1.25 ) average Heuristic sequence: gap / 2.2 : better than O(n 1.25 ) A very simple algorithm with an extremely complex analysis