File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def brick_sort(arr, n):
2+
# Initially array is unsorted
3+
isSorted = 0
4+
while isSorted == 0:
5+
isSorted = 1
6+
temp = 0
7+
for i in range(1, n-1, 2):
8+
if arr[i] > arr[i+1]:
9+
arr[i], arr[i+1] = arr[i+1], arr[i]
10+
isSorted = 0
11+
12+
for i in range(0, n-1, 2):
13+
if arr[i] > arr[i+1]:
14+
arr[i], arr[i+1] = arr[i+1], arr[i]
15+
isSorted = 0
16+
17+
return
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
Created by: Pratik Narola (https://.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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Created by: Pratik Narola (https://.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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from insertion_sort import sort
2+
# iterative Timsort function to sort the
3+
# array[0...n-1] (similar to merge sort)
4+
def tim_sort(arr, n):
5+
6+
# Sort individual subarrays of size RUN
7+
for i in range(0, n, RUN):
8+
sort(arr, i, min((i+31), (n-1)))
9+
10+
# start merging from size RUN (or 32). It will merge
11+
# to form size 64, then 128, 256 and so on ....
12+
size = RUN
13+
while size < n:
14+
15+
# pick starting point of left sub array. We
16+
# are going to merge arr[left..left+size-1]
17+
# and arr[left+size, left+2*size-1]
18+
# After every merge, we increase left by 2*size
19+
for left in range(0, n, 2*size):
20+
21+
# find ending point of left sub array
22+
# mid+1 is starting point of right sub array
23+
mid = left + size - 1
24+
right = min((left + 2*size - 1), (n-1))
25+
26+
# merge sub array arr[left.....mid] &
27+
# arr[mid+1....right]
28+
merge(arr, left, mid, right)
29+
30+
size = 2*size
31+
32+
def merge(arr, l, m, r):
33+
34+
# original array is broken in two parts
35+
# left and right array
36+
len1, len2 = m - l + 1, r - m
37+
left, right = [], []
38+
for i in range(0, len1):
39+
left.append(arr[l + i])
40+
for i in range(0, len2):
41+
right.append(arr[m + 1 + i])
42+
43+
i, j, k = 0, 0, l
44+
# after comparing, we merge those two array
45+
# in larger sub array
46+
while i < len1 and j < len2:
47+
48+
if left[i] <= right[j]:
49+
arr[k] = left[i]
50+
i += 1
51+
52+
else:
53+
arr[k] = right[j]
54+
j += 1
55+
56+
k += 1
57+
58+
# copy remaining elements of left, if any
59+
while i < len1:
60+
61+
arr[k] = left[i]
62+
k += 1
63+
i += 1
64+
65+
# copy remaining element of right, if any
66+
while j < len2:
67+
arr[k] = right[j]
68+
k += 1
69+
j += 1

0 commit comments

Comments
 (0)