Advertisement
fahimkamal63

Selection sort

Oct 18th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. //  Selection sort
  2. //  Date : 17.10.19
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. void selectionSort(int arr[], int n){
  7.     int select = 0;
  8.     for(int i = 0; i < n-1; i++){
  9.         for(int j = i + 1; j < n; j++){
  10.             if(arr[j] < arr[i]) { select = j; }
  11.         }
  12.         int temp = arr[i];
  13.         arr[i] = arr[select];
  14.         arr[select] = temp;
  15.     }
  16. }
  17.  
  18. int main(){
  19.     int n;
  20.     cout << "Enter the range of array: "; cin >> n;
  21.     int arr[n], i;
  22.     cout << "Enter the element of array: ";
  23.     for(i = 0; i < n; i++){
  24.         cin >> arr[i];
  25.     }
  26.     selectionSort(arr, n);
  27.     cout << "The array after sorting: ";
  28.     for(i = 0; i < n; i++){
  29.         cout << arr[i] << ' ';
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement