Selection sort

Posted: 1 Juli 2013 in Praktikum Alpro

Source C++ :

#include <cstdlib>
#include <iostream>

using namespace std;

class Sort{
public:
void tukar(int a, int b);
void selection_sort();

private:
int data[10],data2[10];
int n;
};

void Sort::tukar(int a, int b)
{
int t;
t = data[b];
data[b] = data[a];
data[a] = t;
}

void Sort::selection_sort()
{
int pos,i,j;
for(i=1;i<=n-1;i++)
{
pos = i;
for(j = i+1;j<=n;j++)
{
if(data[j] < data[pos]) pos = j;
}
if(pos != i) tukar(pos,i);
}
}

int main()
{
int n,data[10],data2[10];
cout<<“===PROGRAM SELECTION SORT===”<<endl;

cout<<“Masukkan Jumlah Data : “;
cin>>n;
for(int i=1;i<=n;i++)
{

cout<<“Masukkan data ke “<<i<<” : “;
cin>>data[i];
data2[i]=data[i];
}

//selection_sort();
cout<<“Data Setelah di Sort : “;
for(int i=1; i<=n; i++)
{
cout<<” “<<data[i];
}
cout<<“\n\nSorting dengan selection sort Selesai”;

Sort w;
w.tukar(int a, int b);
w.selection_sort();

system(“PAUSE”);
return EXIT_SUCCESS;
}

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