//参考链接:http://blog.chinaunix.net/uid-25324849-id-2182916.html #include <iostream> using namespace std; const int N = 10000000; const int FILE_NUM = 5; const int MAX_PART = 1000000; const int MIN = -1; //最小值, 必须比要排序数字的最小值要小,否则出错 const int MAX = N + 1; //最大值 ,必须比要排序数字的最大值要大,否则出错 //调整 void adjust(int ls[], int data[], int s) { int t = (s + FILE_NUM)/2; while (t) { if (data[s] > data[ls[t]]) { int temp = s; s = ls[t]; ls[t] = temp; } t /= 2; } ls[0] = s; } void create_loser_tree(int ls[], int data[]) { data[FILE_NUM] = MIN; for (int i = 0; i < FILE_NUM; i++) { ls[i] = FILE_NUM; } for (int i = FILE_NUM - 1; i >= 0; i--) { adjust(ls, data, i); } } void merge_sort_by_losertree(int *data) { int ls[FILE_NUM]; //存放败者索引的节点 create_loser_tree(ls, data); //创建败者树 for(int i = 0;i<FILE_NUM;i++) cout << data[i] << " "; cout << " min:" ; cout << data[ls[0]] << endl; data[4] = 3; adjust(ls, data, ls[0]); for(int i = 0;i<FILE_NUM;i++) cout << data[i] << " "; cout << " min:" ; cout << data[ls[0]] << endl; } int main() { int data[10+1] = {13,5,6,4,99,9,12,17,13,4}; merge_sort_by_losertree(data); return 0; }