简体   繁体   English

两种排序算法在同一数组上为我提供了两个不同的输出(quickSort和heapSort)!

[英]Two sorting algorithms give me two different outputs on the same array (quickSort and heapSort)!

I don't understand why they give me different output when I compile them. 我不明白为什么当我编译它们时它们给我不同的输出。 For example ... when I compile only one algorithm the answer is good, the same is for the other one, but when I compile them both at the same time they give me some weird output. 例如...当我只编译一种算法时,答案是好的,而对于另一种算法,答案是好的,但是当我同时编译它们时,它们给了我一些奇怪的输出。

My code: 我的代码:

#include <iostream>
using namespace std;


int parent(int i){
    return i/2;
}
int leftChild(int i){
    return 2*i+1;
}
int rightChild(int i){
    return 2*i+2;
}
void maxHeapify(int a[], int i, int n){
    int largest;
    int temp;
    int l = leftChild(i);
    int r = rightChild(i);
    //   p.countOperation("CMPbottomUp",n);
    if (l <= n && (a[l] > a[i]))
        largest = l;
    else
        largest = i;
    //      p.countOperation("CMPbottomUp",n);
    if (r <= n && (a[r] > a[largest]))
        largest = r;
    if (largest != i){
        //    p.countOperation("ATTbottomUp",n);
        temp = a[i];
        //  p.countOperation("ATTbottomUp",n);
        a[i] = a[largest];
        //p.countOperation("ATTbottomUp",n);
        a[largest] = temp;
        maxHeapify(a, largest, n);
    }
}

void buildMaxHeap(int a[], int n){
    for (int i=n/2; i>=0; i--){
        maxHeapify(a, i, n);
    }
}
void heapSort(int a[],int n){
    buildMaxHeap(a,n);
    int n1=n;
    int temp;
    for(int i=n1;i>0;i--){
        temp = a[0];
        a[0] = a[i];
        a[i] = temp;
        n1--;
        maxHeapify(a,0,n1);
    }

}

int partitionArray(int arr[], int left, int right){
    int i = left, j = right;
    int tmp;
    int pivot = arr[(left + right) / 2];
    while (i <= j) {
        while (arr[i] < pivot)
            i++;
        while (arr[j] > pivot)
            j--;
        if (i <= j) {
            tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            i++;
            j--;
        }
    }
    return i;
}

void quickSort(int arr[], int left, int right) {
    int index;
    index = partitionArray(arr, left, right);
    if (left < index - 1)
        quickSort(arr, left, index - 1);
    if (index < right)
        quickSort(arr, index, right);
}

int main(){
    int x[8]= {5,87,21,4,12,7,44,3};
    int a[8];
    for(int i=0;i<8;i++){
        a[i] = x[i];
    }
    heapSort(x,8);
    quickSort(a,0,8);

    for(int i=0;i<8;i++){
        cout<<a[i]<<' ';
    }
    cout<<endl;

    for(int j=0;j<8;j++){
        cout<<x[j]<<' ';
    }

    return 0;
}

Example output: 输出示例:

1) When I compile only one algorithm the output is : 3,4,5,7,12,21,44,87 (which is good) 1)当我只编译一种算法时,输出为:3,4,5,7,12,21,44,87(很好)

2) When I compile both of them in the code the output is: 87,4,5,7,12,21,44,87 (quickSort) and 3,3,4,5,7,12,21,44 (heapSort) 2)当我在代码中编译它们两者时,输出为:87,4,5,7,12,21,44,87(quickSort)和3,3,4,5,7,12,21,44( heapSort)

Arrays a and x are right next to each others in stack. 数组ax在堆栈中彼此紧邻。 Seeing how you have duplicate value 87 in output, it seems your sort functions access memory outside the array you give to them. 看到输出中的重复值87如何,似乎您的排序函数在您提供给它们的数组之外访问内存。 This is buffer overrun, a type of Undefined Behaviour. 这是缓冲区溢出,一种未定义的行为。 With that, your code could do anything because you have corrupted variable values (or worse, corrupted addresses/pointers). 这样,您的代码就可以执行任何操作,因为您破坏了变量值(或更糟糕的是,破坏了地址/指针)。

Double check how you access arrays. 仔细检查您如何访问阵列。 Remember that C array indexes for your arrays of length 8 are 0..7! 请记住,长度为8的数组的C数组索引为0..7!

I think that should work: 我认为这应该工作:

heapSort(x,7);
quickSort(a,0,7);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么两种不同的初始化对象的方式给出不同的输出 - why do the two different ways of initialising objects give different outputs 使用Boost :: tokenizer的两个相同程序,但是两个不同的输出 - Two same programs using Boost::tokenizer, but two different outputs 使用quicksort排序不会给出排序数组 - Sorting using quicksort doesn't give sorted array 为什么这两段代码会给我不同的结果? - Why do these two segments of code give me different results? 在两个不同的应用程序中获得两个不同的输出? - Getting two different outputs in two different apps? C ++代码在尝试连续两次打印相同的字符串数组时会模糊两个非常不同的输出 - C++ code blurts out two very different outputs while trying to print the same array of strings twice in a row QuickSort不对数组进行排序 - QuickSort not sorting array 为什么std :: is_same对这两种类型给出不同的结果? - Why does std::is_same give a different result for the two types? 相同的地址位置如何给出两个不同的值? - How does same address location give two different values? 联合声明的两种不同语法如何给我不同的大小? - How does the two different syntax of union declaration give me different size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM