# importing pygameimportpygamepygame.init()# setting window sizewin=pygame.display.set_mode((500,400))# setting title to the windowpygame.display.set_caption("Bubble sort")# initial positionx=40y=40# width of each barwidth=20# height of each bar (data to be sorted)height=[200,50,130,90,250,61,110,88,33,80,70,159,180,20]run=True# method to show the list of heightdefshow(height):# loop to iterate each item of listforiinrange(len(height)):# drawing each bar with respective gappygame.draw.rect(win,(255,0,0),(x+30*i,y,width,height[i]))# infinite loopwhilerun:# execute flag to start sortingexecute=False# time delaypygame.time.delay(10)# getting keys pressedkeys=pygame.key.get_pressed()# iterating eventsforeventinpygame.event.get():# if event is to quitifevent.type==pygame.QUIT:# making run = false so break the while looprun=False# if space bar is pressedifkeys[pygame.K_SPACE]:# make execute flag to trueexecute=True# checking if execute flag is falseifexecute==False:# fill the window with black colorwin.fill((0,0,0))# call the height method to show the list itemsshow(height)# update the windowpygame.display.update()# if execute flag is trueelse:# start sorting using bubble sort techniqueforiinrange(len(height)-1):# after this iteration max element will come at lastforjinrange(len(height)-i-1):# starting is greater than next elementifheight[j]>height[j+1]:# save it in temporary variable# and swap them using temporary variablet=height[j]height[j]=height[j+1]height[j+1]=t# fill the window with black colorwin.fill((0,0,0))# call show method to display the list itemsshow(height)# create a time delaypygame.time.delay(50)# update the displaypygame.display.update()# exiting the main windowpygame.quit()