Advertisement
edutedu

Selection sort - recursiv

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