Professional Documents
Culture Documents
import random
import time
import sys
import pygame
# enter starting from 0 and the result is relevent to starting from zero
"""
###################################### Quick sort
"""
def sortAroundMidpoint(lis):
midP = math.ceil(((len(lis) + 1)/2)-1)
mid = lis[midP]
greater = []
lesser = []
for i in range(len(lis)):
if lis[i] > mid:
greater.append(lis[i])
elif lis[i] < mid:
lesser.append(lis[i])
elif lis[i] == mid:
if i > midP:
greater.append(lis[i])
elif i < midP:
lesser.append(lis[i])
else:
pass
return [lesser, mid, greater]
def mainSort(lis):
complete = False
lis = [lis]
newList = []
while not complete:
complete = True
for i in range(len(lis)):
if type(lis[i]) == list and len(lis[i]) > 1:
for i in sortAroundMidpoint(lis[i]):
newList.append(i)
complete = False
else:
newList.append(lis[i])
lis = newList
newList = []
return lis
def reFormatSorted(lis):
newList = []
for i in lis:
if i == []:
pass
elif isinstance(i, list):
newList.append(i[0])
else:
newList.append(i)
return newList
def quicksort(list1):
return reFormatSorted(mainSort(list1))
"""
#########################################Merge Sort
"""
# iterative algorithm
def merge(list1, list2):
# this is how many comparisons I will have to make
num = len(list1) + len(list2)
list3 = []
# repeats for the sum of the elements in both thists
for i in range(num):
if len(list1) == 0:
for i in list2:
list3.append(i)
return list3
elif len(list2) == 0:
for i in list1:
list3.append(i)
return list3
return list3
# starts with start values then for each iteration increases it by num ** exp so 2
* (2**2) = 8 --> num = 8 * (2**2) = 32 --> Num = 32 ect
def makelistWexponent(start, exp, num, stop):
lis = []
while start < stop:
lis.append(start)
start *= (num**exp)
return lis
print(makelistWexponent(2,2,2,1000))
# makes lists between 2 exponents so between 5 and 5*(10**2) = 500 with step 50 so
50,100,150...500
def makelistWboth(start, stop, step, num, exp):
lis = makelistWexponent(start,exp,num, stop)
lis2 = []
for i in range(len(lis)-1):
lis2.append(makelistWstep(lis[i],lis[i+1], step))
mergetime = 0
quicktime = 0
startTime = 0
print(elmnums)
print(rangemaxs)
mergedata[f"Num of Elements:{num}"][f"Range:{minrange}-{rang}"] =
f"{mergetime}s "
quickdata[f"Num of Elements:{num}"][f"Range:{minrange}-{rang}"] =
f"{mergetime}s "
mergeSortStats = {}
list1 = []
random.seed(500)
list1 = makerandlist(10000, -1000, 1000)
random.seed(500)
list2 = makerandlist(10000, -1000, 1000)
startTime = time.time()
print(quicksort(list1))
print("Quick sort took: {} ms to run".format((time.time()-startTime)))
startTime = time.time()
print(mergesort(list2))
print("Merge sort took: {} ms to run".format((time.time()-startTime)))
list1 = [53,34,56,12,32,34,43,54,23,43,87,32,12]
#print(quicksort(list1))
# what the function does it generate a list (Number of elements in the list 10 -
1000) then the range of items eg randint(0,50) - randint(0,150)
# Then it passes it through both merge and quick sort and records how long it takes
- basically its gathering time data
# a little note sometimes it says time = 0 im not sure what this is about
for i in data:
print("", end="\n\n\n\n\n")
for i2 in i.keys():
print("\n{}: ".format(i2))
for i3 in i[i2].keys():
print("{} : {}".format(i3, i[i2][i3]), end="")