Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. # This code demonstrates three different sorting methods:
  2. # bubble sorting, selection sorting, and insertion sorting
  3.  
  4. # Written by Franklin
  5.  
  6. # Import modules to use
  7. import matplotlib as mpl
  8. import matplotlib.pyplot as plt
  9. import random, math
  10.  
  11. # The specific string Agg specifically is used to create .png files
  12. # which is pulled from the mpl.use function
  13. mpl.use('Agg')
  14. # Creates the area the info will be presented
  15. fig = plt.figure()
  16.  
  17.  
  18. # Bubble Method----------------------------------------------------
  19. def bubble(nums):
  20. counter_1 = 0
  21. for i in range(len(nums)):
  22. swap = False
  23. for j in range(len(nums)-i-1):
  24. if nums[j] > nums[j+1]:
  25. counter_1 +=1
  26. swap = True
  27. temp = nums[j]
  28. nums[j] = nums[j+1]
  29. nums[j+1] = temp
  30. if swap == False:
  31. break
  32. return(counter_1)
  33.  
  34.  
  35. # Selection Method-------------------------------------------------
  36. def selection(nums):
  37. counter_2 = 0
  38. for i in range(len(nums)):
  39. for j in range(i + 1, len(nums)):
  40. if nums[i] > nums[j]:
  41. counter_2 +=1
  42. temp = nums[i]
  43. nums[i] = nums[j]
  44. nums[j] = temp
  45. return(counter_2)
  46.  
  47.  
  48.  
  49.  
  50. # Insertion Method-------------------------------------------------
  51. def insertion_sort(nums):
  52. counter_3 = 0
  53. for i in range(1, len(nums)):
  54. values = nums[i]
  55. j = i - 1
  56. while j >=0:
  57. if values < nums[j]:
  58. counter_3 +=1
  59. nums[j+1] = nums[j]
  60. nums[j] = values
  61. j = j - 1
  62. else:
  63. break
  64. return(counter_3)
  65.  
  66. # amount_of_data > 0
  67. amount_of_data = 10
  68. bubble_data = []
  69. selection_data = []
  70. insertion_data = []
  71.  
  72. for i in range(amount_of_data-1, amount_of_data):
  73. random_list = []
  74. for j in range(1, i):
  75. random_list.append(random.randrange(i))
  76.  
  77. x1 = random_list
  78. x2 = random_list
  79. x3 = random_list
  80. print(x1,x2,x3)
  81. bubble_data.append(bubble(x1))
  82. selection_data.append(selection(x2))
  83. print(x1,x2,x3)
  84. insertion_data.append(insertion_sort(x3))
  85.  
  86. print(bubble_data, selection_data, insertion_data)
  87.  
  88. '''
  89. random_1 = []
  90. for i in range(amount_of_data):
  91. x = random.randrange(amount_of_data)
  92. random_1.append(x)
  93. print(bubble(random_1))
  94. print(random_1)
  95.  
  96.  
  97. random_2 = []
  98. for i in range(amount_of_data):
  99. x = random.randrange(amount_of_data)
  100. random_2.append(x)
  101. print(selection(random_2))
  102. print(random_2)
  103.  
  104.  
  105. random_3 = []
  106. for i in range(amount_of_data):
  107. x = random.randrange(amount_of_data)
  108. random_3.append(x)
  109. print(insertion_sort(random_3))
  110. print(random_3)
  111. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement