Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__ = 'AneoPsy'
- from random import randint
- from time import time
- array = [randint (1,100) for _ in range(1000)]
- def bubbleSort(array):
- n = len(array)
- switch = True
- while switch == True:
- switch = False
- for j in range(0, n-1):
- if array[j] > array[j+1]:
- switch, array[j], array[j+1] = True, array[j+1], array[j]
- return array
- def insertionSort(array):
- for j in range(1, len(array)):
- i = j-1
- var = array[j]
- while i > -1 and array[i] > var:
- array[i+1] = array[i]
- i -= 1
- array[i+1] = var
- return array
- def selectSort(array):
- for k in range(0, len(array)):
- mini = k #min(array[1:])
- for l in range(k, len(array)):
- if(array[l] < array[mini]):
- mini = l
- if mini is not k:
- array[k], array[mini] = array[mini], array[k]
- return array
- def quickSort(array):
- if array == []:
- return []
- else:
- pivot = array[0]
- lesser = quickSort([x for x in array[1:] if x < pivot])
- greater = quickSort([x for x in array[1:] if x >= pivot])
- return lesser + [pivot] + greater
- def testSort():
- timer = [[], [], [], []]
- result = [[], [], [], []]
- for value in range(0,1):
- array = [randint (1,100) for _ in range(100)]
- start = time()
- result[0] = quickSort(list(array))
- end = time()
- timer[0].append(end-start)
- print("qSort %d: %0.10fs" %(value, end-start))
- start = time()
- result[1] = bubbleSort(list(array))
- end = time()
- timer[1].append(end-start)
- print("BubbleSort %d: %0.10fs" %(value, end-start))
- start = time()
- result[2] = insertionSort(list(array))
- end = time()
- timer[2].append(end-start)
- print("insertionSort %d: %0.10fs" %(value, end-start))
- start = time()
- result[3] = selectSort(list(array))
- end = time()
- timer[3].append(end-start)
- print("selectionSort %d: %0.10fs" %(value, end-start))
- testSort()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement