Code:
#include iostream
using namespace std;
// A function implementing Shell sort.
void ShellSort(int a[], int n)
{
int i, j, k, temp;
// Gap 'i' between index of the element to be compared, initially n/2.
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 value at higher index is greater, then break the loop.
if(a[k+i] >= a[k])
break;
// Switch the values otherwise.
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 "<
cin>>arr[i];
}
ShellSort(arr, n);
// Printing the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<
return 0;
}
Output:
Case 1:
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
More C++ Programs: