Add comments
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
array = [24, 10, 40, 28, 31, 9, 29, 42, 39, 19, 18, 37, 11, 14, 49, 47, 22, 16, 38, 6, 4, 27, 21, 48, 35, 23, 17, 1, 44, 25, 41, 2, 46, 30, 8, 50, 32, 5, 20, 33, 34, 26, 36, 12, 43, 15, 45, 7, 3, 13]
|
# selective sort algorithm
|
||||||
print("Original array:", array)
|
|
||||||
for i in range(0, len(array)):
|
# list to be sorted
|
||||||
|
original = [24, 10, 40, 28, 31, 9, 29, 42, 39, 19, 18, 37, 11, 14, 49, 47, 22, 16, 38, 6, 4, 27, 21, 48, 35, 23, 17, 1, 44, 25, 41, 2, 46, 30, 8, 50, 32, 5, 20, 33, 34, 26, 36, 12, 43, 15, 45, 7, 3, 13]
|
||||||
|
print("Original list:", original)
|
||||||
|
|
||||||
|
for i in range(0, len(original)):
|
||||||
j = 0
|
j = 0
|
||||||
while array[i] > array[j]:
|
while original[i] > original[j]:
|
||||||
j += 1
|
j += 1
|
||||||
array.insert(j, array.pop(i))
|
original.insert(j, original.pop(i))
|
||||||
print("New array:", array)
|
print("New list:", original)
|
||||||
@@ -1,12 +1,16 @@
|
|||||||
|
# selective sort algorithm
|
||||||
|
|
||||||
|
# list to be sorted
|
||||||
original = [24, 10, 40, 28, 31, 9, 29, 42, 39, 19, 18, 37, 11, 14, 49, 47, 22, 16, 38, 6, 4, 27, 21, 48, 35, 23, 17, 1, 44, 25, 41, 2, 46, 30, 8, 50, 32, 5, 20, 33, 34, 26, 36, 12, 43, 15, 45, 7, 3, 13]
|
original = [24, 10, 40, 28, 31, 9, 29, 42, 39, 19, 18, 37, 11, 14, 49, 47, 22, 16, 38, 6, 4, 27, 21, 48, 35, 23, 17, 1, 44, 25, 41, 2, 46, 30, 8, 50, 32, 5, 20, 33, 34, 26, 36, 12, 43, 15, 45, 7, 3, 13]
|
||||||
new = []
|
new = []
|
||||||
print("Original list:", original)
|
print("Original list:", original)
|
||||||
|
# sort through the list, finding the smallest number each time
|
||||||
for x in range(0, len(original)):
|
for x in range(0, len(original)):
|
||||||
for i, item in enumerate(original):
|
for i, item in enumerate(original):
|
||||||
if i == 0:
|
if i == 0:
|
||||||
lowest = 0
|
lowest = 0
|
||||||
elif original[i] < original[lowest]:
|
elif original[i] < original[lowest]:
|
||||||
lowest = i
|
lowest = i
|
||||||
new.append(original[lowest])
|
# remove the smallest number from the original list & add it to the new list
|
||||||
original.pop(lowest)
|
new.append(original.pop(lowest))
|
||||||
print("New list:", new)
|
print("New list:", new)
|
||||||
@@ -2,23 +2,28 @@ import pygame, time
|
|||||||
|
|
||||||
finished = False
|
finished = False
|
||||||
|
|
||||||
|
# define function for updating the display contents
|
||||||
def updateDisplay():
|
def updateDisplay():
|
||||||
|
# show the reset button if the sorting has completed
|
||||||
if finished:
|
if finished:
|
||||||
gameDisplay.blit(buttonReset, buttonResetRect)
|
gameDisplay.blit(buttonReset, buttonResetRect)
|
||||||
|
# if the sorting hasn't completed
|
||||||
else:
|
else:
|
||||||
# Set the background colour
|
# wipe the screen
|
||||||
gameDisplay.fill(black)
|
gameDisplay.fill(black)
|
||||||
|
|
||||||
|
# if the sorting algorithm to be used has been been defined as 1, 2 or 3
|
||||||
if sortingAlgorithm != 0:
|
if sortingAlgorithm != 0:
|
||||||
|
# draw one rect object for each item in the list
|
||||||
for loop in range(0,len(numberList)):
|
for loop in range(0,len(numberList)):
|
||||||
# Place other objects to display below
|
pygame.draw.rect(gameDisplay, colourList[numberList[loop]-1], [loop*20, 90, 20, 20])
|
||||||
pygame.draw.rect(gameDisplay, colourList[numberList[loop]-1], [loop*20, 90, 20, 20]) # rect in [] list x,y,w,h 0,0 is top left
|
# display current time taken in top left
|
||||||
# display current time taken
|
|
||||||
timeText = font.render("{:.1f} seconds".format(time.time() - startTime), True, white)
|
timeText = font.render("{:.1f} seconds".format(time.time() - startTime), True, white)
|
||||||
timeTextRect = timeText.get_rect()
|
timeTextRect = timeText.get_rect()
|
||||||
timeTextRect.top = 10
|
timeTextRect.top = 10
|
||||||
timeTextRect.left = 10
|
timeTextRect.left = 10
|
||||||
gameDisplay.blit(timeText, timeTextRect)
|
gameDisplay.blit(timeText, timeTextRect)
|
||||||
|
# if the sorting algorithm hasn't yet been chosen, show the buttons for doing so
|
||||||
else:
|
else:
|
||||||
gameDisplay.blit(buttonSelective, buttonSelectiveRect)
|
gameDisplay.blit(buttonSelective, buttonSelectiveRect)
|
||||||
gameDisplay.blit(buttonInsertion, buttonInsertionRect)
|
gameDisplay.blit(buttonInsertion, buttonInsertionRect)
|
||||||
@@ -26,11 +31,14 @@ def updateDisplay():
|
|||||||
|
|
||||||
# redisplay window
|
# redisplay window
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
|
# adjust for desired framerate
|
||||||
clock.tick(fps)
|
clock.tick(fps)
|
||||||
|
|
||||||
|
# define function for checking whether the user wants to exit the program
|
||||||
def checkQuit():
|
def checkQuit():
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
# allow safe exit
|
# allow exit using window exit button (the red cross)
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
quit()
|
quit()
|
||||||
@@ -41,23 +49,27 @@ def checkQuit():
|
|||||||
pygame.quit()
|
pygame.quit()
|
||||||
quit()
|
quit()
|
||||||
|
|
||||||
|
# define function for checking if any start buttons have been pressed
|
||||||
def checkStartButtons():
|
def checkStartButtons():
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
# checks if mouse position is over any start buttons
|
# checks if mouse position is over selective sort button
|
||||||
if buttonSelectiveRect.collidepoint(event.pos):
|
if buttonSelectiveRect.collidepoint(event.pos):
|
||||||
# prints current location of mouse
|
# prints current location of mouse
|
||||||
print('Selective button was pressed at {0}'.format(event.pos))
|
print('Selective button was pressed at {0}'.format(event.pos))
|
||||||
return 1
|
return 1
|
||||||
|
# checks if mouse position is over insertion sort button
|
||||||
elif buttonInsertionRect.collidepoint(event.pos):
|
elif buttonInsertionRect.collidepoint(event.pos):
|
||||||
# prints current location of mouse
|
# prints current location of mouse
|
||||||
print('Insertion button was pressed at {0}'.format(event.pos))
|
print('Insertion button was pressed at {0}'.format(event.pos))
|
||||||
return 2
|
return 2
|
||||||
|
# checks if mouse position is over bubble sort button
|
||||||
elif buttonBubbleRect.collidepoint(event.pos):
|
elif buttonBubbleRect.collidepoint(event.pos):
|
||||||
# prints current location of mouse
|
# prints current location of mouse
|
||||||
print('Bubble button was pressed at {0}'.format(event.pos))
|
print('Bubble button was pressed at {0}'.format(event.pos))
|
||||||
return 3
|
return 3
|
||||||
|
|
||||||
|
# define function for checking if the reset button has been pressed
|
||||||
def checkResetButton():
|
def checkResetButton():
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
@@ -65,20 +77,19 @@ def checkResetButton():
|
|||||||
if buttonResetRect.collidepoint(event.pos):
|
if buttonResetRect.collidepoint(event.pos):
|
||||||
# prints current location of mouse
|
# prints current location of mouse
|
||||||
print('Reset button was pressed at {0}'.format(event.pos))
|
print('Reset button was pressed at {0}'.format(event.pos))
|
||||||
|
# returns value for use in the variable finished
|
||||||
return False
|
return False
|
||||||
|
# returns value for use in the variable finished
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def resetGame():
|
|
||||||
numberList = originalNumberList
|
|
||||||
|
|
||||||
# initialise PyGame
|
# initialise PyGame
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
|
# define font for usage with text
|
||||||
font = pygame.font.Font("Roboto-Regular.ttf", 32)
|
font = pygame.font.Font("Roboto-Regular.ttf", 32)
|
||||||
|
# define some basic colours, for easier referencing later on
|
||||||
black = (0, 0, 0)
|
black = (0, 0, 0)
|
||||||
white = (255, 255, 255)
|
white = (255, 255, 255)
|
||||||
green = (0, 255, 0)
|
|
||||||
blue = (0, 0, 128)
|
|
||||||
|
|
||||||
# define clock, to allow framerate alteration
|
# define clock, to allow framerate alteration
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
@@ -86,15 +97,16 @@ clock = pygame.time.Clock()
|
|||||||
# list of colours, in colour order
|
# list of colours, in colour order
|
||||||
colourList = [(239, 69, 69), (239, 86, 69), (239, 103, 69), (239, 120, 69), (239, 137, 69), (239, 154, 69), (239, 171, 69), (239, 188, 69), (239, 205, 69), (239, 222, 69), (239, 239, 69), (222, 239, 69), (205, 239, 69), (188, 239, 69), (171, 239, 69), (154, 239, 69), (137, 239, 69), (120, 239, 69), (103, 239, 69), (86, 239, 69), (69, 239, 69), (69, 239, 86), (69, 239, 103), (69, 239, 120), (69, 239, 137), (69, 239, 154), (69, 239, 171), (69, 239, 188), (69, 239, 205), (69, 239, 222), (69, 239, 239), (69, 222, 239), (69, 205, 239), (69, 188, 239), (69, 171, 239), (69, 154, 239), (69, 137, 239), (69, 120, 239), (69, 103, 239), (69, 86, 239), (69, 69, 239), (86, 69, 239), (103, 69, 239), (120, 69, 239), (137, 69, 239), (154, 69, 239), (171, 69, 239), (188, 69, 239), (205, 69, 239), (222, 69, 239), (239, 69, 239), (239, 69, 222), (239, 69, 205), (239, 69, 188), (239, 69, 171), (239, 69, 154), (239, 69, 137), (239, 69, 120), (239, 69, 103), (239, 69, 86)]
|
colourList = [(239, 69, 69), (239, 86, 69), (239, 103, 69), (239, 120, 69), (239, 137, 69), (239, 154, 69), (239, 171, 69), (239, 188, 69), (239, 205, 69), (239, 222, 69), (239, 239, 69), (222, 239, 69), (205, 239, 69), (188, 239, 69), (171, 239, 69), (154, 239, 69), (137, 239, 69), (120, 239, 69), (103, 239, 69), (86, 239, 69), (69, 239, 69), (69, 239, 86), (69, 239, 103), (69, 239, 120), (69, 239, 137), (69, 239, 154), (69, 239, 171), (69, 239, 188), (69, 239, 205), (69, 239, 222), (69, 239, 239), (69, 222, 239), (69, 205, 239), (69, 188, 239), (69, 171, 239), (69, 154, 239), (69, 137, 239), (69, 120, 239), (69, 103, 239), (69, 86, 239), (69, 69, 239), (86, 69, 239), (103, 69, 239), (120, 69, 239), (137, 69, 239), (154, 69, 239), (171, 69, 239), (188, 69, 239), (205, 69, 239), (222, 69, 239), (239, 69, 239), (239, 69, 222), (239, 69, 205), (239, 69, 188), (239, 69, 171), (239, 69, 154), (239, 69, 137), (239, 69, 120), (239, 69, 103), (239, 69, 86)]
|
||||||
# list of numbers to be sorted
|
# list of numbers to be sorted
|
||||||
|
# two lists are used so that the original one is retained whilst the other is altered, allowing resetting at the end of the program
|
||||||
originalNumberList = [30,4,5,6,36,38,37,32,17,19,8,10,56,50,20,13,14,40,58,21,43,51,1,31,41,16,60,22,34,28,9,12,42,2,27,18,26,47,39,24,57,23,46,53,15,25,7,33,49,59,35,44,45,11,54,29,55,52,3,48]
|
originalNumberList = [30,4,5,6,36,38,37,32,17,19,8,10,56,50,20,13,14,40,58,21,43,51,1,31,41,16,60,22,34,28,9,12,42,2,27,18,26,47,39,24,57,23,46,53,15,25,7,33,49,59,35,44,45,11,54,29,55,52,3,48]
|
||||||
numberList = originalNumberList.copy()
|
numberList = originalNumberList.copy()
|
||||||
|
|
||||||
# frame rate of sorting visualisation
|
# frame rate of sorting visualisation
|
||||||
fps = 25
|
fps = 25
|
||||||
|
|
||||||
|
# define which sorting algorithm to use (1, 2 and 3 are the only valid options, for selective, insertion and bubble, respectively)
|
||||||
sortingAlgorithm = 0
|
sortingAlgorithm = 0
|
||||||
|
|
||||||
|
|
||||||
# define height and width of display
|
# define height and width of display
|
||||||
display_width = len(numberList)*20
|
display_width = len(numberList)*20
|
||||||
display_height = 200
|
display_height = 200
|
||||||
@@ -124,65 +136,97 @@ buttonReset = font.render("Reset", True, white)
|
|||||||
buttonResetRect = buttonBubble.get_rect()
|
buttonResetRect = buttonBubble.get_rect()
|
||||||
buttonResetRect.center = (display_width / 2, display_height / 2 + 60)
|
buttonResetRect.center = (display_width / 2, display_height / 2 + 60)
|
||||||
|
|
||||||
|
# update display contents
|
||||||
updateDisplay()
|
updateDisplay()
|
||||||
|
|
||||||
|
# run until manual exit
|
||||||
while True:
|
while True:
|
||||||
|
# selection sort
|
||||||
if sortingAlgorithm == 1:
|
if sortingAlgorithm == 1:
|
||||||
|
# set start time, for use with on-screen timer
|
||||||
startTime = time.time()
|
startTime = time.time()
|
||||||
# selection sort
|
|
||||||
swapPosition = 0
|
swapPosition = 0
|
||||||
swapData = 0
|
swapData = 0
|
||||||
|
# find the smallest number in the list until it is fully sorted
|
||||||
for i in range(0, len(numberList)):
|
for i in range(0, len(numberList)):
|
||||||
swapPosition = i
|
swapPosition = i
|
||||||
for j in range(i, len(numberList)):
|
for j in range(i, len(numberList)):
|
||||||
# find the smallest number
|
# find the smallest number
|
||||||
if numberList[j] < numberList[swapPosition]:
|
if numberList[j] < numberList[swapPosition]:
|
||||||
swapPosition = j
|
swapPosition = j
|
||||||
|
# swap the smallest number with the number in its new position
|
||||||
swapData = numberList[i]
|
swapData = numberList[i]
|
||||||
numberList[i] = numberList[swapPosition]
|
numberList[i] = numberList[swapPosition]
|
||||||
numberList[swapPosition] = swapData
|
numberList[swapPosition] = swapData
|
||||||
|
|
||||||
|
# check if the user wants to quit
|
||||||
checkQuit()
|
checkQuit()
|
||||||
|
# update display contents
|
||||||
updateDisplay()
|
updateDisplay()
|
||||||
|
# record that the sorting has completed
|
||||||
finished = True
|
finished = True
|
||||||
sortingAlgorithm = 0
|
sortingAlgorithm = 0
|
||||||
|
# insertion sort
|
||||||
elif sortingAlgorithm == 2:
|
elif sortingAlgorithm == 2:
|
||||||
|
# set start time, for use with on-screen timer
|
||||||
startTime = time.time()
|
startTime = time.time()
|
||||||
# insertion sort
|
# for each item in the list
|
||||||
for i in range(0, len(numberList)):
|
for i in range(0, len(numberList)):
|
||||||
j = 0
|
j = 0
|
||||||
|
# find its correct position
|
||||||
while numberList[i] > numberList[j]:
|
while numberList[i] > numberList[j]:
|
||||||
j += 1
|
j += 1
|
||||||
|
# put it in its correct position in the list
|
||||||
numberList.insert(j, numberList.pop(i))
|
numberList.insert(j, numberList.pop(i))
|
||||||
|
# check if the user wants to quit
|
||||||
checkQuit()
|
checkQuit()
|
||||||
|
# update display contents
|
||||||
updateDisplay()
|
updateDisplay()
|
||||||
|
# record that the sorting has completed
|
||||||
finished = True
|
finished = True
|
||||||
sortingAlgorithm = 0
|
sortingAlgorithm = 0
|
||||||
elif sortingAlgorithm == 3:
|
elif sortingAlgorithm == 3:
|
||||||
|
# set start time, for use with on-screen timer
|
||||||
startTime = time.time()
|
startTime = time.time()
|
||||||
swapped = True
|
swapped = True
|
||||||
while swapped:
|
while swapped:
|
||||||
swapped = False
|
swapped = False
|
||||||
|
# for each item in the list
|
||||||
for index, item in enumerate(numberList):
|
for index, item in enumerate(numberList):
|
||||||
swappedInternal = False
|
swappedInternal = False
|
||||||
|
# if the numbers are in the wrong position, swap them around
|
||||||
if index != len(numberList) - 1 and item > numberList[index + 1]:
|
if index != len(numberList) - 1 and item > numberList[index + 1]:
|
||||||
numberList[index] = numberList[index + 1]
|
numberList[index] = numberList[index + 1]
|
||||||
numberList[index + 1] = item
|
numberList[index + 1] = item
|
||||||
swapped = True
|
swapped = True
|
||||||
swappedInternal = True
|
swappedInternal = True
|
||||||
|
# check if the user wants to quit
|
||||||
checkQuit()
|
checkQuit()
|
||||||
|
# if items have been swapped, update display contents
|
||||||
if swappedInternal:
|
if swappedInternal:
|
||||||
updateDisplay()
|
updateDisplay()
|
||||||
|
# record that the sorting has completed
|
||||||
finished = True
|
finished = True
|
||||||
sortingAlgorithm = 0
|
sortingAlgorithm = 0
|
||||||
|
# if sorting has completed
|
||||||
elif finished:
|
elif finished:
|
||||||
|
# update display contents
|
||||||
updateDisplay()
|
updateDisplay()
|
||||||
|
# check if the reset button has been pressed
|
||||||
finished = checkResetButton()
|
finished = checkResetButton()
|
||||||
|
# ensure reset button and exiting are both enabled
|
||||||
|
clock.tick(fps)
|
||||||
|
# check if the user wants to quit
|
||||||
|
checkQuit()
|
||||||
|
# ensure reset button and exiting are both enabled
|
||||||
|
clock.tick(fps)
|
||||||
|
# if reset button has been pressed, reset the list
|
||||||
if not finished:
|
if not finished:
|
||||||
numberList = originalNumberList.copy()
|
numberList = originalNumberList.copy()
|
||||||
|
# update display contents
|
||||||
updateDisplay()
|
updateDisplay()
|
||||||
checkQuit()
|
|
||||||
else:
|
else:
|
||||||
|
#check if the user wants to quit
|
||||||
checkQuit()
|
checkQuit()
|
||||||
|
# check if any of the start buttons have been pressed
|
||||||
sortingAlgorithm = checkStartButtons()
|
sortingAlgorithm = checkStartButtons()
|
||||||
Reference in New Issue
Block a user