堆排序

it2022-05-24  66

void Max_Heapify(int *a, int heap_size, int i){   int left = (i<<1) + 1;   int right = (i<<1) + 2;   int largest = i;   while(left<heap_size || right<heap_size)   {       if(left<heap_size && a[largest]<a[left])       {           largest = left;       }       if(right<heap_size && a[largest]<a[right])       {           largest = right;       }       if(i != largest)       {           int temp = a[largest];           a[largest] = a[i];           a[i] = temp;           i = largest;           left = (i<<1) + 1;           right = (i<<1) + 2;       }       else       {           break;       }   }}void Build_Max_Heap(int *a, int heap_size){    for(int i = heap_size / 2 - 1; i >= 0; i--)    {        Max_Heapify(a, heap_size, i);    }}void HeapSort(int *a, int len)  //堆排序接口{    int heap_size = len;    Build_Max_Heap(a,heap_size);    for(int i = len - 1; i>=1; i--)    {        int temp = a[0];        a[0] = a[i];        a[i] = temp;        heap_size--;        Max_Heapify(a, heap_size, 0);    }}

转载于:https://www.cnblogs.com/xly0713/p/3189922.html

相关资源:数据结构—成绩单生成器

最新回复(0)