Skip to content

Commit 4e4888a

Browse files
authored
Merge pull request #72 from yashasvimisra2798/add-Shell-Sort
Create shell_sort.cpp
2 parents 352e37c + 5e4917d commit 4e4888a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

sorting-algorithms/shell_sort.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int shellSort(int arr[], int n)
5+
{
6+
for (int g = n/2; g > 0; g/= 2)
7+
{
8+
9+
for (int i = g; i < n; i += 1)
10+
{
11+
12+
int temp = arr[i];
13+
14+
int j;
15+
for (j = i; j >= g && arr[j - g] > temp; j -= g)
16+
arr[j] = arr[j - g];
17+
18+
19+
arr[j] = temp;
20+
}
21+
}
22+
return 0;
23+
}
24+
25+
void printArray(int arr[], int n)
26+
{
27+
for (int i=0; i<n; i++)
28+
cout << arr[i] << " ";
29+
}
30+
31+
int main()
32+
{
33+
int arr[] = {12, 34, 54, 2, 3}, i;
34+
int n = sizeof(arr)/sizeof(arr[0]);
35+
36+
cout << "Array before sorting:"<<endl;
37+
printArray(arr, n);
38+
39+
shellSort(arr, n);
40+
41+
cout << "\nArray after sorting:"<<endl;
42+
printArray(arr, n);
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)