File tree Expand file tree Collapse file tree 3 files changed +62
-2
lines changed
Expand file tree Collapse file tree 3 files changed +62
-2
lines changed Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 66import matplotlib .animation as animation
77from bubbleSort import bubbleSort
88from 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):
7678arr = 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
You can’t perform that action at this time.
0 commit comments