1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
import time
import numpy as np
import matplotlib.pyplot as plt
import random
L = [10, 20, 30, 0, 1, 2, 1, 0, 100]
def swap(L, p1, p2):
L[p1], L[p2] = L[p2], L[p1]
return L
print(swap(L, 0, 2))
def find_max(L, p1, p2):
m = p1
for i in range(p1, p2 + 1):
if (L[i] > L[m]):
m = i
return m
print(find_max(L, 3, 5))
def random_list(n, min, max):
L = []
for i in range(n):
L.append(random.randint(min, max))
return L
print(random_list(5, 0, 10))
def selection_sort(L):
len(L)
for i in range(0, len(L)-1):
pO = find_max(L,i,len(L)-1)
L = swap(L,i,pO)
return L
print(selection_sort(L))
def bubble_sort(L):
for i in range(0,len(L)-1):
if L[i] < L[i+1]:
L = swap(L,[i+1], [i])
return L
print(bubble_sort(L))
def selection_sort(L):
len(L)
for i in range(0, len(L)-1):
pO = find_max(L,i,len(L)-1)
L = swap(L,i,pO)
return L
n = []
for i in range(2000):
n.append(random.randint(0,2000))
def test_selection(n):
start=time.time()
selection_sort(n)
total_time = time.time() - start
return total_time
print(test_selection(n))
def test_bubble(n):
start=time.time()
bubble_sort(n)
total_time = time.time()-start
return total_time
print(test_selection(n))
def test_python(n):
start = time.time()
n.sort(reverse=True)
total_time=time.time()-start
return total_time
print(test_python(n))
plt.ylim(0,0.5)
plt.xlim(0,2000)
y1 = test_bubble([n])
x = [n]
plt.plot(x,y1)
plt.show() |
Partager