Skip to content

Instantly share code, notes, and snippets.

@pvcodes-zz
Created April 8, 2021 16:17
Show Gist options
  • Save pvcodes-zz/010ca9888ceeaedb4f4211aa3af398b8 to your computer and use it in GitHub Desktop.
Save pvcodes-zz/010ca9888ceeaedb4f4211aa3af398b8 to your computer and use it in GitHub Desktop.
Shell Sort C++ Implementation
#include <iostream>
using namespace std;
void shellSort(int A[], int n) {
for (int gap = n / 2; gap >= 1; gap /= 2) {
for (int j = gap; j < n; j++) {
int temp = A[j];
int i = j - gap;
while (i >= 0 && A[i] > temp) {
A[i + gap] = A[i];
i = i - gap;
}
A[i + gap] = temp;
}
}
}
int main() {
int A[] = {2, 5, 8, 12, 3, 6, 7, 10};
int n = sizeof(A) / sizeof(A[0]);
for (auto i : A)
cout << i << " ";
puts("");
shellSort(A, n);
for (auto i : A)
cout << i << " ";
puts("");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment