Advertisement
marfach

sort algoritm

Sep 28th, 2022 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | Software | 0 0
  1. import time
  2.  
  3.    
  4. n = []
  5. for i in range(int(input("Enter your array length: "))):
  6.     n.append(int(input("Enter some data: ")))
  7. print(f"Your array is: {n}")
  8.  
  9. #Bubble sort
  10. print("-BUBLE SORT-")
  11. def bubble_sort():
  12.     swp_count = 0
  13.     for run in range(len(n)-1):
  14.         for x in range(len(n)-1) :
  15.             if n[x] > n[x+1]:
  16.                 swp_count += 1
  17.                 n[x], n[x+1] = n[x+1],n[x]
  18.         print(f"Your sorted array is: {n}")
  19.     print(f"Sorted {swp_count} times.")
  20.  
  21. start_bbl = time.time()
  22. bubble_sort()
  23. end_bbl = time.time()
  24. print(f"Bubble sort was during {end_bbl-start_bbl} seconds.")
  25.  
  26. #Selection sort
  27. print("-SELECTION SORT-")
  28. def selection_sort():
  29.     for nums in range(len(n)):
  30.         m_num = nums
  31.         for values in  range(nums, len(n)):
  32.             if n[m_num] > n[values]:
  33.                 m_num = values
  34.         n[nums], n[m_num] = n[m_num], n[nums]
  35.     print(f"Your sorted array is: {n}")
  36.  
  37. start_sel = time.time()
  38. selection_sort()
  39. end_sel = time.time()
  40. print(f"Selection sort was during {end_sel-start_sel} seconds.")
  41.  
  42. #Insertion sort
  43. print("-INSERTION SORT-")
  44. def insertion_sort():
  45.     for i in range(len(n)):
  46.         start_num = n[i]
  47.         pos = i
  48.         while pos > 0 and n[i - 1] > start_num:
  49.             n[pos] = n[pos - 1]
  50.             pos -= 1
  51.         print(f"Your sorted array is: {n}")
  52.  
  53. start_ins = time.time()
  54. insertion_sort()
  55. end_ins = time.time()
  56. print(f"Insertion sort was during {end_ins-start_ins} seconds.")
  57.  
  58.  
  59.  
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement