Advertisement
raian008

Selection Sort

Oct 15th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void selection(int arr[], int S){
  5.     int select,minValue,minIndex,i;
  6.  
  7.     for(select=0; select<S-1; select++){
  8.         minIndex=select;
  9.         minValue=arr[select];
  10.  
  11.         for(i=select+1; i<S;i++){
  12.             if(arr[i]<minValue){
  13.                 minValue=arr[i];
  14.                 minIndex=i;
  15.             }
  16.         }
  17.         arr[minIndex]=arr[select];
  18.         arr[select]=minValue;
  19.     }
  20. }
  21. int main(){
  22.  
  23.  
  24.     int i,n;
  25.     cout<<"enter Array size"<<endl;
  26.     cin>>n;
  27.     int a[n];
  28.     std::cout << "Enter array numbers" << std::endl;
  29.  
  30.     for(i=0; i<n;i++){
  31.         cin>>a[i];
  32.     }
  33.  
  34.     selection(a,n);
  35.  
  36. for(i=0; i<n;i++){
  37.         cout<<" "<<a[i];
  38.     }
  39.  
  40.  
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement