Advertisement
alaminrifat

Selection Sort in C++

Nov 9th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define nl '\n'
  4.  
  5.  
  6. int main(){
  7.     int n = 10 , arr[n]={6,2,12,23,11,56,85,41,203,101};
  8.     int indxMin,temp;
  9.  
  10.     cout<<"Before Sorting"<<nl;
  11.     for (int i = 0; i < n ; i++ ){
  12.         cout<<arr[i]<<" ";
  13.     }
  14.     cout<<nl<<nl;
  15.  
  16.     for (int i = 0; i < n-1 ; i++ ){
  17.         indxMin = i;
  18.         for (int j = i+1; j < n ; j++ ){
  19.             if(arr[j] < arr[indxMin]){
  20.                     indxMin = j;
  21.             }
  22.         }
  23.         if(indxMin != i){
  24.             temp = arr[i];
  25.             arr[i] = arr[indxMin];
  26.             arr[indxMin] = temp;
  27.         }
  28.     }
  29.  
  30.     cout<<"After Sorting"<<nl;
  31.     for (int i = 0; i < n ; i++ ){
  32.         cout<<arr[i]<<" ";
  33.     }
  34.  
  35.     return 0;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement