Program C++ Pengurutan (Sorting) dengan Metode Selection Sort


Script

/*selection Sort*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
int selectionsort (int array[], const int size)
 {
 int i, j, kecil, temp;
 for (i=0; i<size; i++)
 {
 kecil=i;
 for (j=i; j<size; j++)
 {
 if (array[kecil]>array[j])
 {kecil = j;}
 }
 temp = array [i];
 array[i] = array[kecil];
 array[kecil] = temp;
 
 }
 }

int main()
{
 int NumList[8] = {5, 34, 32, 25, 75, 42, 22, 2};
 int temp;
 cout<<"Data sebelum diurutkan: \n";
 for (int d=0; d<8; d++)
 {
 cout<<setw(3)<<NumList[d];
 }
 
 cout<<"\n\n";
 selectionsort(NumList,8);
 
 cout<<"Data setelah diurutkan:\n";
 for (int iii=0; iii<8; iii++)
 cout<<setw(3)<<NumList[iii]<<endl<<endl;
 getche ();
}

Hasil

ScreenHunter_198 Feb. 22 16.16

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s