Skip to content

Commit 2856696

Browse files
added a helper function for easy use
1 parent 7f87220 commit 2856696

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

MergeSortVis.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ def display(self):
2525
plt.pause(0.0001)
2626

2727

28-
def merge_sort(current: Vis, lo, hi):
28+
def _merge_sort(current: Vis, lo, hi):
2929
unsorted = current.lst[lo:hi]
3030

3131
if len(unsorted) < 2:
3232
return unsorted
3333

34-
left = merge_sort(current, lo, lo + round(len(unsorted)/2)).copy()
35-
right = merge_sort(current, lo + round(len(unsorted)/2), hi).copy()
34+
left = _merge_sort(current, lo, lo + round(len(unsorted)/2)).copy()
35+
right = _merge_sort(current, lo + round(len(unsorted)/2), hi).copy()
3636

3737
l, r = 0, 0
3838
while l < len(left) and r < len(right):
@@ -65,6 +65,10 @@ def merge_sort(current: Vis, lo, hi):
6565
return current.lst[lo:hi]
6666

6767

68+
def merge_sort(current: Vis):
69+
return _merge_sort(current, 0, len(current.lst))
70+
71+
6872
if __name__ == '__main__':
6973
my_vis = Vis()
70-
merge_sort(my_vis, 0, len(my_vis.lst))
74+
merge_sort(my_vis)

QuickSortVis.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def display(self):
2525
plt.pause(0.0001)
2626

2727

28-
def quick_sort(current: Vis, lo, hi):
28+
def _quick_sort(current: Vis, lo, hi):
2929
unsorted = current.lst[lo:hi]
3030

3131
if len(unsorted) < 2:
@@ -61,9 +61,13 @@ def quick_sort(current: Vis, lo, hi):
6161
current.display()
6262
current.highlights[lo+i], current.highlights[lo+j], current.highlights[lo+pivot_index] = current.barcol, current.barcol, current.barcol
6363

64-
return quick_sort(current, lo, lo+i) + quick_sort(current, lo+i, hi)
64+
return _quick_sort(current, lo, lo+i) + _quick_sort(current, lo+i, hi)
65+
66+
67+
def quick_sort(current: Vis):
68+
return _quick_sort(current, 0, len(current.lst))
6569

6670

6771
if __name__ == '__main__':
6872
my_vis = Vis()
69-
quick_sort(my_vis, 0, len(my_vis.lst))
73+
quick_sort(my_vis)

0 commit comments

Comments
 (0)