Skip to content

Commit 31ebe35

Browse files
committed
Insertion Sort Done plus addtional comments
1 parent a7448a7 commit 31ebe35

9 files changed

+35
-7
lines changed

BubbleSort.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Space Complexity: O(1)
1111
'''
1212

13-
# Based off of Geeks for Geeks Bubble sort example
13+
# Based off of Geeks for Geeks Bubble sort example: https://www.geeksforgeeks.org/bubble-sort/
1414
# Another source: https://www.studytonight.com/data-structures/bubble-sort
1515
def BubbleSort(data, drawDataArray, sortSpeedTime):
1616
# Traverse through all array elements
@@ -24,6 +24,3 @@ def BubbleSort(data, drawDataArray, sortSpeedTime):
2424
drawDataArray(data, ['purple' if x == j or x == j+1 else 'red' for x in range(len(data))] )
2525
# Helps us determine how long this function should be asleep for once the user can see each step of this algorithm
2626
time.sleep(sortSpeedTime)
27-
28-
# After going through the double for loop, the array should be sorted therefore all bars should be blue
29-
drawDataArray(data, ['blue' for x in range(len(data))])

InsertionSort.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import time # To help us visualize this algorithm step by step
2+
3+
'''
4+
Worst Case Time Complexity [ Big-O ]: O(n^2)
5+
6+
Best Case Time Complexity [Big-omega]: O(n)
7+
8+
Average Time Complexity [Big-theta]: O(n^2)
9+
10+
Space Complexity: O(1)
11+
'''
12+
13+
# Based off of Geeks for Geeks Insertion sort example: https://www.geeksforgeeks.org/insertion-sort/
14+
# Another source: https://www.studytonight.com/data-structures/insertion-sorting
15+
def InsertionSort(data, drawDataArray, sortSpeedTime):
16+
# Traverse through length of data array
17+
for i in range (1, len(data)):
18+
key = data[i]
19+
# Move elements of arr[0..i-1], that are > key, to one position ahead of current position
20+
j = i-1
21+
while j >= 0 and key < data[j]:
22+
data[j + 1] = data[j]
23+
j -= 1
24+
# Blue = Done with Sorted, Red = Unsorted, Purple = Being Swap / Being Sorted
25+
drawDataArray(data, ['purple' if x == j or x == j + 1 else 'red' for x in range(len(data))])
26+
time.sleep(sortSpeedTime)
27+
data[j + 1] = key

MergeSort.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
Space Complexity: O(n)
1111
'''
12-
# Based off of Geeks for Geeks Bubble sort example
12+
# Based off of Geeks for Geeks Bubble sort example: https://www.geeksforgeeks.org/merge-sort/
1313
# Another source: https://www.studytonight.com/data-structures/merge-sort#
1414
def MergeSort(data, drawDataArray, sortSpeedTime):
1515
MergeSort2(data,0, len(data)-1, drawDataArray, sortSpeedTime)

QuickSort.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Space Complexity: O(n*log n)
1111
'''
1212

13-
# Based off of Geeks for Geeks Bubble sort example
13+
# Based off of Geeks for Geeks Bubble sort example: https://www.geeksforgeeks.org/quick-sort/
1414
# Another source: https://www.studytonight.com/data-structures/quick-sort
1515
def QuickSort(data, low, high, drawDataArray, sortSpeedTime):
1616

__pycache__/BubbleSort.cpython-38.pyc

-127 Bytes
Binary file not shown.
699 Bytes
Binary file not shown.

__pycache__/MergeSort.cpython-38.pyc

0 Bytes
Binary file not shown.

__pycache__/QuickSort.cpython-38.pyc

0 Bytes
Binary file not shown.

sortingAlgorithmVisualizer.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from BubbleSort import BubbleSort
55
from QuickSort import QuickSort
66
from MergeSort import MergeSort
7+
from InsertionSort import InsertionSort
78

89

910
# Generates a black window; Background for GUI
@@ -78,6 +79,9 @@ def StartSortingAlgorithm():
7879
elif algorithmList.get() == 'Merge Sort':
7980
MergeSort(data, drawDataArray, sortingSpeedScale.get())
8081

82+
elif algorithmList.get() == 'Insertion Sort':
83+
InsertionSort(data, drawDataArray, sortingSpeedScale.get())
84+
8185
# After going through the double for loop, the array should be sorted therefore all bars should be blue
8286
drawDataArray(data, ['blue' for x in range(len(data))])
8387

@@ -95,7 +99,7 @@ def StartSortingAlgorithm():
9599
# FIRST ROW
96100
# (Top Left Corner); Algorithm Label (where you can choose which sorting algorithm you want to use)
97101
Label(UserInterfaceFrame, text="Sorting Algorithm: ", bg='snow4').grid(row=0, column=0, padx=5, pady=5, sticky=W) # W for west aka left
98-
algorithmList = ttk.Combobox(UserInterfaceFrame, textvariable = selectedAlgorithm, values=['Bubble Sort', 'Quick Sort','Merge Sort'])
102+
algorithmList = ttk.Combobox(UserInterfaceFrame, textvariable = selectedAlgorithm, values=['Bubble Sort', 'Quick Sort','Merge Sort', 'Insertion Sort'])
99103
algorithmList.grid(row=0, column=1, padx=5, pady=5)
100104
algorithmList.current(0) #chooses Bubble Sort by default since it is in index 0
101105

0 commit comments

Comments
 (0)