C++ Program to Implement Selection Sort. This Algorithm is very simple. Find the smallest element in the array and swap it. That's all you will get a sorted array in the end.
#include <iostream> using namespace std; int main(){ int array[] = {1,45,10,35,100,13,147,500,80}; int size = sizeof(array)/sizeof(array[0]); int i,j,min; for (i = 0; i < size ; i++){ for(j = i+1 ; j < size ; j++){ if(array[j] < array[i]){ min = array[j]; array[j] = array[i]; array[i] = min; } } } for(i = 0 ; i < size ; i++){ cout<<array[i]<<endl; } return 0; }
1 10 13 35 45 80 100 147 500
Comments :