Skip to content

Commit 8824771

Browse files
committed
Shell Sort
1 parent 6eb545a commit 8824771

11 files changed

+92
-3
lines changed

DOC/Shell_Sort.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Shell Sort
2+
3+
Shell Sort is mainly a variation of Insertion Sort. In insertion sort, we move elements only one position ahead. When an element has to be moved far ahead, many movements are involved. The idea of shell sort, we make the array h-sorted for a large value of h. We keep reducing the value of h until it becomes 1. An array is said to be h-sorted if all sublists of every h'th element is sorted.
4+
5+
## Implementation
6+
7+
```python
8+
def shell_sort(data, draw_data, time_tick):
9+
```
10+
11+
Initialize the gap
12+
13+
```python
14+
gap = len(data) // 2
15+
while gap > 0:
16+
i = 0
17+
j = gap
18+
```
19+
20+
Check the array in from left to right till the last possible index of j.
21+
22+
```python
23+
while j < len(data):
24+
if data[i] > data[j]:
25+
data[i], data[j] = data[j], data[i]
26+
i += 1
27+
j += 1
28+
```
29+
30+
Now we cannot move towards left anymore so check the element from last index of i towards to left side of the array.
31+
32+
```python
33+
while i - gap != -1:
34+
if data[i - gap] > data[i]:
35+
data[i - gap], data[i] = data[i], data[i - gap]
36+
i -= 1
37+
gap //= 2
38+
```
39+
40+
Draw the data being compared and the finalized.
41+
42+
```python
43+
draw_data(data, [PURPLE for x in range(len(data))])
44+
time.sleep(time_tick)
45+
return draw_data(data, [BLUE for x in range(len(data))])
46+
```
47+
48+
## Example
49+
50+
<p align="center">
51+
<img src="../images/shell_sort_1.jpg" width="75%" />
52+
<img src="../images/shell_sort_2.jpg" width="75%" />
53+
<img src="../images/shell_sort_3.jpg" width="75%" />
54+
<img src="../images/shell_sort_4.jpg" width="75%" />
55+
<img src="../images/shell_sort_5.jpg" width="75%" />
56+
<img src="../images/shell_sort_6.jpg" width="75%" />
57+
</p>

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ algorithm_list = [
7676
'Counting Sort',
7777
'Radix Sort',
7878
'Heap Sort',
79-
'Bucket Sort'
79+
'Bucket Sort',
80+
'Shell Sort'
8081
]
8182
```
8283

@@ -247,6 +248,7 @@ from algorithms.countingSort import counting_sort
247248
from algorithms.radixSort import radix_sort
248249
from algorithms.heapSort import heap_sort
249250
from algorithms.bucketSort import bucket_sort
251+
from algorithms.shellSort import shell_sort
250252
```
251253

252254
```python
@@ -271,6 +273,8 @@ def sort():
271273
heap_sort(data, draw_data, time_tick)
272274
elif algorithm_menu.get() == 'Bucket Sort':
273275
bucket_sort(data, len(data), draw_data, time_tick)
276+
elif algorithm_menu.get() == 'Shell Sort':
277+
shell_sort(data, draw_data, time_tick)
274278
else:
275279
pass
276280
```
@@ -301,4 +305,6 @@ GeeksforGeeks. (2020, 30 diciembre). Python Program for Heap Sort. https://www.g
301305

302306
Bucket Sort Tutorials & Notes | Algorithms. (2016, 24 abril). HackerEarth. https://www.hackerearth.com/practice/algorithms/sorting/bucket-sort/tutorial/
303307

304-
GeeksforGeeks. (2021, 8 junio). Bucket Sort. https://www.geeksforgeeks.org/bucket-sort-2/
308+
GeeksforGeeks. (2021, 8 junio). Bucket Sort. https://www.geeksforgeeks.org/bucket-sort-2/
309+
310+
GeeksforGeeks. (2021, 28 junio). ShellSort. https://www.geeksforgeeks.org/shellsort/
868 Bytes
Binary file not shown.

algorithms/shellSort.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import time
2+
from colors import *
3+
4+
5+
def shell_sort(data, draw_data, time_tick):
6+
gap = len(data) // 2
7+
while gap > 0:
8+
i = 0
9+
j = gap
10+
while j < len(data):
11+
if data[i] > data[j]:
12+
data[i], data[j] = data[j], data[i]
13+
i += 1
14+
j += 1
15+
while i - gap != -1:
16+
if data[i - gap] > data[i]:
17+
data[i - gap], data[i] = data[i], data[i - gap]
18+
i -= 1
19+
gap //= 2
20+
draw_data(data, [PURPLE for x in range(len(data))])
21+
time.sleep(time_tick)
22+
return draw_data(data, [BLUE for x in range(len(data))])

images/shell_sort_1.jpg

96.4 KB
Loading

images/shell_sort_2.jpg

104 KB
Loading

images/shell_sort_3.jpg

60.2 KB
Loading

images/shell_sort_4.jpg

63.8 KB
Loading

images/shell_sort_5.jpg

95.6 KB
Loading

images/shell_sort_6.jpg

58.7 KB
Loading

main.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from algorithms.radixSort import radix_sort
1313
from algorithms.heapSort import heap_sort
1414
from algorithms.bucketSort import bucket_sort
15+
from algorithms.shellSort import shell_sort
1516

1617

1718
root = Tk()
@@ -30,7 +31,8 @@
3031
'Counting Sort',
3132
'Radix Sort',
3233
'Heap Sort',
33-
'Bucket Sort'
34+
'Bucket Sort',
35+
'Shell Sort'
3436
]
3537

3638
speed_name = StringVar()
@@ -95,6 +97,8 @@ def sort():
9597
heap_sort(data, draw_data, time_tick)
9698
elif algorithm_menu.get() == 'Bucket Sort':
9799
bucket_sort(data, len(data), draw_data, time_tick)
100+
elif algorithm_menu.get() == 'Shell Sort':
101+
shell_sort(data, draw_data, time_tick)
98102
else:
99103
pass
100104

0 commit comments

Comments
 (0)