标签:
希尔排序
#include<iostream>
#include<stdio.h>
usingnamespace std;
void ShellSort(int *data, intleft, intright)
{
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;
}
}
}
}
void ShellSort2(int *data, intlen)
{
int d = len;
while (d > 1)
{
d = (d + 1) / 2;
for (int i = 0; i < len - d; i++)
{
if (data[i + d] < data[i])
{
int tmp = data[i + d];
data[i + d] = data[i];
data[i] = tmp;
}
}
for (int i = 0; i < 10; i++)
printf("%5d", data[i]);
printf("\n");
}
}
void main()
{
int list[10];
int n = 9, m = 0, i;
printf("input 10 number:");
for (i = 0; i < 10; i++)
scanf("%d", &list[i]);
printf("\n");
ShellSort2(list, 10);
//ShellSort(list,0,9);
printf("\n");
for (i = 0; i < 10; i++)
printf("%5d", list[i]);
printf("\n");
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/wangfengfan1/article/details/47304793