Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import datetime
- def quick_sort(arr):
- if len(arr) < 2:
- return arr
- el = arr[len(arr) // 3]
- left = [x for x in arr if x < el]
- mid = [x for x in arr if x == el]
- right = [x for x in arr if x > el]
- return quick_sort(left) + mid + quick_sort(right)
- def timer(function):
- def wrapper(*args, **kwargs):
- now = datetime.now()
- res = function(*args, **kwargs)
- print(f"time of func is {datetime.now() - now}")
- return wrapper
- @timer
- def bubble_sort(arr):
- for i in range(len(arr)):
- for j in range(len(arr) - 1):
- if arr[j] > arr[j + 1]:
- arr[j], arr[j + 1] = arr[j + 1], arr[j]
- return arr
- @timer
- def insertion_sort(arr):
- for i in range(len(arr) - 1):
- j = i + 1
- while j > -1 and arr[j - 1] > arr[j]:
- arr[j - 1], arr[j] = arr[j], arr[j - 1]
- j -= 1
- return arr
- arr = [1, 5, 2, 3, 4, 6, 2, 1] * 1000
- # j, j + 1
- print(bubble_sort(arr))
- now = datetime.now()
- quick_sort(arr)
- print(f"time of func {datetime.now() - now}")
- print(insertion_sort(arr))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement