Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法在堆排序代码中找到错误。执行不正确。C++;_C++_Heapsort - Fatal编程技术网

C++ 无法在堆排序代码中找到错误。执行不正确。C++;

C++ 无法在堆排序代码中找到错误。执行不正确。C++;,c++,heapsort,C++,Heapsort,请帮忙。我已经检查过了,似乎遗漏了错误。它似乎退出了函数Max_Heapify,并且没有运行第二个循环来打印已排序的数组。这是我已经交上的作业,但我正在努力学习我的错误方法,以确保我能在考试中取得好成绩 #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; // declare global variable of heap size int heap

请帮忙。我已经检查过了,似乎遗漏了错误。它似乎退出了函数Max_Heapify,并且没有运行第二个循环来打印已排序的数组。这是我已经交上的作业,但我正在努力学习我的错误方法,以确保我能在考试中取得好成绩

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

// declare global variable of heap size 
int heap_size = 7;


// function to determine left child node of the tree 
int Left(int i){
    return 2*i+1;
}

// function to determine right child node of the tree 
int Right(int i){
    return 2*i + 2;
}

// create heap tree 
void Max_Heapify (int array[], int index){
    int left_child_index = Left(index);
    int right_child_index = Right(index);
    int largest; 

    // check if left child is smaller than heap size and if left child is bigger than parent
    // if so, save variable as largest value, otherwise, largest value will stay as index
    if ( (left_child_index < heap_size) && (array[left_child_index] > array[index]) )
        largest = left_child_index;
    else largest = index;
    // check if right child is smaller than heap and if bigger than largest value
    if ((right_child_index < heap_size) && (array[right_child_index] > array[largest]) )
        largest = right_child_index;
    // exchange largest values 
    if (largest != index)
        swap(array[index], array[largest]);


    Max_Heapify(array,largest);

}

// check leaves 
void Build_Max_Heap(int array[]){

    for (int i = (heap_size / 2 ) - 1; i >= 0; i--)
        Max_Heapify (array, i);
}


void Heapsort(int array[]) {
    Build_Max_Heap(array);
    for (int i = heap_size-1; i >= 0; i--){
        swap(array[0], array[i]);
        Max_Heapify(array,0);
    }
}

int main(){

    int arr[7] = {21, 9, 50, 7, 6, 33, 77};

    cout << "Non Heapified Array: " << endl;
    for (int i = 0; i < heap_size; i++){
        cout << arr[i] << endl;
    }

    Heapsort(arr);

    for (int i = 0; i < heap_size; i++){
        cout << arr[i]<< endl;
    }


}
#包括
#包括
#包括
使用名称空间std;
//声明堆大小的全局变量
int heap_size=7;
//函数确定树的左子节点
左整数(整数i){
返回2*i+1;
}
//函数确定树的右子节点
右整数(整数i){
返回2*i+2;
}
//创建堆树
void Max_Heapify(整数数组[],整数索引){
int left_child_index=left(index);
int right\u child\u index=右(index);
int最大;
//检查左子级是否小于堆大小,左子级是否大于父级
//若是,则将变量另存为最大值,否则,最大值将保留为索引
if((左子索引<堆大小)&&(数组[左子索引]>数组[索引])
最大=左\子\索引;
否则最大=指数;
//检查右子级是否小于堆,是否大于最大值
if((右子索引<堆大小)&(数组[右子索引]>数组[最大])
最大=右\子\索引;
//交换最大值
如果(最大!=索引)
交换(数组[索引]、数组[最大]);
Max_Heapify(数组,最大);
}
//检查树叶
无效生成\u最大\u堆(int数组[]){
对于(inti=(堆大小/2)-1;i>=0;i--)
Max_Heapify(数组,i);
}
void Heapsort(int数组[]){
构建最大堆(数组);
对于(int i=heap\u size-1;i>=0;i--){
交换(数组[0],数组[i]);
Max_Heapify(数组,0);
}
}
int main(){
int-arr[7]={21,9,50,7,6,33,77};

cout你的
MaxHeapify
永远不会终止。只有当
最大的
不是
i
时,你才应该调用
MaxHeapify
。如果
最大的
i
,那么就不需要在这里执行任何操作,因为元素已经被加密了

if (largest != index){
    swap(array[index], array[largest]);
    Max_Heapify(array,largest);
}

找出错误的最好方法是开始使用调试器。