Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading