// 057.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//思路:从0~99岁用一个数组记录年龄出现的次数。数组的下标即年龄。
//数组总保存的数据就是该年龄出现的次数。
//最后遍历该数组,依次按序保存年龄。
void AgeSort(int *pData, int len)
{
if (NULL == pData || len<=0)
{
return;
}
const int oldestAge = 99;
int timesOfAge[oldestAge+1];//记录每个年龄出现的次数,下标为年龄。
int i=0;
int j=0;
for(i=0; i<oldestAge+1; i++)
{
timesOfAge[i] = 0;
}
for (j=0; j<len; j++)
{
for (i=0; i<oldestAge+1;i++)
{
if (pData[j] == i)
{
++timesOfAge[i];
}
}
}
//依次按序保存年龄。
int index=0;
for (i=0; i<oldestAge+1; i++)
{
for (j=0; j<timesOfAge[i]; j++)
{
pData[index] = i;
index++;
}
}
}
//按员工年龄排序
int main(int argc, char* argv[])
{
int data[10] = {20, 40, 20, 45, 60, 80, 18, 35, 40, 40};
AgeSort(data,10);
for (int i=0; i<10; i++)
{
printf("%d ", data[i]);
}
return 0;
}
057
最新推荐文章于 2024-01-30 08:30:00 发布