Question:
Write a program to perform shell sort on [85,63,0,12,47,96,52]
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
def shellSort (numbers,array_size): increment = array_size / 2; while (increment > 0): for i in range (0,array_size): j = i; temp = numbers[i]; while ((j >= increment) and (numbers[j-increment] > temp)): numbers[j] = numbers[j - increment]; j = j - increment; numbers[j] = temp; if (increment == 2): increment = 1; else : increment = increment * 5 / 11; numbers=[85,63,0,12,47,96,52] array_size=len(numbers) shellSort ( numbers,array_size) print numbers |
Explanation:
Step 1 − Initialize the value of h
Step 2 − Divide the list into smaller sub-list of equal interval h
Step 3 − Sort these sub-lists using insertion sort
Step 4 − Repeat until complete list is sorted
Output:
1 |
[0, 12, 47, 52, 63, 85, 96] |
Leave a Reply