File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed
classical_algorithms/python Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ def bubblesort (array ):
2
+
3
+ n = len (array )
4
+
5
+ for i in range (0 ,n - 1 ):
6
+ for j in range (0 ,n - i - 1 ):
7
+ if (array [j ] > array [j + 1 ]):
8
+ array [j ] , array [j + 1 ] = array [j + 1 ] , array [j ]
9
+
10
+ print (array )
11
+
12
+
13
+ m = int (input ('Enter the number of elements in the array ' ))
14
+
15
+ mylist = []
16
+
17
+ for i in range (0 ,m ):
18
+ num = int (input ('Enter element: ' ))
19
+ mylist .append (num )
20
+
21
+ bubblesort (mylist )
Original file line number Diff line number Diff line change
1
+ def insertion_sort (array ):
2
+
3
+ for i in range (1 ,len (array )):
4
+ j = i - 1
5
+ key = array [i ]
6
+
7
+ #move all the elements of array[0,i-1] that are greater than
8
+ #the key, one position ahead of their current one
9
+
10
+ while j >= 0 and array [j ]> key :
11
+ array [j + 1 ] = array [j ]
12
+ j = j - 1
13
+
14
+ array [j + 1 ] = key
15
+
16
+ print (array )
17
+
18
+
19
+ m = int (input ('Enter the number of elements in the array ' ))
20
+
21
+ mylist = []
22
+
23
+ for i in range (0 ,m ):
24
+ num = int (input ('Enter element: ' ))
25
+ mylist .append (num )
26
+
27
+ insertion_sort (mylist )
You can’t perform that action at this time.
0 commit comments