Sort Visualizer


Python
NumPy


Short preview



Here is the source code for the sort visualizer that has three sorting algorithms: Quick Sort, Merge Sort, and Bubble Sort.

by pressing the keyboard key "R", a new array will appear and you can also specify the size of it. 

# Python implementation for visualizing merge sort.  
import pygame 
import random 
pygame.font.init() 

screen = pygame.display.set_mode((900, 650)) 
pygame.display.set_caption("SORTING VISUALISER")   
run = True
r=20

width = 900
length = 600
array =[0]*r
arr_clr =[(0, 204, 102)]*r
clr_ind = 0
clr =[(0, 204, 102), (255, 0, 0),  
(0, 0, 153), (255, 102, 0),(255, 255, 0)] 
fnt = pygame.font.SysFont("comicsans", 17) 
fnt1 = pygame.font.SysFont("comicsans", 17) 
base_font=pygame.font.Font(None,30)


class button():
    def __init__(self, color, x,y,width,height, text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text

    def draw(self,win,outline=None):
        #Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(win, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
            
        pygame.draw.rect(win, self.color, (self.x,self.y,self.width,self.height),0)
        
        if self.text != '':
            font = pygame.font.SysFont('comicsans', 17)
            text = font.render(self.text, 1, (0,0,0))
            win.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))

    def isOver(self, pos):
        #Pos is the mouse position or a tuple of (x,y) coordinates
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True
            
        return False

def generate_arr(): 
    for i in range(1,r): 
        arr_clr[i]= clr[0] 
        array[i]= random.randrange(1, 100) 
generate_arr()  
def refill():
    screen.fill((255, 255, 255))
    draw()
    pygame.display.update()
    pygame.time.delay(100)
def buble_sort(a):
    pygame.event.pump()  
    for i in range(len(a)-1,0,-1):  
        for j in range(i):
            if a[j]>a[j+1]:
                pygame.event.pump()
                arr_clr[j]= clr[2]
                arr_clr[j+1]= clr[1] 
                refill() 
                arr_clr[j]= clr[0]
                arr_clr[j+1]= clr[0] 
                temp=a[j]
                a[j]=a[j+1]
                a[j+1]=temp 
        arr_clr[i]= clr[3] 
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                pygame.quit()

  
def quicksort(a,l,r):
    c=0
    if l<r:
        c+=1
        p=partition(a,l,r)
        quicksort(a,l,p-1)
        quicksort(a,p+1,r)
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                pygame.quit()
    elif l>=r and 0<l<len(a):
        arr_clr[l]= clr[2]
        refill()
        arr_clr[l]= clr[3]
        refill()
def partition(a,l,r):
    i=l
    j=r-1
    pivot=a[r]
    pygame.event.pump()  
    while i<j:
        while i<r and a[i]<pivot:
            i+=1
        while j>l and a[j]>=pivot:
            j-=1
        if i<j:
            arr_clr[i]= clr[1]
            arr_clr[j]= clr[2] 
            refill() 
            arr_clr[i]= clr[0]
            arr_clr[j]= clr[0]
            a[i],a[j]=a[j],a[i]

    if a[i]>=pivot:
        arr_clr[r]= clr[0]
        a[i],a[r]=a[r],a[i]
        arr_clr[i]= clr[3]
        refill()

    return i

def mergesort(array, l, r): 
    mid =(l+ r)//2
    if l<r: 
        mergesort(array, l, mid) 
        mergesort(array, mid + 1, r) 
        merge(array, l, mid, 
            mid + 1, r) 
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                pygame.quit()
def merge(array, x1, y1, x2, y2): 
    i = x1 
    j = x2 
    temp =[] 
    pygame.event.pump()  
    while i<=y1 and j<=y2: 
        arr_clr[i]= clr[1] 
        arr_clr[j]= clr[1] 
        refill() 
        arr_clr[i]= clr[0] 
        arr_clr[j]= clr[0] 
        if array[i]<array[j]: 
                temp.append(array[i]) 
                i+= 1
        else: 
                temp.append(array[j]) 
                j+= 1
    while i<=y1: 
        arr_clr[i]= clr[1] 
        refill() 
        arr_clr[i]= clr[0] 
        temp.append(array[i]) 
        i+= 1
    while j<=y2: 
        arr_clr[j]= clr[1] 
        refill() 
        arr_clr[j]= clr[0] 
        temp.append(array[j]) 
        j+= 1
    j = 0    
    for i in range(x1, y2+1 ):  
        pygame.event.pump()  
        array[i]= temp[j] 
        j+= 1
        arr_clr[i]= clr[2] 
        refill() 
        if y2-x1 == len(array)-2: 
            arr_clr[i]= clr[3] 
        else:  
            arr_clr[i]= clr[0] 

