forked from brockellefson/IntrospectiveSortResearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapSort.py
57 lines (48 loc) · 1.3 KB
/
HeapSort.py
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
import pygame
from random import randint
import time
pygame.init()
size = (500, 500)
window = pygame.display.set_mode((size))
pygame.display.set_caption("Heapsort")
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
ourlist = []
listLength = 100
xList, y, w = [], 0, 5
tmpX = 0
for i in range(listLength):
ourlist.append(randint(0, 500))
#ourlist.append(i+200)
xList.append(tmpX)
tmpX += w
def draw():
global xList, y, ourlist
for i in range(listLength):
pygame.draw.rect(window, white, (xList[i], y, w, ourlist[i]), 0)
def heapify(arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[i] < arr[l]:
largest = l
if r < n and arr[largest] < arr[r]:
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i] # swap
heapify(arr, n, largest)
def heapSort(arr):
n = len(arr)
for i in range(n, -1, -1):
heapify(arr, n, i)
for i in range(n - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
heapify(arr, i, 0)
time.sleep(.1)
window.fill(black)
draw()
pygame.display.update()
heapSort(ourlist)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()