Decision Maths Unit 7 Sorting Algorithms 3. Shell Sort
18, 5, 12, 9, 3, 7, 8, 2 As there are 8 numbers there will be 8 ÷ 2 = 4 sub lists The first and fifth number 18 and 3 The second and sixth number 5 and 7 The third and seventh number 12 and 8 The fourth and eighth number 9 and 2 Shuttle sort and merge of these pairs of numbers gives 3, 5, 8, 2, 18, 7, 12, 9 On the first pass there were 4 sub lists, so on the second pass there will be 4 ÷ 2 = 2 sub lists The first sub list contains the 1st, 3rd, 5th and 7th numbers The second sub list contains the 2nd 4th , 6th and 8th numbers
The first sub list contains the 1st, 3rd, 5th and 7th numbers 3, 8, 18, 12 The second sub list contains the 2nd 4th , 6th and 8th numbers 5, 2, 7, 9 The two lists are shuttle sorted and merged again to give 3, 2, 8, 5, 12, 7, 18, 9 There were 2 sub lists in the second pass, so in the third pass there will be 2 ÷ 2 = 1 sub list. This last list is now shuttle sorted. 2, 3, 5, 7, 8, 9, 12, 18
Writing the list at each step gives Initial list 18, 5, 12, 9, 3, 7, 8, 2 Four sub lists 18 3 5 7 12 8 9 2 3 18 First Pass 5 7 SORT 8 12 2 9 MERGE 3, 5, 8, 2, 18, 7, 12, 9
Two sub lists 3 8 18 12 5 2 7 9 Second Pass 3 8 12 18 SORT 2 5 7 9 MERGE 3, 2, 8, 5, 12, 7, 18, 9 One sub list 3, 2, 8, 5, 12, 7, 18, 9 Third Pass SORT 2, 3, 5, 7, 8, 9, 12, 18
Pass Comparisons Swaps 1st 4 3 2nd 7 2 3rd 12 6 Total 23 11 In the first pass, there were 4 comparisons and 3 swaps 18 and 3 1 comparison 1 Swap 5 and 7 1 comparison 0 Swap 12 and 8 1 comparison 1 Swap 9 and 2 1 comparison 1 Swap
3, 8, 18, 12 1 comparison 0 Swap 3, 8, 18, 12 1 comparison 0 Swap 3, 8, 18, 12 2 comparisons 1 Swap 3, 8, 12, 18 5, 2, 7, 9 1 comparison 1 Swap 2, 5, 7, 9 1 comparison 0 Swap 2, 5, 7, 9 1 comparison 0 Swap 2, 5, 7, 9
8 / 2 = 4 sub lists 32 11 3 27 16 9 23 19 32 16 11 9 3 23 27 19 First Pass Shuttle Sort C S 1 1 16 32 9 11 1 1 3 23 1 0 19 27 Merge 1 1 19 27 9 11 3 23 16 32 4 3
4 / 2 = 2 sub lists 16 9 3 19 32 11 23 27 16 3 32 23 9 19 11 27 Second Pass Shuttle Sort C S 4 2 3 16 23 32 9 11 19 27 4 1 Merge 3 16 23 32 9 11 19 27 8 3
2 / 2 = 1 sub list Third Pass Shuttle Sort C S 1 0 3 9 16 11 23 19 32 27 3 9 16 11 23 19 32 27 1 0 3 9 16 11 23 19 32 27 2 1 3 9 11 16 23 19 32 27 1 0 3 9 11 16 23 19 32 27 2 1 3 9 11 16 19 23 32 27 1 0 3 9 11 16 19 23 32 27 2 1 3 9 11 16 19 23 27 32 10 3 Total Comparisons and swaps First Pass Second Pass Third Pass Total Comparisons 4 8 10 22 Swaps 3 3 3 9
7 / 2 = 3 sub lists J R E B A I W C S 3 2 First Pass Shuttle Sort E J R 1 1 A B I W 1 0 Merge E J R A B I W 5 3
3 / 2 = 1 sub list A B I W E J R C S 1 1 Second Pass Shuttle Sort A B I W E J R A E I J B W R 1 0 A E I J B W R 1 0 A E I J B W R 4 3 A B E I J W R 1 0 A B E I J W R 2 1 A B E I J R W 10 5 Total Comparisons and swaps First Pass Second Pass Total Comparisons 5 10 15 Swaps 3 5 8
9 / 2 = 4 sub lists 18 3 45 17 1 26 43 22 16 18 1 16 3 26 45 43 First Pass Shuttle Sort 17 22 C S 3 2 1 16 18 3 26 1 0 43 45 1 1 22 17 1 1 Merge 22 17 1 16 18 43 45 3 26 6 4
4 / 2 = 2 sub lists 1 3 43 22 16 26 45 17 18 1 43 16 45 18 3 22 26 17 Second Pass Shuttle Sort C S 7 3 1 16 18 43 45 3 17 22 26 5 2 Merge 1 16 18 43 45 3 17 22 26 12 5
Third Pass Shuttle Sort C S 1 0 1 3 16 17 18 22 43 26 45 1 3 16 17 18 22 43 26 45 1 0 1 3 16 17 18 22 43 26 45 1 0 1 3 16 17 18 22 43 26 45 1 0 1 3 16 17 18 22 43 26 45 1 0 1 3 16 17 18 22 43 26 45 1 0 1 3 16 17 18 22 43 26 45 2 1 1 3 16 17 18 22 26 43 45 1 0 1 3 16 17 18 22 26 43 45 9 1
Total Comparisons and swaps First Pass Second Pass Third Pass Total Comparisons 6 12 9 27 Swaps 4 5 1 10