You are on page 1of 5

import math

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

if list1[0] > list2[0]:


list3.append(list2[0])
list2.pop(0)
else:
list3.append(list1[0])
list1.pop(0)

return list3

# takes 1 list then splits it and returns 2 lists


def split(lis):
mid = int(len(lis) / 2)
left = lis[:mid]
right = lis[mid:]
return left, right

# this function is mergesort()


def mergesort(lis):
if len(lis) >= 2:
lis1, lis2 = split(lis)
lis1 = mergesort(lis1)
lis2 = mergesort(lis2)
return(merge(lis1, lis2))
return lis
"""
################################# Number generation
"""

def makerandlist(num, minn, maxx) :


lis = []
for i in range(num):
lis.append(random.randint(minn, maxx))
return lis

def makelistWstep(start, stop, step) :


num = start
list1 = []
while num <= stop:
num += step
list1.append(num)
return list1

# 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))

def gatherdata(minelm, maxelm,elmstep, minrange, maxrange, rangestep) :


mergedata = {}
quickdata = {}

mergetime = 0
quicktime = 0

startTime = 0

elmnums = makelistWstep(minelm, maxelm, elmstep)

rangemaxs = makelistWstep(minrange, maxrange, rangestep)

print(elmnums)
print(rangemaxs)

for num in elmnums:


mergedata[f"Num of Elements:{num}"] = {}
quickdata[f"Num of Elements:{num}"] = {}
for rang in rangemaxs:
list1 = makerandlist(num, minrange, rang)
list2 = makerandlist(num, minrange, rang)
startTime = time.time()
list1 = mergesort(list1)
mergetime = time.time() - startTime
startTime = time.time()
list2 = quicksort(list2)
quicktime = time.time() - startTime

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 "

return mergedata, quickdata

#for key in mergedata.keys():


#print(f"{key}:{mergedata[key]}")

# going to export diagnostics data to a text file!

# number of elements in list : range of values : time taken


# eg 1000Items:{Range100:0.072341234}
quickSortStats = {}

# number of elements in list : range of values : time taken

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

data = list(gatherdata(10, 1000, 10, 0, 100, 50))

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="")

You might also like