Skip to content

Commit

Permalink
Add Shell Sort
Browse files Browse the repository at this point in the history
  • Loading branch information
NagaChaitanya Vellanki committed Nov 12, 2012
1 parent 46b56c9 commit 3e9aa04
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions shell_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include <algorithm>
#include <array>

using namespace std;

int main() {
char arr[] = {'S', 'H', 'E', 'L', 'L', 'S', 'O', 'R', 'T', 'E', 'X', 'A', 'M', 'P', 'L', 'E'};
int N = sizeof(arr);
int h = 1;

while(h < N/3) {
h = 3*h + 1;
}

while(h >= 1) {
for(int i = h; i < N; i++) {
for(int j = i; j >= h && (arr[j] < arr[j-h]); j -= h) {
swap(arr[j], arr[j-h]);
}
}
h = h/3;
}

for(int i = 0; i < N; i++) {
cout << arr[i] << " -- ";
}
cout << endl;

return 0;
}

0 comments on commit 3e9aa04

Please sign in to comment.