Advertisement
AlfonsoPEREZ

cs shit

Dec 10th, 2023 (edited)
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import random, time, sys
  2.  
  3.  
  4. random_numbers = []
  5.  
  6. def bubble_sort(lst):
  7.   for i in range(0, len(lst)-1):
  8.     for j in range(0, len(lst)-1):
  9.       if lst[j] > lst[j + 1]:
  10.         temp = lst[j]
  11.         lst[j] = lst[j + 1]
  12.         lst[j + 1] = temp
  13.  
  14.   return(lst)
  15.  
  16. def insertion_sort(lst):
  17.   length = range(1, len(lst))
  18.   for i in length:
  19.     value = lst[i]
  20.  
  21.     while lst[i-1] > value and i > 0:
  22.       lst[i], lst[i - 1] = lst[i - 1], lst[i]
  23.       i = i - 1
  24.  
  25.   return(lst)
  26.  
  27. def quick_sort(lst):
  28.   length = len(lst)
  29.   if length <= 1:
  30.     return lst
  31.   else:
  32.     pivot = lst.pop()
  33.  
  34.   greater = []
  35.   lower = []
  36.  
  37.   for i in lst:
  38.     if i > pivot:
  39.       greater.append(i)
  40.  
  41.     else:
  42.       lower.append(i)
  43.  
  44.   return quick_sort(lower) + [pivot] + quick_sort(greater)
  45.  
  46.  
  47. start1 = time.time()
  48. (bubble_sort(random_numbers))
  49. end1 = time.time()
  50. print("Bubble Sort Time:", end1-start1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement