c++ - Recursive Way to Find 2nd/3rd Smallest Element In Array -
below code written find smallest element of unsorted array in c++.
how should go editing recursive function find second or third smallest element in array?
i understand there other methods out there (i've searched), i'd know how using recursive function works minimum number.
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; = min(array,left,mid); b = min(array,mid+1,right); if (a<b) return b; else return a; } }
many in advance
here attempt @ finding 2nd smallest:
#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 1 line @ time. (enter -999 terminate)" << endl; vector<int> array; // int input; while(true){ cin >> input; if(input == -999) //check termination condition break; array.push_back(input); } int imin; int imax; cout<<"enter imin, imax"<<endl; cin>>imin; cin>>imax; cout<<"min number " << 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 && < c && < 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 < && b < c && b < d){ min = b; if (a<c && <d) next_min = a; else if (c < b && c < d) next_min = c; else next_min = d; } if (c < && c < b && c < d) { min = c; if (b<a && b<d) next_min = b; else if (a < b && < d) next_min = a; else next_min = d; } if (d < && d < c && d < b){ min = d; if (a<c && <b) next_min = a; else if (c < b && c < a) next_min = c; else next_min = b; } } }
this function find n
smallest elements in recursive way, hope helps!
#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 = 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); (int num : smallest3) { cout << num << endl; } }
brief explanation: n
smallest elements in ascending order left & right half, call them leftresult
, rightresult
, merge two, picking next smallest element 2 partial results. merge sort, except return n
elements instead of sorting whole array.
Comments
Post a Comment