def draw(): 
    txt = fnt.render("ENTER THE NUMBER OF VALUES YOU WANT"\
    , 1, (0, 0, 0)) 
    screen.blit(txt, (0, 20)) 
    txt2 = fnt.render("TO SORT IN THE BLUE BOX: MAX(299)"\
    , 1, (0, 0, 0)) 
    screen.blit(txt2, (0, 40)) 


    txt1 = fnt.render("PRESS 'R' FOR NEW ARRAY.", 
                    1, (0, 0, 0)) 
    screen.blit(txt1, (20, 70)) 

    element_width =(width-r-1)//r-1
    boundry_arr = 900 / r-1
    boundry_grp = 550 / 100
    pygame.draw.line(screen, (0, 0, 0),  
                    (0,95), (900, 95), 6) 

    for i in range(1, r): 
        pygame.draw.line(screen, arr_clr[i],
            (boundry_arr * i-3, 100),
            (boundry_arr * i-3, array[i] *boundry_grp+ 100),
            element_width) 
    qsbutton.draw(screen,(0,0,0))
    bsbutton.draw(screen,(0,0,0))
    msbutton.draw(screen,(0,0,0))
    pygame.draw.rect(screen,color,input_rect)
    text_surface=base_font.render(user_text,True,(0,0,0))
    screen.blit(text_surface, (input_rect.x + (input_rect.width/2 - text_surface.get_width()/2), input_rect.y + (input_rect.height/2 - text_surface.get_height()/2)))
    # screen.blit(text_surface,(input_rect.x+2,input_rect.y+4))
qsbutton=button((255,0,0),450,50,110,40,"QUICK SORT")
bsbutton=button((255,0,0),565,50,110,40,"BUBLE SORT")
msbutton=button((255,0,0),680,50,110,40,"MERGE SORT")
input_rect=pygame.Rect(340,42,70,40)
color=pygame.Color("lightskyblue3")
active=0
enter_pressed=0
user_text=""
while run: 
    screen.fill((255, 255, 255)) 
    for event in pygame.event.get(): 
        pos=pygame.mouse.get_pos()
        if event.type == pygame.QUIT: 
            run = False
        if event.type==pygame.MOUSEBUTTONDOWN:
            if input_rect.collidepoint(event.pos):
                active=1
            else:
                active=0
            if qsbutton.isOver(pos):
                quicksort(array,0,len(array)-1)
            if bsbutton.isOver(pos):
                buble_sort(array)
            if msbutton.isOver(pos):
                mergesort(array,1,len(array)-1)

        if event.type == pygame.KEYDOWN:
            if active==0:
                enter_pressed=1
                if enter_pressed==1:
                    if event.key == pygame.K_r: 
                        generate_arr() 
            if active==1:
                if event.unicode =="\b" and len(user_text)>0:
            
                    user_text=user_text[:-1]
                    if len(user_text)>0:
                        r=int(user_text)
                        array =[0]*r
                        arr_clr =[(0, 204, 102)]*r
                        generate_arr()

                elif event.unicode !="\b" and (event.unicode=="1"or event.unicode=="2"\
                or event.unicode=="3"or event.unicode=="4"or event.unicode=="5"or event.unicode=="6"or \
                event.unicode=="7"or event.unicode=="8"or event.unicode=="9"or event.unicode=="0"):
                    user_text+=event.unicode
                    print(type(int(event.unicode)))
                    r=int(user_text)
                    array =[0]*r
                    arr_clr =[(0, 204, 102)]*r
                    generate_arr()
                   


    draw() 
    pygame.display.update() 
    
pygame.quit()
print(array)

 


Comments (0)