8000 Merge pull request #17 from nywleswoey/master · Deyems/Algorithms-Explanation@8a6be9b · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a6be9b

Browse files
authored
Merge pull request TheAlgorithms#17 from nywleswoey/master
Add description for shell sort
2 parents e5a5955 + 2a59d85 commit 8a6be9b

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

Sorting Algorithms/Shell Sort.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Shell Sort
2+
3+
#### Problem Statement
4+
5+
Given an unsorted array of n elements, write a function to sort the array
6+
7+
#### Approach
8+
9+
- start with the initial gap, g
10+
- go through the first (n - g) elements in the array
11+
- compare the element with the next element that is g distance away
12+
- swap the two elements if the first element is bigger
13+
- decrease the gap and repeat until gap = 1
14+
15+
#### Time Complexity
16+
Time complexity is dependent on the gap sequences.
17+
Below time complexities are based on the gap sequences of n/2^k.
18+
19+
O(n^2) Worst case performance
20+
21+
O(n) Best-case performance
22+
23+
O(n^2) Average performance
24+
25+
#### Space Complexity
26+
27+
O(1) Worst case
28+
29+
#### Founder's Name
30+
31+
Donald Shell
32+
33+
#### Example
34+
35+
```
36+
arr[] = {61, 109, 149, 111, 34, 2, 24, 119}
37+
Initial Gap: 4
38+
39+
1. Index = 0, Next element index = 4
40+
2. 61 > 34, swap 61 and 34
41+
3. The array is now {34, 109, 149, 111, 61, 2, 24, 119}
42+
43+
4. Index = 1, Next element index = 5
44+
5. 109 > 2, swap 109 and 2
45+
6. The array is now {34, 2, 149, 111, 61, 109, 24, 119}
46+
47+
7. Index = 2, Next element index = 6
48+
8. 149 > 24, swap 149 and 24
49+
9. The array is now {34, 2, 24, 111, 61, 109, 149, 119}
50+
51+
10. Index = 3, Next element index = 7
52+
11. 111 < 119, do nothing and continue
53+
54+
12. Divide the gap by 2 and repeat until gap = 1
55+
56+
```
57+
58+
59+
#### Code Implementation Links
60+
61+
- [Java](https://github.com/TheAlgorithms/Java/blob/master/Sorts/ShellSort.java)
62+
- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/Sorting/Shell%20Sort.cpp)
63+
- [Python](https://github.com/TheAlgorithms/Python/blob/master/sorts/shell_sort.py)
64+
- [C-Sharp](https://github.com/TheAlgorithms/C-Sharp/blob/master/sorts/shell_sort.cs)
65+
- [Go](https://github.com/TheAlgorithms/Go/blob/master/sorts/shell_sort.go)
66+
- [Ruby](https://github.com/TheAlgorithms/Ruby/blob/master/Sorting/shell_sort.rb)
67+
- [C](https://github.com/TheAlgorithms/C/blob/master/sorting/shellSort.c)
68+
- [Javascript](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/shellSort.js)
69+
70+
71+
#### Video Explanation
72+
73+
[A video explaining the Shell Sort Algorithm](https://www.youtube.com/watch?v=H8NiFkGu2PY)
74+
75+
#### Others
76+
77+
Shell sort is also known as diminishing increment sort.

0 commit comments

Comments
 (0)
0