Skip to content

Commit a604262

Browse files
author
Abhishek Kumar Maurya
authored
added 4 sorting algorithms
1 parent 2813dd1 commit a604262

File tree

7 files changed

+499
-0
lines changed

7 files changed

+499
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/env/
2+
/__pycache__/
3+
/.vscode/

README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Sorting algorithm visualizer
2+
3+
Visualizing sorting algorithms, using the matplotlib library.
4+
5+
Algorithms covered so far:
6+
7+
| Name | Function Name |
8+
| - |:-: |
9+
| Quick Sort | quick_sort |
10+
| Bubble Sort | bubble_sort |
11+
| Selection Sort | selection_sort |
12+
| Insertion Sort | insertion_sort |
13+
| Heap Sort | heap_sort |
14+
| Merge Sort | merge_sort |
15+
| Cocktail Sort | cocktail_sort |
16+
| Cycle Sort | cycle_sort |
17+
| Pigeonhole Sort | pigeonhole_sort |
18+
| Comb Sort | comb_sort |
19+
20+
21+
# Usage:
22+
23+
Install
24+
25+
```pip install -r requirements.txt```
26+
27+
Run
28+
29+
```python main.py function_name```
30+
31+
Pass function name as a command line argument from list of functions above
32+
(in all lower case and spaces replaced by underscore).
33+
34+
**For example:**
35+
36+
```python main.py quick_sort```
37+
38+
# How to contribute
39+
40+
**If you want to add a new sorting algorithm:**
41+
42+
1. Code the algorithm in ```sorting.py```.
43+
2. Name the function appropriately, like ```quick_sort```, ```bubble_sort```.
44+
3. While coding the function, **do not use python lists**. Instead, use an ```Array``` object. The ```Array``` class is defined in ```sorting.py```. (See already implemented algorithms, for your reference)
45+
4. The ```Array``` object has ```swap```, ```set```, ```get_len```, ```get``` methods implemented. Feel free to implement any more, additional methods, that you may see fit.
46+
5. Make sure you add the sorting algorithm to the Readme file!
47+
6. Make sure your newly implemented algorithm works, by running `test.py` after appending it to the list of algorithms in `test.py`.

main.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
import sorting as s
3+
import visualizer as vs
4+
import numpy as np
5+
import random
6+
7+
8+
def get_random_array(length):
9+
n = list(range(length))
10+
random.shuffle(n)
11+
return n
12+
13+
14+
array_size = 50
15+
16+
algorithms = [s.bubble_sort, s.heap_sort, s.selection_sort,
17+
s.insertion_sort, s.quick_sort, s.insertion_sort, s.merge_sort, s.pigeonhole_sort,
18+
s.cycle_sort, s.comb_sort, s.cocktail_sort]
19+
20+
if len(sys.argv) < 2:
21+
print('Usage:', 'python', sys.argv[0], 'function_name', '\n')
22+
print('Choose from:', '\n')
23+
for algorithm in algorithms:
24+
print(algorithm.__name__)
25+
print('\n')
26+
sys.exit(0)
27+
28+
algorithms = list(filter(lambda a: a.__name__ == sys.argv[1], algorithms))
29+
if (len(algorithms) == 0):
30+
print('Method does not exist.')
31+
else:
32+
algorithm = algorithms[0]
33+
arr = s.Array(get_random_array(array_size))
34+
algorithm(arr)
35+
vs.show()

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
numpy
2+
matplotlib

0 commit comments

Comments
 (0)