Skip to content

Commit e8e9a23

Browse files
author
Sanghyeon Park
committed
ShellSort
1 parent 92b441c commit e8e9a23

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

‎Sorting/ShellSort/ShellSort‎

23.5 KB
Binary file not shown.

‎Sorting/ShellSort/ShellSort.cpp‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#pragma region LICENSE
2+
// Copyright 2019 PaleCosmos
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
#pragma endregion
16+
#include <iostream>
17+
#define size 15
18+
19+
using namespace std;
20+
21+
void insertion(int *, int, int, int);
22+
23+
void shellSort(int *arr)
24+
{
25+
int gap, i;
26+
27+
for (gap = size / 2; gap > 0; gap = gap / 2)
28+
{
29+
if ((gap % 2) == 0)
30+
{
31+
gap++;
32+
}
33+
34+
for (i = 0; i < gap; i++)
35+
{
36+
insertion(arr, i, size - 1, gap);
37+
}
38+
}
39+
}
40+
41+
void print(int *arr, string define)
42+
{
43+
cout << define << '\n';
44+
cout << "[";
45+
for (int k = 0; k < size; k++)
46+
{
47+
cout << arr[k];
48+
if (k != size - 1)
49+
{
50+
cout << ",";
51+
}
52+
}
53+
cout << "]\n";
54+
}
55+
56+
int main()
57+
{
58+
int arr[size]{8, 11, 2, 9, 5, 14, 0, 13, 10, 3, 6, 1, 4, 7, 12};
59+
60+
print(arr, "Before");
61+
62+
shellSort(arr);
63+
64+
print(arr, "\nAfter");
65+
66+
return 0;
67+
}
68+
69+
void insertion(int *arr, int first, int last, int gap)
70+
{
71+
int j, key;
72+
73+
for (int i = first + gap; i <= last; i += gap)
74+
{
75+
key = arr[i];
76+
77+
for (j = i - gap; j >= first && arr[j] > key; j -= gap)
78+
{
79+
arr[j + gap] = arr[j];
80+
}
81+
82+
arr[j + gap] = key;
83+
}
84+
}

0 commit comments

Comments
 (0)