Search code examples
c++algorithmdata-structuresarray-algorithms

Find the maximum and minimum element in an array


In this question i can't find the error everything seems correct to me, i am sorting the array using Quick Sort but the sorting algorithm is not working , so that i can find the max and min

#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;
}

Solution

  • In quicksort() and 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);
        }
    }
    

    You pass vector<int> arr by value, which means the compiler will make a copy of arr, not changing the actual value of arr.

    You have to pass by reference instead:

    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);
        }
    }
    

    But, you can also use std::sort instead. In 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;
    }
    

    But the "sorting" solution, in general, is inefficient. You should use std::minmax_element instead:

    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;
    }