//希爾排序算法
//希爾排序算法[cpp] view plaincopyprint?
#include <iostream>
#include<stdio.h>
using namespace std;
int data[] = { 23, 34, 523, 421, 31, 3465, 4, 2341 };
const int n = sizeof(data) / sizeof(int);
//希爾排序
//取間隔d的數進行排序,然後縮小d的范圍,直到d<=0,d的范圍取值d=(d+1)/2;
void ShellSort(int left,int right)
{
int len = right-left+1;
int d=len;
while(d>1)
{
d=(d+1)/2;
for(int i=left;i<right+1-d;i++)
{
if(data[i+d]<data[i])
{
int tmp = data[i+d];
data[i+d]=data[i];
data[i] = tmp;
}
}
}
}
int main()
{
ShellSort(0,n-1);
for(int i=0;i<n;i++)
{
printf("%d\t",data[i]);
}
return 0;
}
#include <iostream>
#include<stdio.h>
using namespace std;
int data[] = { 23, 34, 523, 421, 31, 3465, 4, 2341 };
const int n = sizeof(data) / sizeof(int);
//希爾排序
//取間隔d的數進行排序,然後縮小d的范圍,直到d<=0,d的范圍取值d=(d+1)/2;
void ShellSort(int left,int right)
{
int len = right-left+1;
int d=len;
while(d>1)
{
d=(d+1)/2;
for(int i=left;i<right+1-d;i++)
{
if(data[i+d]<data[i])
{
int tmp = data[i+d];
data[i+d]=data[i];
data[i] = tmp;
}
}
}
}
int main()
{
ShellSort(0,n-1);
for(int i=0;i<n;i++)
{
printf("%d\t",data[i]);
}
return 0;
}