m2skills

Shell py

Apr 4th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. # program to implement shell sort
  2.  
  3. def shell(myList):
  4.     gap = int(len(myList)/2)
  5.    
  6.     # loop for gaps
  7.     while gap > 0:
  8.         # loop till i is in range from gaps to lenght of the list
  9.         for i in range(gap, len(myList)):
  10.             # store the ith element in the temp variable
  11.             temp = myList[i]
  12.             j = i
  13.             # applying insertion sort on the sublist
  14.             # shifting the elements larger than the ith element to the right
  15.             while j >= gap and myList[j - gap] > temp:
  16.                 myList[j] = myList[j - gap]
  17.                 j -= gap
  18.             # puting the element at its correct position   
  19.             myList[j] = temp
  20.         # reduce gap by it half
  21.         gap = int(gap/2)
  22.     return myList
  23.    
  24.    
  25. # main function
  26. print("Enter the array to be sorted : ")
  27. myList1 = list(map(int, input().strip().split(',')))
  28.  
  29. myList1 = shell(myList1)
  30. print("The List after applying Shell Sort is : ")
  31. print(myList1)
  32.  
  33. '''
  34. Enter the array to be sorted :
  35. 11, 7, 12, 14, 19, 1, 6, 18, 8, 20
  36. The List after applying Shell Sort is :
  37. [1, 6, 7, 8, 11, 12, 14, 18, 19, 20]
  38.  
  39. Process finished with exit code 0
  40. '''
Add Comment
Please, Sign In to add comment