lundi 27 juillet 2020

Problem in sorting a vector using recursion

i am getting the inputed vector as the output as it is.

code:

#include <bits/stdc++.h>
using namespace std;
void insert(vector<int> v,int val){
   if(v.size()==0 || v[v.size()-1]<=val ){
     v.push_back(val);
     return;
   }
   int x = v[v.size()-1];
   v.pop_back();
   insert(v,val);
   v.push_back(x);
   return;
}
void fun(vector<int> v){
   if( v.size()==0 )
    return;
   int val = v[ v.size()-1 ];
   v.pop_back();
   fun(v);
   insert(v,val);
   return;
}
int main() {
 vector<int> v{9,4,1,-2,10,1,12,5,0,-4};
 fun(v);
 auto x= v.begin();
 for(;x!=v.end();x++)
    cout<<*x<<" ";
  }

the out put is the same input vector ,i.e O/p : 9,4,1,-2,10,1,12,5,0,-4 please tell where am i going wrong

Aucun commentaire:

Enregistrer un commentaire