Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # program to implement shell sort
- def shell(myList):
- gap = int(len(myList)/2)
- # loop for gaps
- while gap > 0:
- # loop till i is in range from gaps to lenght of the list
- for i in range(gap, len(myList)):
- # store the ith element in the temp variable
- temp = myList[i]
- j = i
- # applying insertion sort on the sublist
- # shifting the elements larger than the ith element to the right
- while j >= gap and myList[j - gap] > temp:
- myList[j] = myList[j - gap]
- j -= gap
- # puting the element at its correct position
- myList[j] = temp
- # reduce gap by it half
- gap = int(gap/2)
- return myList
- # main function
- print("Enter the array to be sorted : ")
- myList1 = list(map(int, input().strip().split(',')))
- myList1 = shell(myList1)
- print("The List after applying Shell Sort is : ")
- print(myList1)
- '''
- Enter the array to be sorted :
- 11, 7, 12, 14, 19, 1, 6, 18, 8, 20
- The List after applying Shell Sort is :
- [1, 6, 7, 8, 11, 12, 14, 18, 19, 20]
- Process finished with exit code 0
- '''
Add Comment
Please, Sign In to add comment