added few sorting algorithms (#95) · OmkarPathak/pygorithm@953c2d4 · GitHub
Skip to content

Commit 953c2d4

Browse files
PratiknarolaOmkarPathak
authored andcommitted
added few sorting algorithms (#95)
* Create brick_sort.py * Create tim_sort.py * Create cocktail_sort.py * Create gnome_sort.py * Update cocktail_sort.py * Update cocktail_sort.py * Update cocktail_sort.py * Update cocktail_sort.py * Update cocktail_sort.py * Update gnome_sort.py * Update tim_sort.py * Update tim_sort.py
1 parent cb83b18 commit 953c2d4

4 files changed

Lines changed: 175 additions & 0 deletions

File tree

‎pygorithm/sorting/brick_sort.py‎

Lines changed: 17 additions & 0 deletions

‎pygorithm/sorting/cocktail_sort.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
Created by: Pratik Narola (https://github.com/Pratiknarola)
3+
last modified: 14-10-2019
4+
'''
5+
6+
'''
7+
8+
Cocktail Sort is a variation of Bubble sort.
9+
The Bubble sort algorithm always traverses elements from left
10+
and moves the largest element to its correct position in first iteration
11+
and second largest in second iteration and so on.
12+
Cocktail Sort traverses through a given array in both directions alternatively.
13+
14+
'''
15+
16+
def cocktail_sort(a):
17+
n = len(a)
18+
swapped = True
19+
start = 0
20+
end = n-1
21+
while swapped:
22+
23+
# reset the swapped flag on entering the loop,
24+
# because it might be true from a previous
25+
# iteration.
26+
swapped = False
27+
28+
# loop from left to right same as the bubble
29+
# sort
30+
for i in range(start, end):
31+
if a[i] > a[i + 1]:
32+
a[i], a[i + 1] = a[i + 1], a[i]
33+
swapped = True
34+
35+
# if nothing moved, then array is sorted.
36+
if not swapped:
37+
break
38+
39+
# otherwise, reset the swapped flag so that it
40+
# can be used in the next stage
41+
swapped = False
42+
43+
# move the end point back by one, because
44+
# item at the end is in its rightful spot
45+
end -= 1
46+
47+
# from right to left, doing the same
48+
# comparison as in the previous stage
49+
for i in range(end-1, start-1, -1):
50+
if a[i] > a[i + 1]:
51+
a[i], a[i + 1] = a[i + 1], a[i]
52+
swapped = True
53+
54+
# increase the starting point, because
55+
# the last stage would have moved the next
56+
# smallest number to its rightful spot.
57+
start = start + 1

‎pygorithm/sorting/gnome_sort.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Created by: Pratik Narola (https://github.com/Pratiknarola)
3+
last modified: 14-10-2019
4+
'''
5+
6+
'''
7+
8+
Gnome Sort also called Stupid sort is based on the concept of a Garden Gnome sorting his flower pots.
9+
A garden gnome sorts the flower pots by the following method-
10+
11+
He looks at the flower pot next to him and the previous one;
12+
if they are in the right order he steps one pot forward, otherwise he swaps them and steps one pot backwards.
13+
If there is no previous pot (he is at the starting of the pot line), he steps forwards;
14+
if there is no pot next to him (he is at the end of the pot line), he is done.
15+
16+
'''
17+
18+
19+
20+
# A function to sort the given list using Gnome sort
21+
def gnome_sort( arr, n):
22+
index = 0
23+
while index < n:
24+
if index == 0:
25+
index = index + 1
26+
if arr[index] >= arr[index - 1]:
27+
index = index + 1
28+
else:
29+
arr[index], arr[index-1] = arr[index-1], arr[index]
30+
index = index - 1
31+
32+
return arr

‎pygorithm/sorting/tim_sort.py‎

Lines changed: 69 additions & 0 deletions

0 commit comments

Comments
 (0)