Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def BubbleSort(unsorted):
- count = 0
- for i in range(len(unsorted)):
- for j in range(0,len(unsorted)-i-1):
- if unsorted[j] > unsorted[j+1]:
- count= count+1
- temp = unsorted[j]
- unsorted[j] = unsorted[j+1]
- unsorted[j+1] = temp
- print("Bubble Sorted Values:", unsorted)
- print("Bubble Sorted Swaps:",count, "times")
- def insertionSort(unsorted):
- count=0
- for index in range(1,len(unsorted)):
- currentvalue = unsorted[index]
- position = index
- while position>0 and unsorted[position-1]>currentvalue:
- count = count+1
- unsorted[position]=unsorted[position-1]
- position = position-1
- unsorted[position]=currentvalue
- print("Insertion Sorted Values:", unsorted)
- print("Insertion Sorted Swaps:", count, "times")
- def selectionSort(unsorted):
- count= 0
- for i in range(len(unsorted)):
- positionOfMin=i
- for j in range(i+1,len(unsorted)):
- if unsorted[positionOfMin]>unsorted[j]:
- count+=1
- positionOfMin = j
- temp = unsorted[i]
- unsorted[i] = unsorted[positionOfMin]
- unsorted[positionOfMin] = temp
- print("Selection Sorted Values:", unsorted)
- print("Selection Sorted Swaps:", count, "times")
- def main():
- array= []
- for x in range(0, 50):
- array.append(random.randint(0, 50))
- print("Arrays Value:", array)
- print()
- BubbleSort(array)
- print()
- insertionSort(array)
- print()
- selectionSort(array)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement