Describe
The program implements the hill sorting algorithm, which, by non-descending ordering, tests the data as integers.
Input
The first line is the number of data elements to be sorted n;
The second line is the data element to be sorted.
Output
The result of a trip to the sort of hill.
Sample input
1050 36 41 19 23 4 20 18 12 22
Sample output
4 20 18 12 22 50 36 41 19 23
The Hill sort (Shell sort) is a sort of insertion. Also known as narrowing incremental sorting, is a more efficient and improved version of the direct insertion sorting algorithm. Hill Sort is a non-stable sorting algorithm. The method is due to DL. The shell was named after it was introduced in 1959. Hill sort is to group records by a certain increment of the subscript, sorting each group using the direct insertion sorting algorithm; As the increments gradually decrease, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the entire file is divided into a group, the algorithm terminates. [1] and the problem is a trip to the hill sort, that is, the sorting cycle is reduced to one time, the specific code is as follows
#include<iostream>
#include<algorithm>
using
namespace
std;
#define maxsize 1000
void
shellSort(
int
*a,
int
n)
{
int
i,j,gap;
//for(gap=n/2;gap>0;gap/=2)
for
(gap=n/2;gap==n/2;gap/=2)
{
for
(i=gap;i<n;i++)
{
for
(j=i-gap;j>=0&&a[j]>a[j+gap];j-=gap)
{
swap(a[j],a[j+gap]);
}
}
}
}
int
main()
{
int
n;
cin>>n;
int
a[maxsize];
for
(
int
i=0;i<n;i++)
{
cin>>a[i];
}
shellSort(a,n);
for
(
int
i=0;i<n;i++)
{
cout<<a[i]<<
" "
;
}
return
0;
}
Hill Sort algorithm implementation (1099)