// 排序算法练习.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
void show(int *a,int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[8] = {21,25,49,25,16,8,62,38};
const int max = 8;
int i, j, k, gap;
gap = max / 2;
for (; gap >= 1; gap /= 2)
{
for (i = 0; i < max; i++)
{
for (j = i + gap; j < max; j+=gap)
{
if (a[j]<a[j - gap])
{
int temp = a[j];
while (j - gap >= 0 && a[j]<a[j - gap])
a[j] = a[j - gap];
a[j - gap] = temp;
}
}
}
show(a,max);
}
return 0;
//
#include "stdafx.h"
void show(int *a,int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[8] = {21,25,49,25,16,8,62,38};
const int max = 8;
int i, j, k, gap;
gap = max / 2;
for (; gap >= 1; gap /= 2)
{
for (i = 0; i < max; i++)
{
for (j = i + gap; j < max; j+=gap)
{
if (a[j]<a[j - gap])
{
int temp = a[j];
while (j - gap >= 0 && a[j]<a[j - gap])
a[j] = a[j - gap];
a[j - gap] = temp;
}
}
}
show(a,max);
}
return 0;
}
本来想用泛型编程写的,结果BUG调不好。so 只能写个这样简单的了。