# Sorting Algorithm Visualization : Merge Sort

The human brain can easily process visuals instead of long codes to understand the algorithms. In this article, a program that program visualizes the Merge sort Algorithm has been implemented.

The GUI(Graphical User Interface) is implemented using pygame package in python.

Approach:

• An array of random values is generated and are drawn as lines(bars) in the window .
• Different colors are used to indicate which elements being compared, sorted and unsorted.
• Since the algorithm performs the operation very fast, pygame.time.delay() has been used to slow down the process.
• New array can be generated by pressing ‘r’ key.
• The actions are performed using ‘pygame.event.get()’ method, which stores all the events which user performs.
• Examples:

Input :
Press “Enter” key to Perform Visualization.
Press “r” key to generate new array.
Output :
Initial:

Visualizing:

Final:

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Below is the program to visualize the Merge Sort algorithm:

 `# Python implementation for visualizing merge sort.  ` `import` `pygame ` `import` `random ` `pygame.font.init() ` `# Total window ` `screen ``=` `pygame.display.set_mode((``900``, ``650``)) ` ` `  `# Title and Icon  ` `pygame.display.set_caption(``"SORTING VISUALISER"``) ` `# Place any custom png file in same folder as the source code ` `# and mention it below and uncomment below two lines. ` `# img = pygame.image.load ` `(``'E:/Projects / Sorting Visualiser / sorticon.png'``) ` `# pygame.display.set_icon(img) ` ` `  `# Boolean variable to run the program in while loop ` `run ``=` `True` ` `  `# Window size ` `width ``=` `900` `length ``=` `600` `array ``=``[``0``]``*``151` `arr_clr ``=``[(``0``, ``204``, ``102``)]``*``151` `clr_ind ``=` `0` `clr ``=``[(``0``, ``204``, ``102``), (``255``, ``0``, ``0``),  ` `(``0``, ``0``, ``153``), (``255``, ``102``, ``0``)] ` `fnt ``=` `pygame.font.SysFont(``"comicsans"``, ``30``) ` `fnt1 ``=` `pygame.font.SysFont(``"comicsans"``, ``20``) ` `# Generate new Array ` `def` `generate_arr(): ` `    ``for` `i ``in` `range``(``1``, ``151``): ` `        ``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(``20``) ` ` `  `# Sorting Algo:Merge sort ` `def` `mergesort(array, l, r): ` `    ``mid ``=``(l ``+` `r)``/``/``2` `    ``if` `l

Output:

```
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.