Skip to content

Commit de9a403

Browse files
authored
Added Shell sort (jainaman224#44)
1 parent b4f4f26 commit de9a403

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

Shell_Sort/Shell_Sort.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// C++ implementation of Shell Sort
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
int Shell_Sort(int array[], int size)
7+
{
8+
for(int gap = size / 2; gap > 0; gap /= 2)
9+
{
10+
for(int i = gap; i < size; i++)
11+
{
12+
int temp = array[i], j;
13+
14+
for(j = i; j >= gap && array[j - gap] > temp; j -= gap)
15+
array[j] = array[j - gap];
16+
17+
array[j] = temp;
18+
}
19+
}
20+
21+
return 0;
22+
}
23+
24+
void Print_Array(int array[], int size)
25+
{
26+
for(int i = 0; i < size; i++)
27+
cout << array[i] << " ";
28+
29+
cout << endl;
30+
}
31+
32+
int main()
33+
{
34+
int array[] = {12, 34, 54, 2, 3};
35+
int size = 5;
36+
37+
Shell_Sort(array, size);
38+
Print_Array(array, size);
39+
40+
return 0;
41+
}
42+
43+
44+
/* Output
45+
46+
2 3 12 34 54
47+
48+
*/

Shell_Sort/Shell_Sort.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def Shell_Sort(array):
2+
size = len(array)
3+
gap = int(size / 2)
4+
5+
while gap > 0:
6+
for i in range(gap, size):
7+
temp = array[i]
8+
9+
j = i
10+
while j >= gap and array[j - gap] > temp:
11+
array[j] = array[j - gap]
12+
j -= gap
13+
14+
array[j] = temp
15+
gap = int(gap / 2)
16+
17+
def Print_Array(array):
18+
for i in range(len(array)):
19+
print(array[i], end = " ")
20+
21+
print()
22+
23+
array = [12, 34, 54, 2, 3]
24+
25+
Shell_Sort(array)
26+
Print_Array(array)
27+
28+
29+
''' Output
30+
31+
2 3 12 34 54
32+
33+
'''

0 commit comments

Comments
 (0)