Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- n = []
- for i in range(int(input("Enter your array length: "))):
- n.append(int(input("Enter some data: ")))
- print(f"Your array is: {n}")
- #Bubble sort
- print("-BUBLE SORT-")
- def bubble_sort():
- swp_count = 0
- for run in range(len(n)-1):
- for x in range(len(n)-1) :
- if n[x] > n[x+1]:
- swp_count += 1
- n[x], n[x+1] = n[x+1],n[x]
- print(f"Your sorted array is: {n}")
- print(f"Sorted {swp_count} times.")
- start_bbl = time.time()
- bubble_sort()
- end_bbl = time.time()
- print(f"Bubble sort was during {end_bbl-start_bbl} seconds.")
- #Selection sort
- print("-SELECTION SORT-")
- def selection_sort():
- for nums in range(len(n)):
- m_num = nums
- for values in range(nums, len(n)):
- if n[m_num] > n[values]:
- m_num = values
- n[nums], n[m_num] = n[m_num], n[nums]
- print(f"Your sorted array is: {n}")
- start_sel = time.time()
- selection_sort()
- end_sel = time.time()
- print(f"Selection sort was during {end_sel-start_sel} seconds.")
- #Insertion sort
- print("-INSERTION SORT-")
- def insertion_sort():
- for i in range(len(n)):
- start_num = n[i]
- pos = i
- while pos > 0 and n[i - 1] > start_num:
- n[pos] = n[pos - 1]
- pos -= 1
- print(f"Your sorted array is: {n}")
- start_ins = time.time()
- insertion_sort()
- end_ins = time.time()
- print(f"Insertion sort was during {end_ins-start_ins} seconds.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement