簡體   English   中英

在數組中查找第二/第三最小元素的遞歸方法

[英]Recursive Way to Find 2nd/3rd Smallest Element In Array

以下是我編寫的代碼,用於查找C ++中未排序數組的最小元素。

我應該如何編輯我的遞歸函數以找到數組中第二個甚至第三個最小的元素?

我知道還有其他方法(我已經搜索過),但是我想知道如何使用像這樣的遞歸函數來做到這一點,該函數僅適用於最小數量。

int min(vector<int> &array, int &min, int &next_min, int left,int right)
{    
    int a;        
    int b;

    if(left == right) { 
        return array[left];
    } else {   
        int mid= (right+left)/2;

        a = min(array,left,mid);
        b = min(array,mid+1,right);

        if (a<b) 
            return b;
        else         
            return a;
    }
}

提前謝謝了

這是我嘗試找到第二小的選擇:

#include<iostream>
#include<vector>
#include <cstdlib>

using namespace std;

int counter=0;

void minf(vector<int> &array,int left,int right,int &min, int &next_min);

int main()
{
    int next_min;

    cout << "Enter integers one line at a time. (Enter -999 to terminate)" << endl;

    vector<int> array;  // 
    int input;

    while(true){
        cin >> input;
        if(input == -999) //check for termination condition
        break;
        array.push_back(input);
    }

    int imin;
    int imax;

    cout<<"Enter imin, then imax"<<endl;
    cin>>imin;
    cin>>imax; 

    cout<<"Min number is " << next_min <<endl;

    cin.get();
    system("pause");
    return 0;
}

void minf(vector<int> &array,int left,int right,int &min, int &next_min)
{
    int a;
    int b;
    int c;
    int d;

    if(left == right) { 
        min = array[left];
        next_min = 2147483647;
    } else {
        int mid= (right+left)/2;

        minf(array,left,mid,a,b);
        minf(array,mid+1,right,c,d);

        if (a < b && a < c && a < d) {
            min = a;
            if (b<c && b <d)
                next_min = b;
            else if (c < b && c < d)
                next_min = c;
            else
                next_min = d;
        }    

        if (b < a && b < c && b < d){
            min = b;
            if (a<c && a <d)
                next_min = a;
            else if (c < b && c < d)
                next_min = c;
            else
                next_min = d;
        }

        if (c < a && c < b && c < d) {
            min = c;
            if (b<a && b<d)
                next_min = b;
            else if (a < b && a < d)
                next_min = a;
            else
                next_min = d;
        }   

        if (d < a && d < c && d < b){
            min = d;
            if (a<c && a <b)
                next_min = a;
            else if (c < b && c < a)
                next_min = c;
            else
                next_min = b;
        }     
    }
}

有什么原因不能使用std::sort(vector.beigin(), vector.end())嗎? 那么vector[0]最小, vector[1]最小,依此類推。

該函數將以遞歸方式找到n最小的元素,希望對您有所幫助!

#include <vector>
#include <iostream>

using namespace std;

vector<int> nMin(const vector<int> &array, int n, int left, int right) {
    vector<int> result;
    if (left == right) {
        result.push_back(array[left]);
    } else {
        int mid = (right + left) / 2;
        vector<int> leftResult = nMin(array, n, left, mid);
        vector<int> rightResult = nMin(array, n, mid + 1, right);
        int i = 0;
        int l = 0;
        int r = 0;
        int L = leftResult.size();
        int R = rightResult.size();
        while (i < n && (l < L || r < R)) {
            i++;
            if (l < L) {
                if (r < R) {
                    if (leftResult[l] < rightResult[r]) {
                        result.push_back(leftResult[l++]);
                    } else {
                        result.push_back(rightResult[r++]);
                    }
                } else {
                    result.push_back(leftResult[l++]);
                }
            } else {
                result.push_back(rightResult[r++]);
            }
        }
    }
    return result;
}

int main() {
    vector<int> test = {-2, 6, 7, 1, 3, 7, 4, 2, 5, 0, 8, -2};
    vector<int> smallest3 = nMin(test, 3, 0, test.size() - 1);
    for (int num : smallest3) {
        cout << num << endl;
    }
}

簡要說明:從左半部分到右半部分按升序最多獲取n最小元素,將它們leftResult稱為leftResultrightResult ,然后將兩者合並,始終從兩個部分結果中選擇下一個最小元素。 這類似於merge sort ,只是它最多返回n元素而不是對整個數組進行排序。

即使您在每一步中將數組分成兩半,您的搜索仍然是線性的(因為數據未排序)。 給函數最后的最小值怎么辦?

int minf(vector<int> &array, int last_min)
{
    int minimum = 2147483647;
    for (vector<int>::iterator it = array.begin(); it != array.end(); ++it)
        if(*it < minimum && *it > last_min) minimum = *it;
    return minimum;
}

然后,您可以編寫一個打印n個最小元素的函數:

void printMins(vector<int> &array, int count)
{
    int last_min = -2147483648;
    while(count-- > 0)
    {
        last_min = minf(array, last_min);
        cout << last_min << endl;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM