Skip to content

Commit cf8fab1

Browse files
committed
added Sorting Algorithms
1 parent 30e8f4c commit cf8fab1

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

SorthingAlgorithms.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
def insertion_sort(list_to_sort):
2+
for step in range(1, len(list_to_sort)):
3+
key = list_to_sort[step]
4+
j = step - 1
5+
while j >= 0 and list_to_sort[j] > key:
6+
list_to_sort[j + 1] = list_to_sort[j]
7+
j -= 1
8+
list_to_sort[j + 1] = key
9+
10+
11+
def quick_sort(list_to_sort):
12+
def partition(sub_list, low, hi):
13+
pivot = sub_list[(low + hi) // 2]
14+
left = low
15+
right = hi
16+
while left <= right:
17+
while sub_list[left] < pivot:
18+
left += 1
19+
while sub_list[right] > pivot:
20+
right -= 1
21+
if left <= right:
22+
sub_list[left], sub_list[right] = sub_list[right], sub_list[left]
23+
left += 1
24+
right -= 1
25+
return left, right
26+
27+
def quick_sort_fun(list_to_sort, low, hi):
28+
if low < hi:
29+
left, right = partition(list_to_sort, low, hi)
30+
quick_sort_fun(list_to_sort, low, right)
31+
quick_sort_fun(list_to_sort, left, hi)
32+
33+
quick_sort_fun(list_to_sort, 0, len(list_to_sort) - 1)
34+
35+
36+
def main():
37+
list_to_sort = [5, 5, 1, 78, 99, 2, 3, 7]
38+
print(list_to_sort)
39+
quick_sort(list_to_sort)
40+
print(list_to_sort)
41+
42+
list_to_sort_2 = ["Bob", "Hannah", "Alice", "Peter", "Paul"]
43+
print(list_to_sort_2)
44+
insertion_sort(list_to_sort_2)
45+
print(list_to_sort_2)
46+
47+
48+
if __name__ == "__main__":
49+
main()

0 commit comments

Comments
 (0)