Saturday, 22 October 2016

Smallest Code: Selection Sort in C++

You must be thinking Smallest Code? Now, What the heck it is? Yeah! Yeah! I know I sound shit but you know C++ STL is so good that it makes me fall in love with it, every time I use it. And once again, I have implemented SELECTION SORT IN C++ using STL. If there is a question in your mind that Does use of STL makes you a bad programmer? Then read Tourist's Code you will get the answer.

#include<iostream>
#include<algorithm>
using namespace std;

int main(){
    int v[]={3,1,8,9,2,2,7,4};
    auto len =sizeof(v)/sizeof(*v);
    for(auto i=0;i!=len-2;++i){
       int *p = min_element(v+i, v+len);
       swap(v[i],*p);
    }
    for(auto &k:v)
        cout<<k<<" ";
    return 0;
}
 
Did you enjoy the code? If you did, say "Hi" in the comments below.  HAPPY SORTING :)

No comments:

Post a Comment