C++ Programming Code Examples C++ > Sorting Searching Code Examples Simple Shell Sort Program in C++ Simple Shell Sort Program in C++ Shellsort, also known as Shell sort or Shell's method, is an in-place comparison sort. It can be seen as either a generalization of sorting by exchange or sorting by insertion. The method starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared. #include <iostream> #include<conio.h> #include<stdlib.h> using namespace std; #define maxsize 5 int main() { int arr_sort[maxsize], i, j, k, a, t; cout << "Simple C++ Shell Sort Example - Array\n"; cout << "\nEnter " << maxsize << " Elements for Sorting : " << endl; for (i = 0; i < maxsize; i++) cin >> arr_sort[i]; cout << "\nYour Data :"; for (i = 0; i < maxsize; i++) { cout << "\t" << arr_sort[i]; } for (i = maxsize / 2; i > 0; i = i / 2) { for (j = i; j < maxsize; j++) { for (k = j - i; k >= 0; k = k - i) { if (arr_sort[k + i] >= arr_sort[k]) break; else { //Swapping Values t = arr_sort[k]; arr_sort[k] = arr_sort[k + i]; arr_sort[k + i] = t; } } cout << "\nShell Sort Iteration " << i << " : " << j; for (a = 0; a < maxsize; a++) { cout << "\t" << arr_sort[a]; } } } cout << "\n\nSorted Data :"; for (i = 0; i < maxsize; i++) { cout << "\t" << arr_sort[i]; } getch(); }