skb50bd

Selection Sort

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