简体   繁体   English

无法从同一数组中找到相对于最大值的数组中的最小值

[英]Can't find the minimum in an array with respect to maximum from the same array

I wanted to write a program to take two arrays as input and convert the first array such that the difference of maximum value and minimum value from the first array gives the smallest possible number.我想编写一个程序,将两个 arrays 作为输入并转换第一个数组,以便第一个数组的最大值和最小值之差给出尽可能小的数字。

I tried to write a code to find a smaller number most closest to the maximum from the array in C++, but the function for finding the minimum works on Codelite, but not on other compilers;我试图编写一个代码来从 C++ 中的数组中找到一个最接近最大值的较小数字,但是用于查找最小值的 function 在 Codelite 上有效,但在其他编译器上无效; Is there any fix to solve this, either to the code or the compiler?有没有解决这个问题的方法,无论是代码还是编译器?

Here is the code I tried:这是我试过的代码:

#include <iostream>
using namespace std;

void swap(int A[], int B[], int n)
{
    int x, y, temp;
    
    for(x=0;x<n;++x)
    {
        for(y=0;y<n;++y)
        {
            if(A[x]>B[y])
            {
                temp = A[x];
                A[x] = B[y];
                B[y] = temp;
            }
        }
    }
}

void sortas(int A[], int n)
{
    int i, j, temp;
    
    for (i = 0; i < n; i++)
    {
        for (j = i; j < n; j++)
        {
            if (A[i] > A[j+1])
            {
                temp = A[i];
                A[i] = A[j+1];
                A[j+1] = temp;
            }
        }
    }
}

int maxfind(int A[], int n)
{
    int z, a;
    
    a = A[0];
    for(z=0;z<n;++z)
    {
        if(a<A[z])
        {
            a = A[z];
        }
    }
    
    cout << "Max value in A is" << a << endl;
    
    return a;
}

int minfind(int A[], int n, int amax, int amin)
{
    int z, maxi;
    maxi = amax;
    for(z=0;z<n;++z)
    {
        if(maxi >= A[z])
        {
            amin = A[z];
        }
        else
        {
            maxi = maxi-1;
        }
    }
    cout << "Mix value in A is" << amin << endl;
    
    return amin;
}


int main() {
    
    int z, t;
    cout << "Enter number of test cases: ";
    cin >> t;
    
    int n, i, j, amax, amin;

    for(z=0;z<t;++z)
    {
        cout << "Enter size of array" << endl;
        cin >> n;
        
        int A[n], B[n];
        
        cout << "Enter Array A values:" << endl;
        for(i=0;i<n;++i)
        {
            cin >> A[i];
        }
        
        cout << "Enter Array B values:" << endl;
        for(j=0;j<n;++j)
        {
            cin >> B[j];
        }
        
        swap(A, B, n);
        sortas(A, n);
        
        cout << "Swapped and sorted array is: " << endl;
        for(i=0;i<n;++i)
        {
            cout << A[i] << "\t" << B[i] << endl;
        }
        amax = 0;
        amin = 0;
        amax = maxfind(A, n);
        amin = minfind(A, n, amax, amin);
    }
    
    return 0;
}


Here is the output to that code:这是该代码的 output:

1 1 1 3 4 2 1 1 1 3 4 2

Max value in A is 1 Min value in A is 1 A中的最大值为1 A中的最小值为1

1 2 2 -1882830412 4 3 6 3 1 2 2 -1882830412 4 3 6 3

Max value in A is 2 Min value in A is -1882830412 A中的最大值为2 A中的最小值为-1882830412

The problem is with your bubble sort:问题出在您的冒泡排序上:

void sortas(int A[], int n)
{
    int i, j, temp;
    
    for (i = 0; i < n; i++)
    {
        for (j = i; j < n; j++)  // why not start at i + 1 ??
        {
            if (A[i] > A[j+1])   // j + 1 is out of bounds when j == n - 1
            {
                temp = A[i];
                A[i] = A[j+1];  // <-- Some random value is written to A.
                A[j+1] = temp;  // <-- Overwriting some variable on the stack    
                                //     of main() (possibly B ?)
            }
        }
    }
}

A correct bubble sort (this is not the pedantic bubble sort), this is probably the most used.正确的冒泡排序(这不是迂腐的冒泡排序),这可能是最常用的。

void sortas(int A[], int n)
{
    for (int i = 0; i < n - 1; ++i)
    {
        for (int j = i + 1; j < n; ++j)
        {
            if (A[i] > A[j])
                std::swap(A[i], A[j]);
        }
    }
}

The actual bubble sort algorithn (the "pedantic" bubble sort), swaps only occur on neighboring values.实际的冒泡排序算法(“迂腐的”冒泡排序),交换只发生在相邻值上。

void sortas(int A[], int n)
{
    for (int i = 0; i < n - 1; ++i)
    {
        for (int j = 0; j < (n - i) - 1; ++j)
        {
            if (A[j] > A[j + 1])
                std::swap(A[j], A[j + 1]);
        }
    }
}

Use one or the other, for integers, the performance is identical.使用其中之一,对于整数,性能是相同的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM