堆排序的应用之优先级队列的实现
2013-10-03 11:31
435 查看
/*
2013年10月3日
by--acton
优先级队列:
1.最大优先级队列的一个应用是在一台分时的计算机上进行作业调度,
这种队列要执行的各个作业及它们之间的的相对优先的关系加以记录,
当一个作业完成或者中断的时候,用Extract_Max将其从就绪的要执行的队列中remove掉,
然后对这个堆进行再次的建立一个大顶堆,然后选出优先级相对较高的程序进行执行,
在任何时候一旦有一个作业进入就绪状态,则将其加入到这个队列当中 use Insert fuction。
2.最小优先级队列的建立,相应的option 包括Insert ,Minmum,Extract_Min 和Decrease_key等,
这种队列一般用于基于事件驱动的模拟器中。在这种应用中,队列中的各项是要模拟的事件,每一个都有发生的时间作为关键字,
事件模拟器要按各事件发生的时间顺序进行,因为模拟某一事件可能导致稍后对其他事件的模拟。模拟程序每次都要在每一部用Extract_Min
选择下一个需要模拟的事件。当产生一个新的事件之后使用Insert将其加入到队列中
*/
2013年10月3日
by--acton
优先级队列:
1.最大优先级队列的一个应用是在一台分时的计算机上进行作业调度,
这种队列要执行的各个作业及它们之间的的相对优先的关系加以记录,
当一个作业完成或者中断的时候,用Extract_Max将其从就绪的要执行的队列中remove掉,
然后对这个堆进行再次的建立一个大顶堆,然后选出优先级相对较高的程序进行执行,
在任何时候一旦有一个作业进入就绪状态,则将其加入到这个队列当中 use Insert fuction。
2.最小优先级队列的建立,相应的option 包括Insert ,Minmum,Extract_Min 和Decrease_key等,
这种队列一般用于基于事件驱动的模拟器中。在这种应用中,队列中的各项是要模拟的事件,每一个都有发生的时间作为关键字,
事件模拟器要按各事件发生的时间顺序进行,因为模拟某一事件可能导致稍后对其他事件的模拟。模拟程序每次都要在每一部用Extract_Min
选择下一个需要模拟的事件。当产生一个新的事件之后使用Insert将其加入到队列中
*/
# include <stdio.h> # include <stdlib.h> # define INIT_HEAP_SIZE 7 # define INFINITY 99999 # define MAX_HEAP_SIZE 20 # define test_main main int Heap_Size = INIT_HEAP_SIZE - 1; // the global varible to define the Heap_Size as the init heap_size , //but it will change with the dynamic options int parent(int i){ return i/2; } int Left(int i ){ return i*2; } int Right(int i){ return i*2+1; } void Exchange(int * p, int * q){ *q = *q + *p; *p = *q - *p; *q = *q - *p; } void Max_Heapfy(int S[], int i){ int l = Left(i); int r = Right(i); int maxer; if(l <= Heap_Size && S[i] < S[l]){ maxer = l ; }else{ maxer = i ; } if(r <= Heap_Size && S[maxer] < S[r]){ maxer = r; } if(maxer != i){ Exchange(&S[maxer],&S[i]); Max_Heapfy(S,maxer); } } void Increase_Key(int S[], int i , int key){ //将元素X的关键字的值增加到K,这里的K值不能小于X的原来的关键字的值 if (key < S[i]){ printf("the value key is little than the value in the location i\n"); return ; } S[i] = key; while (i >= 0 && S[parent(i)] < S[i]){ //adjust to the root or the the parent is larger than //the children specifically is the the newly insert key Exchange(&S[i],&S[parent(i)]); i = parent(i); } } void Insert(int * S, int x){ //把元素X插入集合S,这一操作可以写为S = S U X; Heap_Size ++ ; S[Heap_Size] = - INFINITY ; Increase_Key(S,Heap_Size,x); //return S; } int MaxiMum(int S[]){ //返回S中具有最大关键字的元素 return S[0]; } int Extract_Max(int* S){ //去掉并返回S中的具有最大关键字的元素 //运行的时间的复杂度为O(lgn) if (Heap_Size < 1){ printf("Heap underflow\n"); return -INFINITY; } int max = S[0]; S[0] = S[Heap_Size]; Heap_Size -- ; Max_Heapfy(S,0); return max; } void Build_Max_Heap(int S[]){ for (int i = Heap_Size /2 ; i >= 0 ; i -- ){ Max_Heapfy(S,i); } for (i = 0 ; i <= Heap_Size ; i ++ ){ printf("%5d ",S[i]); } putchar(10); } int test_main(void){ /*int * S = (int * )malloc(sizeof(int)* INIT_HEAP_SIZE); if(!S){ printf("memery error!\n"); exit(-1); }*/ int S[MAX_HEAP_SIZE] = {4,1,2,7,6,9,10}; Build_Max_Heap(S); printf("MAXMUM of S is %d \n",MaxiMum(S)); // printf("MAXMUM return is %d \n",Extract_Max(S)); // 此时会将堆中的最大元素踢走 然后重新对堆进行调整 Insert(S,-1); printf("MAXMUM of S is %d \n",MaxiMum(S)); printf("\nhello"); for (int i = 0 ; i <= Heap_Size ; i ++ ){ printf("%5d ",S[i]); } putchar(10); return 0; }
相关文章推荐
- 堆的实现及应用(优先级队列,堆排,TopK问题)
- W. :利用最小优先级队列实现对k个已序队列的合并排序。习题6.5-8
- [数据结构]用插入排序和选择排序的思想实现优先级队列
- 第六章堆排序之“优先级队列实现先进先出队列和栈”(练习6.5-6)
- 堆排序应用之优先队列的实现
- 第六章堆排序之“优先级队列实现先进先出队列和栈”(练习6.5-6)
- 栈的应用——对栈排序、用栈实现队列的功能
- 堆排序的应用-优先级队列
- 优先级队列的应用 之对扑克牌排序
- 优先级队列的简单实现及STL举例应用
- C++模板链表实现优先级队列
- vector结构体排序与priority优先级队列自动排序对比
- 拓扑排序--九度.1449.[优先级队列实现小顶堆]
- java开发系统内核:实现进程优先级队列
- 基于数组实现Java 自定义Queue队列及应用
- 泛型优先级队列的实现
- 优先级队列的实现
- 普林斯顿公开课 算法4-1:优先级队列API和基本实现
- java实现 数据结构:链表、 栈、 队列、优先级队列、哈希表
- 优先级队列实现