Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. def BubbleSort(unsorted):
  5. count = 0
  6. for i in range(len(unsorted)):
  7. for j in range(0,len(unsorted)-i-1):
  8. if unsorted[j] > unsorted[j+1]:
  9. count= count+1
  10. temp = unsorted[j]
  11. unsorted[j] = unsorted[j+1]
  12. unsorted[j+1] = temp
  13. print("Bubble Sorted Values:", unsorted)
  14. print("Bubble Sorted Swaps:",count, "times")
  15.  
  16. def insertionSort(unsorted):
  17. count=0
  18. for index in range(1,len(unsorted)):
  19. currentvalue = unsorted[index]
  20. position = index
  21. while position>0 and unsorted[position-1]>currentvalue:
  22. count = count+1
  23. unsorted[position]=unsorted[position-1]
  24. position = position-1
  25. unsorted[position]=currentvalue
  26. print("Insertion Sorted Values:", unsorted)
  27. print("Insertion Sorted Swaps:", count, "times")
  28.  
  29. def selectionSort(unsorted):
  30. count= 0
  31. for i in range(len(unsorted)):
  32. positionOfMin=i
  33. for j in range(i+1,len(unsorted)):
  34. if unsorted[positionOfMin]>unsorted[j]:
  35. count+=1
  36. positionOfMin = j
  37. temp = unsorted[i]
  38. unsorted[i] = unsorted[positionOfMin]
  39. unsorted[positionOfMin] = temp
  40. print("Selection Sorted Values:", unsorted)
  41. print("Selection Sorted Swaps:", count, "times")
  42.  
  43.  
  44. def main():
  45. array= []
  46. for x in range(0, 50):
  47. array.append(random.randint(0, 50))
  48. print("Arrays Value:", array)
  49. print()
  50. BubbleSort(array)
  51. print()
  52. insertionSort(array)
  53. print()
  54. selectionSort(array)
  55. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement