Skip to content

Commit ab65a39

Browse files
ayush246cclauss
authored andcommitted
Double sort (Added with required updates) (#1399)
* Added with required updates * Updated * required updates * Update double_sort.py * Update double_sort.py
1 parent dbf904f commit ab65a39

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

sorts/double_sort.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def double_sort(lst):
2+
"""this sorting algorithm sorts an array using the principle of bubble sort ,
3+
but does it both from left to right and right to left ,
4+
hence i decided to call it "double sort"
5+
:param collection: mutable ordered sequence of elements
6+
:return: the same collection in ascending order
7+
Examples:
8+
>>> double_sort([-1 ,-2 ,-3 ,-4 ,-5 ,-6 ,-7])
9+
[-7, -6, -5, -4, -3, -2, -1]
10+
>>> double_sort([])
11+
[]
12+
>>> double_sort([-1 ,-2 ,-3 ,-4 ,-5 ,-6])
13+
[-6, -5, -4, -3, -2, -1]
14+
>>> double_sort([-3, 10, 16, -42, 29]) == sorted([-3, 10, 16, -42, 29])
15+
True
16+
"""
17+
no_of_elements=len(lst)
18+
for i in range(0,int(((no_of_elements-1)/2)+1)): # we dont need to traverse to end of list as
19+
for j in range(0,no_of_elements-1):
20+
if (lst[j+1]<lst[j]): # applying bubble sort algorithm from left to right (or forwards)
21+
temp=lst[j+1]
22+
lst[j+1]=lst[j]
23+
lst[j]=temp
24+
if (lst[no_of_elements-1-j]<lst[no_of_elements-2-j]): # applying bubble sort algorithm from right to left (or backwards)
25+
temp=lst[no_of_elements-1-j]
26+
lst[no_of_elements-1-j]=lst[no_of_elements-2-j]
27+
lst[no_of_elements-2-j]=temp
28+
return lst
29+
if __name__ == "__main__":
30+
print("enter the list to be sorted")
31+
lst = [int(x) for x in input().split()] # inputing elements of the list in one line
32+
sorted_lst=double_sort(lst)
33+
print("the sorted list is")
34+
print(sorted_lst)

0 commit comments

Comments
 (0)