Skip to content

Commit f8d4f6d

Browse files
authored
Merge pull request larissalages#75 from avk29/master
Bubble Sort.py
2 parents 0892fa7 + 80c598d commit f8d4f6d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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)

0 commit comments

Comments
 (0)