Skip to content

Commit 3afc762

Browse files
committed
Improved the Documentation.
1 parent 38c53c4 commit 3afc762

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

README.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,41 @@
22

33
Visualizing sorting algorithms, using the matplotlib library.
44

5-
Algorithms covered so far:<br />
6-
Quick Sort<br />
7-
Bubble Sort<br />
8-
Selection Sort<br />
9-
Insertion Sort<br />
10-
Heap Sort<br />
11-
Merge Sort
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 |
1215

1316
# Usage:
1417

18+
Install
19+
20+
```pip install -r requirements.txt```
21+
1522
Run
1623

1724
```python main.py function_name```
1825

19-
Choose function name from list of functions above (in all lower case and spaces replaced by underscore).
26+
Pass function name as a command line argument from list of functions above
27+
(in all lower case and spaces replaced by underscore).
2028

21-
For example:
29+
**For example:**
2230

2331
```python main.py quick_sort```
2432

2533
# How to contribute
2634

27-
Add new sorting algorithms to `sorting.py`
28-
In your algorithms, pass an `Array` (defined in `sorting.py`) object, instead of a list.
29-
30-
The call to your sorting algorithm should be `sorting_algorithm(Array([1, 2, 3]))`, not `sorting_algorithm([1, 2, 3])`.
31-
32-
Whenever you are swapping values, call `Array.swap`, and whenever you are setting a value, call `Array.set`.
33-
The length of the array can be found out using `Array.length`.
34-
35-
Make sure you test your newly implemented algorithm by running `test.py` after appending it to the list of algorithms in `test.py`.
35+
**If you want to add a new sorting algorithm:**
3636

37+
1. Code the algorithm in ```sorting.py```.
38+
2. Name the function appropriately, like ```quick_sort```, ```bubble_sort```.
39+
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)
40+
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.
41+
5. Make sure you add the sorting algorithm to the Readme file!
42+
6. Make sure your newly implemented algorithm works, by running `test.py` after appending it to the list of algorithms in `test.py`.

sorting.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def set(self, index, num):
2525
if not test:
2626
vs.plot(self.values)
2727

28-
def getLen(self):
28+
def get_len(self):
2929
return len(self.values)
3030

3131

@@ -34,7 +34,7 @@ def bubble_sort(nums): # n^2
3434
swapped = True
3535
while swapped:
3636
swapped = False
37-
for i in range(nums.getLen() - 1):
37+
for i in range(nums.get_len() - 1):
3838
if nums.values[i] > nums.values[i + 1]:
3939
# Swap the elements
4040
nums.swap(i, i + 1)
@@ -44,11 +44,11 @@ def bubble_sort(nums): # n^2
4444

4545
def selection_sort(nums): # n^2
4646
# This value of i corresponds to how many values were sorted
47-
for i in range(nums.getLen()):
47+
for i in range(nums.get_len()):
4848
# We assume that the first item of the unsorted segment is the smallest
4949
lowest_value_index = i
5050
# This loop iterates over the unsorted items
51-
for j in range(i + 1, nums.getLen()):
51+
for j in range(i + 1, nums.get_len()):
5252
if nums.values[j] < nums.values[lowest_value_index]:
5353
lowest_value_index = j
5454
# Swap values of the lowest unsorted element with the first unsorted
@@ -58,7 +58,7 @@ def selection_sort(nums): # n^2
5858

5959
def insertion_sort(nums): # n^2
6060
# Start on the second element as we assume the first element is sorted
61-
for i in range(1, nums.getLen()):
61+
for i in range(1, nums.get_len()):
6262
item_to_insert = nums.values[i]
6363
# And keep a reference of the index of the previous element
6464
j = i - 1
@@ -94,7 +94,7 @@ def heapify(nums, heap_size, root_index):
9494
# Heapify the new root element to ensure it's the largest
9595
heapify(nums, heap_size, largest)
9696

97-
n = nums.getLen()
97+
n = nums.get_len()
9898

9999
# Create a Max Heap from the list
100100
# The 2nd argument of range means we stop at the element before -1 i.e.
@@ -147,11 +147,11 @@ def merge(left_list, right_list):
147147
return sorted_list
148148

149149
# If the list is a single element, return it
150-
if nums.getLen() <= 1:
150+
if nums.get_len() <= 1:
151151
return nums.values
152152

153153
# Use floor division to get midpoint, indices must be integers
154-
mid = nums.getLen() // 2
154+
mid = nums.get_len() // 2
155155

156156
# Sort and merge each half
157157
left_list = merge_sort(Array(nums.values[:mid]))
@@ -199,4 +199,4 @@ def _quick_sort(items, low, high):
199199
_quick_sort(items, low, split_index)
200200
_quick_sort(items, split_index + 1, high)
201201

202-
_quick_sort(nums, 0, nums.getLen() - 1)
202+
_quick_sort(nums, 0, nums.get_len() - 1)

0 commit comments

Comments
 (0)