简体   繁体   中英

How can I adapt a search algorithm in an array of higher and lower numbers to complexity (3n / 2) - 2?

I have a program which searches for the largest and smallest number in an array of n elements in the C ++ language. What I want to do is to decrease the complexity of the algorithm a (3n / 2) - 2 , which currently does not meet this complexity.

This complexity is in the worst case

My question is how can I leave this algorithm to the aforementioned complexity formula? Or what can I modify, delete and add to comply with that condition?

Thank you. The comparison algorithm is as follows:

#include <iostream>
using namespace std;
int main(){
    int arreglo[10] = {9,8,7,6,5,4,3,2,1,0};
    int menor =0, mayor =0, comparaciones=0;
    menor = arreglo[0], mayor = arreglo[0];
    for(int i=1;i<10;i++){
        if(arreglo[i]>mayor){
            mayor = arreglo[i];
        }
        comparaciones++;
        if(arreglo[i]<menor){
                menor = arreglo[i];
        }    
        comparaciones++;
    }
    cout<<"Mayor: "<<mayor<<" Menor: "<<menor<<" Comparaciones: "<<comparaciones;
}

UPDATE: The algorithm has a complexity equation of 5n-2 , I must lower its complexity to (3n / 2) - 2

This solution uses the Divide and Conquer paradigm.

I based this answer from this website and there you can see the explanation of why this will take (3n / 2) - 2 comparisons.

To understand how it works, I suggest getting a pen and paper and follow the code, using a smaller input (eg: {3,2,1,0}).

#include <iostream>
using namespace std;

int* maxMin(int* values, int begin, int end) {
    int partialSmallest, partialLargest;
    int mid, max1, min1, max2, min2;
    //Here we store Largest/Smallest
    int* result = new int[2];

    //When there's only one element
    if (begin == end) {
        partialSmallest = values[begin];
        partialLargest = values[begin];
    }
    else {
        //There is not only one element, therefore
        //We will split into two parts, and call the function recursively
        mid = (begin + end) / 2;
        // Solve both "sides"
        int* result1 = maxMin(values, begin, mid);
        int* result2 = maxMin(values, mid+1, end);

        max1 = result1[0];
        min1 = result1[1];

        max2 = result2[0];
        min2 = result2[1];
        //Combine the solutions.
        if (max1 < max2)
            partialLargest = max2;
        else
            partialLargest = max1;
        if (min1 < min2)
            partialSmallest = min1;
        else
            partialSmallest = min2;
    }

    result[0] = partialLargest;
    result[1] = partialSmallest;
    return result;
}

int main(){
    int values[10] = {9,8,7,6,5,4,3,2,1,0};
    int* finalResult = maxMin(values, 0, 9);
    cout << "Largest: " << finalResult[0] << " Smallest: " << finalResult[1];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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