Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- def bomble(lista):
- start_b = time.time()
- bez_zmiany = False
- while not bez_zmiany:
- for i in range(len(lista)-1):
- if lista[i] > lista[i+1]:
- lista[i], lista[i+1] = lista[i+1], lista[i]
- break
- if i == len(lista)-2:
- bez_zmiany = True
- break
- stop_b = time.time()
- print(f'Babelkowe: {(stop_b - start_b):.4f}s')
- def wybor(lista):
- start_w = time.time()
- for i in range(len(lista)):
- index = lista.index(min(lista[i:]))
- lista[index], lista[i] = lista[i], lista[index]
- stop_w = time.time()
- print(f'Wybor: {(stop_w - start_w):.4f}s')
- def quicksort(lista, start, stop):
- if start >= stop-1:
- return
- pivot = int((stop - start) / 2) + start
- i = start
- j = stop
- while i < j:
- if lista[i] >= lista[pivot] >= lista[j]:
- lista[i], lista[j] = lista[j], lista[i]
- j -= 1
- i += 1
- quicksort(lista, start, j)
- quicksort(lista, j+1, stop)
- def quick(lista):
- start_q = time.time()
- quicksort(lista, 0, len(lista)-1)
- stop_q = time.time()
- print(f'Quicksort: {(stop_q - start_q):.4f}s')
- if __name__ == "__main__":
- rnd = random.sample(range(5000), 20)
- rnd_b = rnd.copy()
- rnd_w = rnd.copy()
- rnd_q = rnd.copy()
- # bomble(rnd_b)
- wybor(rnd_w)
- quick(rnd_q)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement