【问题标题】:Find the maximum and minimum element in an array [closed]查找数组中的最大和最小元素[关闭]
【发布时间】:2021-12-25 23:34:39
【问题描述】:

在这个问题中我找不到错误,一切对我来说似乎都是正确的,我正在使用快速排序对数组进行排序,但排序算法不起作用,所以我可以找到最大值和最小值

#include <iostream>
#include <vector>

using namespace std;

void swap(int *a, int *b){
    int t = *a;
    *a = *b;
    *b = t;
}

int partition(vector<int> arr ,int low, int high){
    int pivot = arr[high];
    int i = low-1;
    for (int j=low; j<=high; j++){
        if(arr[j]<pivot){
            i++;
            swap(&arr[i],&arr[j]);
        }
    }
    swap(&arr[i+1],&arr[high]);
    return (i+1);
}

void quickSort(vector<int> arr, int low, int high){
    if(low<high){
        int pi = partition(arr,low,high);
        quickSort(arr,low,pi-1);
        quickSort(arr,pi+1,high);
    }
}


int main()
{
    vector<int> arr = {5,2,3,4,1};
    int arr_size = arr.size();
    quickSort(arr,0,arr_size-1);
    cout<<"The minimum and maximum array is \n";
    cout<<arr[0]<<" "<<arr[arr_size-1];

    return 0;
}

【问题讨论】:

  • 在使用调试器逐行单步执行代码时,您观察到了什么?
  • 如果您的函数按值获取向量,则调用者将看不到任何更改。
  • @πάνταῥεῖ 当我在函数分区中使用调试器 pivot = 0 检查时,请帮助我
  • @interjay 我如何通过引用来调用任何示例。
  • 你把参数vector&lt;int&gt; &amp;arr

标签: c++ algorithm data-structures array-algorithms


【解决方案1】:

quicksort()partition()

int partition(vector<int> arr ,int low, int high){
    int pivot = arr[high];
    int i = low-1;
    for (int j=low; j<=high; j++){
        if(arr[j]<pivot){
            i++;
            swap(&arr[i],&arr[j]);
        }
    }
    swap(&arr[i+1],&arr[high]);
    return (i+1);
}
void quickSort(vector<int> arr, int low, int high){
    if(low<high){
        int pi = partition(arr,low,high);
        quickSort(arr,low,pi-1);
        quickSort(arr,pi+1,high);
    }
}

您通过值传递vector&lt;int&gt; arr,这意味着编译器将复制arr,而不更改arr 的实际值。

您必须通过引用传递:

int partition(vector<int> &arr ,int low, int high){
    int pivot = arr[high];
    int i = low-1;
    for (int j=low; j<=high; j++){
        if(arr[j]<pivot){
            i++;
            swap(&arr[i],&arr[j]);
        }
    }
    swap(&arr[i+1],&arr[high]);
    return (i+1);
}
void quickSort(vector<int> &arr, int low, int high){
    if(low<high){
        int pi = partition(arr,low,high);
        quickSort(arr,low,pi-1);
        quickSort(arr,pi+1,high);
    }
}

但是,您也可以改用std::sort。在main():

int main()
{
    std::vector<int> arr = {5,2,3,4,1};
    int arr_size = (int)arr.size();
    std::sort(arr.begin(), arr.end());
    std::cout<<"The minimum and maximum array is \n";
    std::cout<<arr[0]<<" "<<arr[arr_size-1];

    return 0;
}

但一般来说,“排序”解决方案效率低下。你应该改用std::minmax_element

int main()
{
    std::vector<int> arr = {5,2,3,4,1};
    auto result = std::minmax_element(arr.begin(), arr.end());
    std::cout<<"The minimum and maximum array is \n";
    std::cout<< *result.first <<' '<< *result.second;

    return 0;
}

【讨论】:

  • 或使用std::minmax_element
猜你喜欢
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 2014-07-21
  • 2018-10-17
  • 2010-12-12
  • 1970-01-01
  • 2020-11-01
相关资源
最近更新 更多