#include <iostream>
#include <stdio.h>
#include <stdlib.h>
void printArray(int* pArray, int len)
{
printf("array[] = {");
for (int i = 0; i < len; i++)
{
if (i != len - 1)
{
printf("%d,", *(pArray + i));
}
else
{
printf("%d}\n", *(pArray + i));
}
}
return;
}
int main(int argc, char** argv)
{
// 排序数组
int array[] = {11,22,29,24,55,3,38,8,19,1,53};
int iArrayLen = sizeof(array)/sizeof(array[0]);
int h = 1;
int temp = 0;
while (h < iArrayLen/3)
{
h = 3 * h + 1;
}
while (h >= 1)
{
for (int i = h; i < iArrayLen; i++)
{
for (int j = i; j >= h; j -= h)
{
if (array[j-h] > array[j])
{
temp = array[j-h];
array[j-h] = array[j];
array[j] = temp;
}
else
{
break;
}
}
}
printArray(array, iArrayLen);
h = h / 3;
}
return 0;
}
希尔排序C++
最新推荐文章于 2024-08-19 18:41:12 发布