Simple Shell Sort Program in C++
Definition
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.
Simple Shell Sort Program Example
/* Simple Shell Sort Program Using Array in C++*/
/* Data Structure C++ Programs,C++ Array Examples */
#include <iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
#define MAX_SIZE 5
int main() {
int arr_sort[MAX_SIZE], i, j, k, a, t;
cout << "Simple C++ Shell Sort Example - Array\n";
cout << "\nEnter " << MAX_SIZE << " Elements for Sorting : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_sort[i];
cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}
for (i = MAX_SIZE / 2; i > 0; i = i / 2) {
for (j = i; j < MAX_SIZE; 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 < MAX_SIZE; a++) {
cout << "\t" << arr_sort[a];
}
}
}
cout << "\n\nSorted Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}
getch();
}
Sample Output
Simple Shell Sort Example - Functions and Array
Enter 5 Elements for Sorting
4556
300
201
10
23
Your Data : 4556 300 201 10 23
Shell Sort Iteration [2:2] 201 300 4556 10 23
Shell Sort Iteration [2:3] 201 10 4556 300 23
Shell Sort Iteration [2:4] 23 10 201 300 4556
Shell Sort Iteration [1:1] 10 23 201 300 4556
Shell Sort Iteration [1:2] 10 23 201 300 4556
Shell Sort Iteration [1:3] 10 23 201 300 4556
Shell Sort Iteration [1:4] 10 23 201 300 4556
Sorted Data : 10 23 201 300 4556
Sorting Programs
- Simple Bubble Sort Program in C++
- Simple Bubble Sort Program using functions in C++
- Simple Insertion Sort Program in C++
- Simple Insertion Sort Program using functions in C++
- Simple Selection Sort Program in C++
- Simple Selection Sort Program using functions in C++
- Simple Quick Sort Program in C++
- Simple Merge Sort Program in C++
- Simple Shell Sort Program in C++
- Simple Shell Sort Program using functions in C++
- Simple Heap Sort Program in C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming