Computer Science Canada Selection sort |
Author: | rickdragon [ Tue Jan 13, 2004 2:21 pm ] |
Post subject: | Selection sort |
#include<iostream.h> #include<conio.h> void main() { int i,n,k,j,a[100],min,loc,temp; clrscr(); cout<<"Enter the size of an array : "; cin>>n; clrscr(); cout<<"Enter the element of the array : "<<endl; for(k=1;k<=n;k++) cin>>a[k]; for(k=1;k<=n;k++) { min = a[k]; loc = k; for(j=k+1;j<=n;j++) { if(min > a[j]) { min = a[j]; loc = j; } } temp = a[k]; a[k] = a[loc]; a[loc] = temp; } clrscr(); cout<<"The sorted elements of an array are : "<<endl; for(k=1;k<=n;k++) cout<<a[k]<<endl; getch(); } |