def mainVisual(): close = True while close: mouse = pygame.mouse.get_pos() #print(mouse) for event in pygame.event.get(): if event.type == pygame.QUIT: close = False img = pygame.image.load("wall.jpg") screen.blit(img, [0, 0]) pygame.display.update() displayfiles.message_display("Welcome To Sort Visualizer", [400, 100], white) displayfiles.button("Bubble Sort", 400, 300, 150, 30, red, bright_red, bubblesort.bubblemain) displayfiles.button("Merge Sort", 400, 350, 150, 30, red, bright_red, mergesort.mergemain) displayfiles.button("Insertion Sort", 400, 400, 150, 30, red, bright_red, insertionsort.insertionmain) displayfiles.button("Quick Sort", 400, 450, 150, 30, red, bright_red, quicksort.quickmain) displayfiles.button("Quit", 20, 500, 150, 30, red, bright_red, visualQuit) print("Out") pygame.quit() quit()
def bubblemain(): img = pygame.image.load("wall2.jpeg") screen.blit(img, [0, 0]) displayfiles.message_display("Bubble Sort", [400, 50], white) pygame.display.update() print("Bubble") done = True flag = False while done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = False done = displayfiles.button("Home", 450, 500, 150, 30, red, bright_red, getOut) displayfiles.button("Quit", 650, 500, 150, 30, red, bright_red, visualQuit) if not flag: for i in range(len(arr)): swap = False for j in range(len(arr) - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] swap = True for event in pygame.event.get(): if event.type == pygame.QUIT: closed = True exit() img = pygame.image.load("wall2.jpeg") screen.blit(img, [0, 0]) displayfiles.message_display("Bubble Sort", [400, 50], white) displayfiles.displayArray(arr, j) pygame.display.update() clock.tick(speed) if not swap: flag = True break displayfiles.button("Time Complexity : O(n^2)", 550, 100, 250, 30, red, bright_red) displayfiles.button("Inplace Sort", 550, 150, 250, 30, red, bright_red) displayfiles.button("Swap Elements", 550, 200, 250, 30, red, bright_red) pygame.display.update() print('out of buubke')
def mergeSort(a): if len(a) > 1: mid = len(a) // 2 left = a[:mid] right = a[mid:] mergeSort(left) mergeSort(right) i, j, k = 0, 0, 0 while i < len(left) and j < len(right): for event in pygame.event.get(): if event.type == pygame.QUIT: closed = True exit() screen.blit(img, [0, 0]) displayfiles.message_display("Merge Sort", [400, 50], white) displayfiles.button("Quit", 650, 500, 150, 30, red, bright_red, visualQuit) displayfiles.button("Complexity : O(nlogn)", 550, 100, 250, 30, red, grey) displayfiles.button("Inplace Sort", 550, 150, 250, 30, red, grey) displayfiles.button("Swap Elements", 550, 200, 250, 30, red, grey) displayfiles.displayArray(a, k) pygame.display.update() clock.tick(speed) if left[i] < right[j]: a[k] = left[i] i += 1 k += 1 else: a[k] = right[j] j += 1 k += 1 while i < len(left): a[k] = left[i] i += 1 k += 1 while j < len(right): a[k] = right[j] j += 1 k += 1 return
def partion(a, low, high): i = low - 1 pivot = a[high] for j in range(low, high): if a[j] < pivot: i += 1 a[i], a[j] = a[j], a[i] for event in pygame.event.get(): if event.type == pygame.QUIT: closed = True exit() screen.blit(img, [0, 0]) displayfiles.message_display("Inseertion Sort", [400, 50], white) displayfiles.displayArray(a, j) displayfiles.button("Complexity : O(nlogn)", 550, 100, 250, 30, red, grey) displayfiles.button("Inplace Sort", 550, 150, 250, 30, red, grey) displayfiles.button("Swap Elements", 550, 200, 250, 30, red, grey) pygame.display.update() clock.tick(speed) a[i + 1], a[high] = a[high], a[i + 1] return i + 1
def insertionSort(a): n = len(a) for i in range(1,n): place_key = a[i] #9 8 5 4 j = i-1 while j>=0 and place_key<a[j]: for event in pygame.event.get(): if event.type==pygame.QUIT: closed = True exit() img = pygame.image.load("wall2.jpeg") screen.blit(img,[0,0]) displayfiles.message_display("Insertion Sort",[400,50],white) displayfiles.displayArray(a,j) displayfiles.button("Complexity : O(n^2)",550,100,250,30,red,grey) displayfiles.button("Inplace Sort",550,150,250,30,red,grey) displayfiles.button("Swap Elements",550,200,250,30,red,grey) pygame.display.update() clock.tick(speed) a[j+1] = a[j] j-=1 a[j+1] = place_key
def quickmain(): img = pygame.image.load("wall2.jpeg") screen.blit(img, [0, 0]) displayfiles.message_display("Quick Sort", [400, 50], white) pygame.display.update() done = True while done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = False if arr != sorted(arr): quickSort(arr) displayfiles.button("Start", 50, 500, 150, 30, red, bright_red, startAgain) done = displayfiles.button("Home", 450, 500, 150, 30, red, bright_red, getOut) displayfiles.button("Quit", 650, 500, 150, 30, red, bright_red, visualQuit) displayfiles.button("Complexity : O(nlogn)", 550, 100, 250, 30, red, grey) displayfiles.button("Inplace Sort", 550, 150, 250, 30, red, grey) displayfiles.button("Swap Elements", 550, 200, 250, 30, red, grey) pygame.display.update()