Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. mergeSort(lefthalf)
  2. mergeSort(righthalf)
  3.  
  4. i=0
  5. j=0
  6. k=0
  7. while i<len(lefthalf) and j<len(righthalf):
  8. if lefthalf[i]<righthalf[j]:
  9. alist[k]=lefthalf[i]
  10. i=i+1
  11. else:
  12. alist[k]=righthalf[j]
  13. j=j+1
  14. k=k+1
  15.  
  16. while i<len(lefthalf):
  17. alist[k]=lefthalf[i]
  18. i=i+1
  19. k=k+1
  20.  
  21. while j<len(righthalf):
  22. alist[k]=righthalf[j]
  23. j=j+1
  24. k=k+1
  25. print("Merging ",alist)
  26.  
  27. def quickSortHelper(alist,first,last):
  28. if first<last:
  29.  
  30. splitpoint = partition(alist,first,last)
  31.  
  32. quickSortHelper(alist,first,splitpoint-1)
  33. quickSortHelper(alist,splitpoint+1,last)
  34.  
  35.  
  36. def partition(alist,first,last):
  37. print("sample",alist)
  38. pivotvalue = alist[first]
  39.  
  40. leftmark = first+1
  41. rightmark = last
  42.  
  43. done = False
  44. while not done:
  45.  
  46. while leftmark <= rightmark and
  47. alist[leftmark] <= pivotvalue:
  48. leftmark = leftmark + 1
  49.  
  50. while alist[rightmark] >= pivotvalue and
  51. rightmark >= leftmark:
  52. rightmark = rightmark -1
  53.  
  54. if rightmark < leftmark:
  55. done = True
  56. else:
  57. temp = alist[leftmark]
  58. alist[leftmark] = alist[rightmark]
  59. alist[rightmark] = temp
  60.  
  61. temp = alist[first]
  62. alist[first] = alist[rightmark]
  63. alist[rightmark] = temp
  64. print("partition",alist)
  65.  
  66.  
  67. return rightmark
  68.  
  69. alist = [12,35,67,10,45,29,87,81,34]
  70. quickSort(alist)
  71. print "quick:"
  72. print(alist)
  73.  
  74. alist = [53,21,93,11,70,31,44,58,27]
  75. bubbleSort(alist)
  76. print "bubbleSort:"
  77. print(alist)
  78.  
  79. currentvalue = alist[index]
  80. position = index
  81.  
  82. while position>0 and alist[position-1]>currentvalue:
  83. alist[position]=alist[position-1]
  84. position = position-1
  85.  
  86. alist[position]=currentvalue
  87.  
  88. alist = [54,26,93,17,77,31,44,55,20]
  89. insertionSort(alist)
  90. print("insertion:")
  91. print(alist)
  92.  
  93. temp = alist[fillslot]
  94. alist[fillslot] = alist[positionOfMax]
  95. alist[positionOfMax] = temp
  96.  
  97. alist = [54,26,93,17,77,31,44,55,20]
  98. selectionSort(alist)
  99. print("selection sort:")
  100. print(alist)
  101.  
  102. for startposition in range(sublistcount):
  103. gapInsertionSort(alist,startposition,sublistcount)
  104.  
  105. print("After increments of size",sublistcount,
  106. "The list is",alist)
  107.  
  108. sublistcount = sublistcount // 2
  109. def gapInsertionSort(alist,start,gap):
  110. for i in range(start+gap,len(alist),gap):
  111.  
  112. currentvalue = alist[i]
  113. position = i
  114.  
  115. while position>=gap and alist[position-gap]>currentvalue:
  116. alist[position]=alist[position-gap]
  117. position = position-gap
  118.  
  119. alist[position]=currentvalue
  120.  
  121. alist = [54,26,93,17,77,31,44,55]
  122. shellSort(alist)
  123. print("shell sort")
  124. print(alist)
  125.  
  126. while not maxLength:
  127. maxLength = True
  128.  
  129. buckets = [list() for _ in range( RADIX )]
  130.  
  131.  
  132. for i in aList:
  133. tmp = i / placement
  134. buckets[tmp % RADIX].append( i )
  135. if maxLength and tmp > 0:
  136. maxLength = False
  137.  
  138.  
  139. a = 0
  140. for b in range( RADIX ):
  141. buck = buckets[b]
  142. for i in buck:
  143. aList[a] = i
  144. a += 1
  145.  
  146.  
  147. print("bucket=",buckets,"the list is",alist)
  148. placement *= RADIX
  149. alist = [54,26,93,17,77,31,44,55]
  150. radixsort(alist)
  151. print("radix")
  152. print(alist)
  153.  
  154. for val in alist:
  155. count_alist[val] += 1
  156. print(count_alist)
  157. sorted_alist = []
  158. for i in range(minimum, maximum+1):
  159. if count_alist[i] > 0:
  160. for j in range(0, count_alist[i]):
  161. sorted_alist.append(i)
  162. print(sorted_alist)
  163. return sorted_alist
  164.  
  165. alist = [3,2,-1,1,5,0,10,18,25,25]
  166.  
  167. print (alist)
  168. print("countingsort=")
  169. print count_sort(alist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement