Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This code demonstrates three different sorting methods:
- # bubble sorting, selection sorting, and insertion sorting
- # Written by Franklin
- # Import modules to use
- import matplotlib as mpl
- import matplotlib.pyplot as plt
- import random, math
- # The specific string Agg specifically is used to create .png files
- # which is pulled from the mpl.use function
- mpl.use('Agg')
- # Creates the area the info will be presented
- fig = plt.figure()
- # Bubble Method----------------------------------------------------
- def bubble(nums):
- counter_1 = 0
- for i in range(len(nums)):
- swap = False
- for j in range(len(nums)-i-1):
- if nums[j] > nums[j+1]:
- counter_1 +=1
- swap = True
- temp = nums[j]
- nums[j] = nums[j+1]
- nums[j+1] = temp
- if swap == False:
- break
- return(counter_1)
- # Selection Method-------------------------------------------------
- def selection(nums):
- counter_2 = 0
- for i in range(len(nums)):
- for j in range(i + 1, len(nums)):
- if nums[i] > nums[j]:
- counter_2 +=1
- temp = nums[i]
- nums[i] = nums[j]
- nums[j] = temp
- return(counter_2)
- # Insertion Method-------------------------------------------------
- def insertion_sort(nums):
- counter_3 = 0
- for i in range(1, len(nums)):
- values = nums[i]
- j = i - 1
- while j >=0:
- if values < nums[j]:
- counter_3 +=1
- nums[j+1] = nums[j]
- nums[j] = values
- j = j - 1
- else:
- break
- return(counter_3)
- # amount_of_data > 0
- amount_of_data = 10
- bubble_data = []
- selection_data = []
- insertion_data = []
- for i in range(amount_of_data-1, amount_of_data):
- random_list = []
- for j in range(1, i):
- random_list.append(random.randrange(i))
- x1 = random_list
- x2 = random_list
- x3 = random_list
- print(x1,x2,x3)
- bubble_data.append(bubble(x1))
- selection_data.append(selection(x2))
- print(x1,x2,x3)
- insertion_data.append(insertion_sort(x3))
- print(bubble_data, selection_data, insertion_data)
- '''
- random_1 = []
- for i in range(amount_of_data):
- x = random.randrange(amount_of_data)
- random_1.append(x)
- print(bubble(random_1))
- print(random_1)
- random_2 = []
- for i in range(amount_of_data):
- x = random.randrange(amount_of_data)
- random_2.append(x)
- print(selection(random_2))
- print(random_2)
- random_3 = []
- for i in range(amount_of_data):
- x = random.randrange(amount_of_data)
- random_3.append(x)
- print(insertion_sort(random_3))
- print(random_3)
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement