使用c中的sift down方法实现堆排序

时间:2018-04-20 15:29:01

标签: c sorting heap

我正在尝试使用c中的sift down方法实现堆排序,程序的输出缺少数组的第0个索引。我无法找到问题所在的问题,是否是由于阵列溢出?

这是我的代码

#include<stdio.h>
void heapSort(int[], int);
void siftdown(int[], int, int, int);
int main(void)
{
     int array[] = {5, 2, 1, 4, 3};
   int nodeCount = sizeof(array)/sizeof(array[0]);
     heapSort(array, nodeCount);
     printf("Sorted array:\n");

      for(int i=0; i < nodeCount; i++)
        {
           printf("%d\n", array[i]);
        }
     return 0;
}
void heapSort(int array[], int nodeCount){
     //creating a heap
     for(int i = nodeCount / 2; i >= 1; i--){
         siftdown(array, array[i], i, nodeCount);
     }

     //perform heapsort
     for(int lastNode = nodeCount; lastNode > 1; lastNode--){
         int lastNodeValue = array[lastNode];
         array[lastNode] = array[1];
         array[1] = lastNodeValue;
         siftdown(array, lastNodeValue, 1, lastNode -1);
     }
}
void siftdown(int array[], int nodeValue, int root, int last){
     int leftChild = 2 * root + 1 ;
     while(leftChild <= last){ //at least has one child
         if(leftChild < last){ //has right child
             if(array[leftChild + 1] > array[leftChild]){
                 leftChild++;
             }
        }
         if(nodeValue >= array[leftChild]){
             break;
         }
         else{
             array[root] = array[leftChild];
             root = leftChild;
            leftChild = 2 * root + 1;
         }
         array[root] = nodeValue;
     }
}

该计划的输出:

  

排序数组:5 1 2 3 4

1 个答案:

答案 0 :(得分:0)

from ortools.linear_solver import pywraplp solver = pywraplp.Solver("Soft Constraint Example", pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) product_a = solver.IntVar(0, 10000, "Pounds of Product A:") product_b = solver.IntVar(0, 10000, "Pounds of Product B:") product_a_surplus = solver.IntVar(0, 100, "Product A Surplus:") product_a_deficit = solver.IntVar(0, 100, "Product A Deficit:") product_b_surplus = solver.IntVar(0, 100, "Product B Surplus:") product_b_deficit = solver.IntVar(0, 100, "Product B Deficit:") cost_surplus = solver.IntVar(0, 10000, "Cost Surplus:") cost_deficit = solver.IntVar(0, 10000, "Cost Deficit:") product_a_goal = solver.Add(product_a - product_a_surplus + product_a_deficit == 500) product_b_goal = solver.Add(product_b - product_b_surplus + product_b_deficit == 250) cost_goal = solver.Add(product_a * 100 + product_b * 200 + cost_surplus - cost_deficit == 75000) solver.Minimize( (1/100) * product_a_surplus + (1/100) * product_a_deficit + (1/200) * product_b_surplus + (1/200) * product_b_deficit + (1/75000) * cost_surplus + (1/75000) * cost_deficit ) status = solver.Solve() print(status == solver.OPTIMAL) final_cost = product_a.solution_value() * 100 + product_b.solution_value() * 200 print("Final Cost:", final_cost) var = [product_a, product_b, product_a_surplus, product_a_deficit, product_b_surplus, product_b_deficit, cost_surplus, cost_deficit] for v in var: print(v.name(), v.solution_value()) 功能中的循环是:

heapSort

因为 for(int i = nodeCount / 2; i >= 1; i--){ siftdown(array, array[i], i, nodeCount); } 永远不是0,所以你永远不会筛选出那个节点。将比较更改为i

在你的i >= 0函数中,你有这个循环:

heapSort

C中的数组从0开始。因此,如果数组中有5个项目,则索引为0,1,2,3,4。您的循环使用数组索引1,2,3,4,5。也需要改变那个循环。它看起来像:

 //perform heapsort
 for(int lastNode = nodeCount; lastNode > 1; lastNode--){
     int lastNodeValue = array[lastNode];
     array[lastNode] = array[1];
     array[1] = lastNodeValue;
     siftdown(array, lastNodeValue, 1, lastNode -1);
 }