1:最理想、最简单,如果数据在Mysql 那边单一数据库, 那么比较简单例如 表明playerranking
表结构
id int
varchar name
degree int
degree字段做为索引
select * from playerranking order by degree limit 100
2:第一种情况是:
环境在Linux ubuntu下
100w的数据量也是大数据量操作,所以考虑采用内存映射。
3:采用堆排序,100个节点的堆,跟最小的比较,如果比最小的大,那么就把它替换了,然后再把整个堆进行排序一下
我写了一个测试例子,并且测试的排序的时间
排序有几种 快速排序
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
struct stPlayer
{
int charid;
int degree;
};
#define MAX_PLAYER 100000000
//小根堆排序
//显示前100名
void heap_show(stPlayer *data)
{
if(data == NULL)
{
return;
}
for(int i = 0; i < 100; i++)
{
printf(" 角色数据排行榜%d %d \r\n",data[i].charid, data[i].degree);
}
}
void max_heapify(stPlayer *data,int i,int heapsize)//以某个节点为根节点的子树进行调整,调整为大顶堆
{
int left = 2*i+1;
int right = 2*i+2;
int largest = i;
if(left < heapsize && data[left].degree > data[i].degree)
{
largest=left;
}
if(right < heapsize && data[right].degree > data[largest].degree)
{
largest=right;
}
if(largest != i)
{
stPlayer temp;
temp.charid = data[largest].charid;
temp.degree = data[largest].degree;
data[largest].charid = data[i].charid;
data[largest].degree = data[i].degree;
data[i].charid = temp.charid;
data[i].degree = temp.degree;
max_heapify(data,largest,heapsize);
}
}
void bulid_max_heap(stPlayer *data,int heapsize)//建堆的过程,通过自底向上地调用max_heapify来将一个数组data【1……n】变成一个大顶堆,
{
if(data == NULL)
{
return;
}
//只需要对除了叶子节点以外的节点进行调整
for(int i=heapsize/2-1;i>=0;i--)
max_heapify(data, i, heapsize);
}
void heap_sort(stPlayer *data,int heapsize)
{
//堆排序算法实现主体:先用bulid_max_heap将输入数组构造成大顶堆
//然后将data【0】和堆的最后一个元数交换,继续进行调整。
bulid_max_heap(data,heapsize);
for(int i=heapsize-1;i>0;i--)
{
stPlayer temp;
temp.charid = data[0].charid;
temp.degree = data[0].degree;
data[0].charid = data[i].charid;
data[0].degree = data[i].degree;
data[i].charid = temp.charid;
data[i].degree = temp.degree;
max_heapify(data,0,i);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
srand( (unsigned)time( NULL ) );
stPlayer *player = (stPlayer *)malloc(MAX_PLAYER*sizeof(stPlayer));
stPlayer *ranking = (stPlayer *)malloc(100*sizeof(stPlayer));
for(int i = 0; i <MAX_PLAYER; i++ )
{
player[i].charid = i;
player[i].degree = rand();
}
for(int i = 0; i < 100; i++ )
{
ranking[i].charid = player[i].charid;
ranking[i].degree = player[i].degree;
}
heap_show(ranking);
heap_sort(ranking, 100);
heap_show(ranking);
for(int i = 100; i < MAX_PLAYER; i++ )
{
if(ranking[0].degree< player[i].degree)
{
ranking[0].charid = player[i].charid;
ranking[0].degree = player[i].degree;
heap_sort(ranking, 100);
}
}
printf("排序后\r\n");
heap_show(ranking);
free(player);
free(ranking);
system("PAUSE");
return 0;
}
4:思路是这样,把问题分解、细化,如果是100w的数据量,那么就分成1w分数据
1份:0-100 2份:100-200 3份: 200-300 4份 400-500 ....
1份 合并 100份 2份 合并 99份 3份合并98....依次类推 也算作一个归并算法
归并到最后就是一个结果,算法找个时间 我也是先一下