SabirSazzad

Selection sort

Mar 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int n,i,j,temp;
  7.     cout << "Insert Array size: ";
  8.     cin >> n;
  9.     int *arr=new int[n];
  10.     cout << "Insert Array Elements..." <<endl;
  11.     for(i=0; i<n; i++)
  12.     {
  13.         cin >> arr[i];
  14.     }
  15.     for(j=0; j<n-1; j++)
  16.     {
  17.         for(i=j+1; i<n; i++)
  18.         {
  19.             if(arr[j]>arr[i])
  20.             {
  21.                 temp = arr[j];
  22.                 arr[j] = arr[i];
  23.                 arr[i] = temp;
  24.             }
  25.         }
  26.     }
  27.     cout << "Sorted Array is...."<<endl;
  28.     for(i=0; i<n; i++)
  29.     {
  30.         cout << arr[i] << " ";
  31.     }
  32.  
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment