Skip to content

Commit dec5aff

Browse files
authored
Add files via upload
1 parent 6d192b9 commit dec5aff

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Sorting:
2+
'''
3+
Sorting is any process of arranging items systematically. In Computer Science, algorithms put elements of a list in a certain order. Most frequently used orders are numarically and lexicograghical.
4+
Efficient sorting is important for optimizing the efficiency of other algorithms, which require sorted input or the sort of a given input as part of their process.
5+
'''
6+
# Selection Sort Algorithm
7+
'''
8+
This algorithm works by repeatedly finding the minimum in the list and placing it at the beginning. This way, algorithm maintains two lists:
9+
1. Sublist of already-sorted elements, which is filled from left to right.
10+
2. Sublist of the remaining unsorted elements that needs to be sorted.
11+
12+
In Other words, this algorithm works by iterating over list and swapping each element with minimum(or maximum) element found in insorted list with that in the sorted list.
13+
Below is the code for Selection Sort Algorithm:
14+
'''
15+
def swap(i,min_index, lst):
16+
temp = lst[i]
17+
lst[i] = lst[min_index]
18+
lst[min_index] = temp
19+
20+
def selection_sort(lst):
21+
22+
for i in range(len(lst)):
23+
minimum_num = i
24+
for j in range(i+1, len(lst)):
25+
if lst[minimum_num] > lst[j]:
26+
minimum_num = j
27+
#lst[i] , lst[minimum_num] = lst[minimum_num] , lst[i]
28+
swap(i, minimum_num, lst)
29+
return lst
30+
31+
print("Selection Sort", selection_sort([64, 25, 12, 22, 11]))
32+
# Time Complexity for this Sorting Algorithm is O(n^2) as we have to traverse the list n(n-1)(n-2)...1 times
33+
34+
# Bubble Sort Algorithm:
35+
'''
36+
This is another sorting algorithm also known as 'Sinking Sort'. It works by comparing the adjacent elements and swapping them if they are in the wrong order. This is repeated until list is sorted.
37+
Think of it this way, a bubble passses over the list, takes the max/min element with it to and move it to the right side.
38+
Below is the code for Bubble Sort: It has a Time Complexity of O(n^2)
39+
'''
40+
def Bubble_sort(lst):
41+
for i in range(len(lst)):
42+
for j in range(0, len(lst)-i-1):
43+
if lst[j] > lst[j+1]:
44+
swap(j,j+1, lst)
45+
return lst
46+
47+
lst = [3,2,1,5,4]
48+
print("Bubble Sort: ", Bubble_sort(lst))
49+
50+
#Insertion Sort Algorithm
51+
'''
52+
Insertion sort algorithm works the same way as we would naturally sort the in real life. It itrates over the given list, figures out what the correct position is of every element, insert it there.
53+
Below is the Code: Time Complexity is O(n^2)
54+
'''
55+
def Insertion_sort(lst):
56+
57+
for i in range(1, len(lst)):
58+
key = lst[i]
59+
j = i -1
60+
while j>=0 and key < lst[j]:
61+
lst[j+1] = lst[j]
62+
j-=1
63+
lst[j+1] = key
64+
return lst
65+
lst = [64,99,15,22,11,1,0]
66+
print("Insertion Sort: ", Insertion_sort(lst))
67+
# Note: The Complexity actually n^2 only when the input list reverse sorted. So, the best case complexity (when the list is sorted) is Omega(n)
68+
69+
# Space Complexity: Note that all algorithms are in-place, hence the space complexity is O(1)
70+

0 commit comments

Comments
 (0)