Skip to content

Commit 90e0e1b

Browse files
sorting
1 parent 4bca8e7 commit 90e0e1b

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

Sorting/bubble_sort.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
Selction Sort
3+
4+
Time Complexity : O(n^2)
5+
Best Case : O(n)
6+
Average Case : O(n^2)
7+
Worst Case : O(n^2)
8+
9+
Space Complexity : O(1)
10+
'''
11+
12+
13+
def bubble_sort(A):
14+
n = len(A)
15+
for k in range(1, n):
16+
flag = 0
17+
for i in range(0, n-k):
18+
if A[i] > A[i+1]:
19+
A[i], A[i+1] = A[i+1], A[i]
20+
flag = 1
21+
22+
if flag == 0:
23+
break
24+
return A
25+
26+
A = [2, 7, 4, 1, 5, 3]
27+
print(bubble_sort(A))

Sorting/insertion_sort.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
Insertion Sort
3+
4+
Time Complexity : O(n^2)
5+
Best Case : O(n)
6+
Average Case : O(n^2)
7+
Worst Case : O(n^2)
8+
9+
Space Complexity : O(1)
10+
'''
11+
12+
13+
def insertion_sort(A):
14+
n = len(A)
15+
for i in range(1, n):
16+
value = A[i]
17+
hole = i
18+
while hole > 0 and A[hole - 1] > value:
19+
A[hole] = A[hole - 1]
20+
hole -= 1
21+
A[hole] = value
22+
return A
23+
24+
A = [2, 7, 4, 1, 5, 3]
25+
print(insertion_sort(A))
File renamed without changes.
File renamed without changes.

Sorting/selection_sort.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Selction Sort
3+
4+
Time Complexity : O(n^2)
5+
Space Complexity : O(1)
6+
'''
7+
8+
9+
def selection_sort(A):
10+
n = len(A)
11+
for i in range(0, n-1):
12+
i_min = i
13+
for j in range(i+1, n):
14+
if A[j] < A[i_min]:
15+
i_min = j
16+
17+
A[i], A[i_min] = A[i_min], A[i]
18+
return A
19+
20+
A = [2, 7, 4, 1, 5, 3]
21+
print(selection_sort(A))

0 commit comments

Comments
 (0)