Skip to content

Updating README.md #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Sorting-Algorithms-Visualizer

Program made with Python and Pygame module for visualizing sorting algorithms \
This project is a Python application that visualizes different sorting algorithms using Pygame. It is great for students, developers, and educators who want to learn how sorting algorithms work through animation.\
Support this project by leaving a :star:

| | | |
Expand All @@ -14,3 +14,27 @@ Support this project by leaving a :star:
- Clone GitHub repository `git clone https://github.com/LucasPilla/Sorting-Algorithms-Visualizer.git`
- Install requirements: `pip3 install -r requirements.txt`
- Run: `python3 src/main.py`

## How to use
1. Enter appropriate number of bars required in the text box
2. Pick appropriate sorting algorithm from the dropdown for the visualizer
3. Use the play/pause button to control the visuals

## Supported Algorithms - Average Time Complexities
- Cycle Sort - O(n^2)
- Cocktail Sort - O(n^2)
- Counting Sort - O(n + k)
- Quick Sort - O( n log n)
- Merge Sort - O(n log n)
- Selection Sort - O(n^2)
- Bubble Sort - O(n^2)
- Insertion Sort - O(n^2)
- Bogo Sort - O((n-1)!)
- Heap Sort - O(n log n)
- Shell Sort - O(n log n)
- Radix Sort - O(nk)
- Bucket Sort - (n + k)
- Pancake Sort - O(n^2)

## Licensing
- This project is licensed under the MIT License
63 changes: 61 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
screen = pygame.display.set_mode((900, 500))
window = Window(screen)

# Load images and get their sizes
slow_image = pygame.image.load('res/button_slow.png')
slow_width, slow_height = slow_image.get_size()

normal_image = pygame.image.load('res/button_normal.png')
normal_width, normal_height = normal_image.get_size()

fast_image = pygame.image.load('res/button_fast.png')
fast_width, fast_height = fast_image.get_size()

window.add_widget(
widget_id = 'size_input',
widget = TextBox((30, 440, 100, 50), 'Size', grey, baseFont, '100')
Expand All @@ -35,6 +45,24 @@
widget_id = 'play_button',
widget = ButtonBox((350, 440, 40, 40), 'res/playButton.png', 'res/stopButton.png')
)
# Positions for speed buttons
slow_x = 425
normal_x = slow_x + slow_width + 20 # 20 pixels spacing
fast_x = normal_x + normal_width + 20

# Add speed buttons with accurate dimensions
window.add_widget(
widget_id='slow_button',
widget=ButtonBox((slow_x, 438, slow_width, slow_height), 'res/button_slow.png', 'res/button_slow.png')
)
window.add_widget(
widget_id='normal_button',
widget=ButtonBox((normal_x, 438, normal_width, normal_height), 'res/button_normal.png', 'res/button_normal.png')
)
window.add_widget(
widget_id='fast_button',
widget=ButtonBox((fast_x, 438, fast_width, fast_height), 'res/button_fast.png', 'res/button_fast.png')
)

def drawBars(screen, array, redBar1, redBar2, blueBar1, blueBar2, greenRows = {}):
'''Draw the bars and control their colors'''
Expand All @@ -56,6 +84,15 @@ def main():
isPlaying = False
isSorting = False
sortingIterator = None
visualDelay = 100 # delay is set to 100 by default
current_speed = 'normal'

# speed modes in milliseconds
speedModes = {
'slow' : 250, # delay is set to 200 in slow mode
'normal' : 100, # delay is set to 100 is normal mode
'fast' : 20 # delay is set to 20 in fast mode
}

while running:
screen.fill(white)
Expand All @@ -65,8 +102,26 @@ def main():

window.update(event)

# constantly check for mode changes
if window.get_widget_value('slow_button'):
if current_speed != 'slow':
current_speed = 'slow'
visualDelay = speedModes[current_speed]
print("slow button is pressed")
elif window.get_widget_value('normal_button'):
if current_speed != 'normal':
current_speed = 'normal'
visualDelay = speedModes[current_speed]
print("normal button is pressed")
elif window.get_widget_value('fast_button'):
if current_speed != 'fast':
current_speed = 'fast'
visualDelay = speedModes[current_speed]
print("fast button is pressed")

isPlaying = window.get_widget_value('play_button')
if isPlaying and not isSorting:
if isPlaying and not isSorting:
print("play button is pressed")
# random list to be sorted
numBars = int(window.get_widget_value('size_input'))
numbers = [randint(10, 400) for i in range(numBars)]
Expand All @@ -76,13 +131,17 @@ def main():
sortingIterator = algorithmsDict[sortingAlgorithm](numbers, 0, numBars-1)
isSorting = True

if not isPlaying:
if not isPlaying and isSorting:
current_speed = 'normal'
visualDelay = speedModes[current_speed]
isSorting = False


if isSorting:
try:
numbers, redBar1, redBar2, blueBar1, blueBar2 = next(sortingIterator)
drawBars(screen, numbers, redBar1, redBar2, blueBar1, blueBar2)
pygame.time.delay(visualDelay) # control speed of sorting visuals
except StopIteration:
isSorting = False
window.set_widget_value('play_button', False)
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes