Selection Sort

Posted by Budi Arieyanto | Posted in | Posted on 02.51

#include <conio.h>
#include <iostream.h>
#include <iomanip.h>

void selectionsort(int array[], const int size);
void main()
{
 int bil[]={5, 34, 32, 25, 75, 42, 22, 2};
 int jmldata=sizeof (bil)/ sizeof (int);

 cout<<"data sebelum diurutkan : "<<endl;
 for (int i=0; i<jmldata; i++)
  {
   cout<<setw(5)<<bil[i];
  }
  cout<<endl<<endl;
  selectionsort(bil,jmldata);
  cout<<"data sesudah diurutkan"<<endl;
  for (int i=0; i<jmldata; i++)
  {
   cout<<setw(5)<<bil[i];
  }
   cout<<endl;
  getch();
}

void selectionsort(int array[],const int size)
{
 int i, j, smallest, temp;
 for (i=0; i<size; i++)
  {
   smallest=i;
          for(j=i;j<size;j++)
       {
        if(array[smallest]>array[j])
         {
           smallest=j;
         }
       }
      temp=array[i];
      array[i]=array[smallest];
        array[smallest]=temp;
  }
}

Comments (0)

Posting Komentar