fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // A function implementing Shell sort.
  5. void ShellSort(int a[], int n)
  6. {
  7. int i, j, k, temp;
  8. // Gap 'i' between index of the element to be compared, initially n/2.
  9. for(i = n/2; i > 0; i = i/2)
  10. {
  11. for(j = i; j < n; j++)
  12. {
  13. for(k = j-i; k >= 0; k = k-i)
  14. {
  15. // If value at higher index is greater, then break the loop.
  16. if(a[k+i] >= a[k])
  17. break;
  18. // Switch the values otherwise.
  19. else
  20. {
  21. temp = a[k];
  22. a[k] = a[k+i];
  23. a[k+i] = temp;
  24. }
  25. }
  26. }
  27. }
  28. }
  29. int main()
  30. {
  31. int n, i;
  32. cout<<"\nEnter the number of data element to be sorted: ";
  33. cin>>n;
  34.  
  35. int arr[n];
  36. for(i = 0; i < n; i++)
  37. {
  38. cout<<"Enter element "<<i+1<<": ";
  39. cin>>arr[i];
  40. }
  41.  
  42. ShellSort(arr, n);
  43.  
  44. // Printing the sorted data.
  45. cout<<"\nSorted Data ";
  46. for (i = 0; i < n; i++)
  47. cout<<"->"<<arr[i];
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Enter the number of data element to be sorted: 
Sorted Data