Advertisement
Guest User

Selection sort

a guest
Jun 18th, 2018
92
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.h>
  2. #include<conio.h>
  3.  
  4. void main()
  5. {
  6.     int ar[500], n, s, t;
  7.    cout<<"Enter number of elements: ";
  8.    cin>>n;
  9.    cout<<"\n\n\n\n";
  10.    for(int i=0; i<n; i++)
  11.    {
  12.     cout<<"Enter the value "<<(i+1)<<": ";
  13.     cin>>ar[i];
  14.       cout<<"\n";
  15.    }
  16.    cout<<"\n\n\n";
  17.    for(int i=0; i<n; i++)
  18.    {
  19.     cout<<ar[i]<<" ";
  20.    }
  21.    for(int i=0; i<n-1; i++)
  22.    {
  23.     for(int j=i+1; j<n; j++)
  24.       {
  25.         if(ar[j]<ar[i])
  26.          {
  27.             ar[i]+=ar[j];
  28.             ar[j]=ar[i]-ar[j];
  29.             ar[i]-=ar[j];
  30.          }
  31.       }
  32.    }
  33.    cout<<"\n\nSorted array:\n\n";
  34.    for(int i=0; i<n; i++)
  35.    {
  36.     cout<<ar[i]<<" ";
  37.    }
  38.    getch();
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement