Skip to content
135 changes: 135 additions & 0 deletions PyGamesScripts/Sorting Algorithm Visualizer/Bubble_Sort.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pygame 2.0.1 (SDL 2.0.14, Python 3.8.5)\n",
"Hello from the pygame community. https://www.pygame.org/contribute.html\n"
]
}
],
"source": [
"# Importing modules\n",
"import pygame\n",
"import random\n",
"\n",
"pygame.font.init()\n",
"\n",
"screen = pygame.display.set_mode((700, 700))\n",
"\n",
"pygame.display.set_caption(\"Bubble Sort\")\n",
"\n",
"# bool variable to run the sorting visualzation, as long as this is true\n",
"run = True\n",
"\n",
"width = 700\n",
"length = 600\n",
"array =[0]*50\n",
"arr_color =[(0, 204, 102)]*50\n",
"\n",
"color =[(0, 255, 0), (255, 0, 0), (0, 0, 255), (255, 102, 0)]\n",
"\n",
"fnt = pygame.font.SysFont(\"helvetica\", 30)\n",
"\n",
"# generating array\n",
"def generate_arr():\n",
" for i in range(1, 50):\n",
" arr_color[i]= color[0]\n",
" array[i]= random.randrange(1, 100)\n",
"\n",
"generate_arr()\n",
"\n",
"# we have to refill the screen to keep the sorting continued\n",
"def refill():\n",
" screen.fill((0, 0, 0))\n",
" draw()\n",
" pygame.display.update()\n",
" pygame.time.delay(10)\n",
"\n",
"# sorting algorithm\n",
"def BubbleSort(array):\n",
"\n",
" for i in range(1, len(array)):\n",
" pygame.event.pump()\n",
" refill()\n",
" j = 0\n",
" while j < (len(array)-i):\n",
" arr_color[j]= color[1]\n",
" arr_color[j+1] = color[1]\n",
" if (array[j] > array[j+1]):\n",
" temp = array[j]\n",
" array[j] = array[j+1]\n",
" array[j+1] = temp\n",
" refill() \n",
" arr_color[j] = color[0]\n",
" arr_color[j+1] = color[0]\n",
" refill()\n",
" j = j+1\n",
" arr_color[j] = color[2]\n",
"\n",
"# to draw the array elements as bars\n",
"def draw():\n",
" txt = fnt.render(\"PRESS ENTER\", 1, (0, 0, 0))\n",
" # positioning of the above text\n",
" screen.blit(txt, (230, 20))\n",
" element_width =(width-50)//50\n",
" boundryArr = 700 / 50\n",
" boundryGrp = 550 / 100\n",
" pygame.draw.line(screen, (0, 0, 0), (0, 95), (700, 95), 6)\n",
" \n",
" for i in range(1, 50):\n",
" pygame.draw.line(screen, arr_color[i], (boundryArr * i-3, 100), (boundryArr * i-3, \\\n",
" array[i]*boundryGrp + 100), element_width)\n",
"\n",
"while run:\n",
" screen.fill((100, 100, 100))\n",
" # event handler for pygame\n",
" for event in pygame.event.get():\n",
"\n",
" if event.type == pygame.QUIT:\n",
" run = False\n",
" if event.type == pygame.KEYDOWN:\n",
" if event.key == pygame.K_RETURN:\n",
" BubbleSort(array) \n",
" draw()\n",
" pygame.display.update()\n",
" \n",
"pygame.quit()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
134 changes: 134 additions & 0 deletions PyGamesScripts/Sorting Algorithm Visualizer/Insertion_Sort.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pygame 2.0.1 (SDL 2.0.14, Python 3.8.5)\n",
"Hello from the pygame community. https://www.pygame.org/contribute.html\n"
]
}
],
"source": [
"# Importing modules\n",
"\n",
"import pygame\n",
"import random\n",
"\n",
"pygame.font.init()\n",
"\n",
"screen = pygame.display.set_mode((700, 700))\n",
"\n",
"pygame.display.set_caption(\"Insertion Sort\")\n",
"\n",
"# bool variable to run the sorting visualzation, as long as this is true\n",
"run = True\n",
"\n",
"width = 700\n",
"length = 600\n",
"array =[0]*50\n",
"arr_color =[(0, 204, 102)]*50\n",
"\n",
"color =[(0, 255, 0), (255, 0, 0), (0, 0, 255), (255, 102, 0)]\n",
"\n",
"fnt = pygame.font.SysFont(\"helvetica\", 30)\n",
"\n",
"# generating array\n",
"def generate_arr():\n",
" for i in range(1, 50):\n",
" arr_color[i]= color[0]\n",
" array[i]= random.randrange(1, 100)\n",
"\n",
"generate_arr()\n",
"\n",
"# we have to refill the screen to keep the sorting continued\n",
"def refill():\n",
" screen.fill((0, 0, 0))\n",
" draw()\n",
" pygame.display.update()\n",
" pygame.time.delay(10)\n",
"\n",
"# sorting algorithm\n",
"def insertionSort(array):\n",
"\n",
" for i in range(1, len(array)):\n",
" pygame.event.pump()\n",
" refill()\n",
" key = array[i]\n",
" arr_color[i]= color[2]\n",
" j = i-1\n",
" while j >= 0 and key < array[j]:\n",
" arr_color[j]= color[2]\n",
" array[j + 1]= array[j]\n",
" refill()\n",
" arr_color[j]= color[3]\n",
" j = j-1\n",
" array[j + 1]= key\n",
" refill()\n",
" arr_color[i]= color[0]\n",
"\n",
"# to draw the array elements as bars\n",
"def draw():\n",
" txt = fnt.render(\"PRESS ENTER\", 1, (0, 0, 0))\n",
" # positioning of the above text\n",
" screen.blit(txt, (230, 20))\n",
" element_width =(width-50)//50\n",
" boundryArr = 700 / 50\n",
" boundryGrp = 550 / 100\n",
" pygame.draw.line(screen, (0, 0, 0), (0, 95), (700, 95), 6)\n",
" \n",
" for i in range(1, 50):\n",
" pygame.draw.line(screen, arr_color[i], (boundryArr * i-3, 100), (boundryArr * i-3, \\\n",
" array[i]*boundryGrp + 100), element_width)\n",
"\n",
"while run:\n",
" screen.fill((100, 100, 100))\n",
" # event handler for pygame\n",
" for event in pygame.event.get():\n",
"\n",
" if event.type == pygame.QUIT:\n",
" run = False\n",
" if event.type == pygame.KEYDOWN:\n",
" if event.key == pygame.K_RETURN:\n",
" insertionSort(array) \n",
" draw()\n",
" pygame.display.update()\n",
" \n",
"pygame.quit()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
23 changes: 23 additions & 0 deletions PyGamesScripts/Sorting Algorithm Visualizer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Sorting Algorithm Visualizer

* TechStack: Python, PyGame Module
* Algorithms covered:
* Bubble Sort
* Insertion Sort
* Libraries
* PyGame
* Random

# Setup
This code can be run on any Python IDE of Jupyter notebook.

# Purpose
The Sorting Algorithms are visualized here, which helps to understand the working of these Algorithms in a better way

# Screenshot of output

* [Img1](Images/BubbleSort_1.png)
* [Img2](Images/BubbleSort_2.png)
* [Img3](Images/InsertionSort_1.png)
* [Img4](Images/InsertionSort_2.png)

2 changes: 2 additions & 0 deletions PyGamesScripts/Sorting Algorithm Visualizer/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import PyGame
import Random