Advertisement
Dani_info

Selection Sort Recursive

Sep 15th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void selectionRecursive(int[], int);
  6. int maxim (int[], int);
  7.  
  8. int main()
  9. {
  10.     int n;
  11.     cout<<"n="<<endl; cin>>n;
  12.     cout<<"Introdu elementele:"<<endl;
  13.     int v[n];
  14.     for (int i=0; i<n; i++)
  15.         cin>>v[i];
  16.  
  17.     cout<<"vectorul sortat este:"<<endl;
  18.     selectionRecursive(v, n-1);
  19.     for (int i=0; i<n; i++)
  20.         cout<<v[i]<<" ";
  21.     return 0;
  22. }
  23.  
  24. int maxim (int v[], int n){
  25.     int indmax=0;
  26.     int nrmax=v[0];
  27.     for (int i=0; i<=n; i++){
  28.         if (v[i]>nrmax){
  29.             indmax=i;
  30.             nrmax=v[i];
  31.         }
  32.     }
  33.     return indmax;
  34. }
  35.  
  36. void selectionRecursive (int v[], int n){
  37.     if (n>0){
  38.         swap (v[n], v[maxim (v, n)]);
  39.         selectionRecursive(v, n-1);
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement