按年龄分类人群的最快方法是什么? [英] What would be the fastest way to sort an array of people by age?

查看:147
本文介绍了按年龄分类人群的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一系列结构

struct
{
string name;
string 2nd_name;
int age; // 0 to 150
}

最大数组的长度为10 ^ 8。

Maximum array's length is 10^8.

我知道我可以使用mergesort / quicksort以及所有其他众所周知的算法,但是我想知道是否可以添加其他东西来加快排序。 / p>

I know I could use mergesort/quicksort and all the others well known algorithms, however I'd like to know if It's possible to add something else that would speed up the sorting.

推荐答案

人们的年龄与用于排序的任意整数有些不同:它具有极少数可能的不同值(所有人的年龄都在(介于0和150之间)。因此,对其进行排序的最快方法是分配151个链接列表(我们将其称为存储桶),然后根据每个人的年龄将其数据结构放入存储桶中。

People's age is somewhat different than arbitrary integers for sorting: it has a very small number of possible distinct values (all people's ages are between 0 and 150). So the quickest way to sort it would be to allocate 151 linked lists (let's call them buckets) and put each person's data structure in the bucket according to his/her age:

bucket[person->age].add(person)

这篇关于按年龄分类人群的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