Skip to content

Commit 60e9438

Browse files
committed
Added features
1 parent d38bbd9 commit 60e9438

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

insertionSort.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Insertion sort in Python
2+
def insertionSort(array):
3+
4+
# keep track of swapping
5+
6+
# loop through each element of array
7+
for i in range(1,len(array)):
8+
9+
# Setting the desired key element
10+
key=array[i]
11+
j=i-1
12+
13+
# Swapping the key and compared number
14+
# swapping occurs if elements
15+
# are not in the intended order
16+
while((j>-1) and (array[j]>key)):
17+
array[j+1]=array[j]
18+
j=j-1
19+
20+
array[j+1]=key
21+

selectionSort.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Selection sort in Python
2+
3+
4+
def selectionSort(array):
5+
6+
# keep track of swapping
7+
swapped = False
8+
9+
# loop through each element of array
10+
for i in range(len(array)):
11+
12+
# for keeping the smallest integer
13+
index = i
14+
15+
# loop to compare array elements
16+
for j in range(i+1,len(array)):
17+
18+
# change > to < to sort in descending order
19+
if array[j]<array[index]:
20+
21+
# index is equal to the condition number
22+
index=j
23+
24+
swapped=True
25+
26+
# swapping occurs if elements
27+
# are not in the intended order
28+
29+
temp=array[i]
30+
array[i]=array[index]
31+
array[index]=temp
32+
33+
# no swapping means the array is already sorted
34+
# so no need for further comparison
35+
if not swapped:
36+
break
37+

sorting.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import matplotlib.animation as animation
77
from bubbleSort import bubbleSort
88
from quickSort import quickSort
9+
from insertionSort import insertionSort
10+
from selectionSort import selectionSort
911

1012

1113
# plt.style.use('dark_background')
@@ -76,8 +78,8 @@ def __repr__(self):
7678
arr = TrackedArray(arr)
7779

7880
# function call for bubble sort
79-
bubbleSort(arr)
80-
81+
# bubbleSort(arr)
82+
insertionSort(arr)
8183
# function call for quick sort
8284
#quickSort(arr, 0, len(arr)-1)
8385

0 commit comments

Comments
 (0)