In this program, we are going to share a C++ program to implement shell sort. If you are a beginner and want to start learning the C++ programming, then keep your close attention in this tutorial as I am going to share a program for C++ program to implement shell sort with the output.
Copy the below C++ program and execute it with the help of GCC compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include<iostream> using namespace std; void ShellSort(int a[], int n) { int i, j, k, temp; for(i = n/2; i > 0; i = i/2) { for(j = i; j < n; j++) { for(k = j-i; k >= 0; k = k-i) { if(a[k+i] >= a[k]) break; else { temp = a[k]; a[k] = a[k+i]; a[k+i] = temp; } } } } } int main() { int n, i; cout<<"\nEnter the number of data element to be sorted: "; cin>>n; int arr[n]; for(i = 0; i < n; i++) { cout<<"Enter element "<<i+1<<": "; cin>>arr[i]; } ShellSort(arr, n); cout<<"\nSorted Data "; for (i = 0; i < n; i++) cout<<"->"<<arr[i]; return 0; } |
Enter the number of data element to be sorted: 10
Enter element 1: 9
Enter element 2: 3
Enter element 3: 4
Enter element 4: 6
Enter element 5: 8
Enter element 6: 5
Enter element 7: 1
Enter element 8: 2
Enter element 9: 7
Enter element 10: 0
Sorted Data ->0->1->2->3->4->5->6->7->8->9
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.