Jumat, 24 Juni 2011

Program C++ modifikasi Selection Sort


wooow ... .. increasingly dizzying friends. I can be of practical tasks which make c++ program to sort the numbers from the largest and the smallest of the method of selection sort. But we must keep trying and learning. I'll give a little knowledge of what I learned with my friends.
Hmmm .... Friends can see the algorithms and code,,, check it out .. :)

Algoritma :
{ Mengurutkan bilangan dari yang terbesar dan dari yang terkecil }
Deklarasi
n               : integer (input)
a               : integer of array (output)

Deskripsi
Read(n)
for i <= 0 to n do
write (a[i])
endfor
for j <= 0 to n do
write (a[j])
endfor
int temp
temp=a
a=b
b=temp
minimum :
int i, min
min = a[dari]
tempat = dari
for i <= dari + 1 to n do
if ( a[i] < min )
min=a[i]
tempat=i
endif
endfor
maksimum :
int i, max
max = a[dari]
tempat = dari
for i <= dari +1 do
if ( a[i] > max )
max=a[i]
tempat=i
Selection :
int i, t
for i <= 0 to n do
{minimum(i, n, t)
tukar(a[i],a[t])
endfor
for i <= 0 to n do
{maksimum(i, n, t)
tukar(a[i],a[t])
endfor
 
Berikut kode programnya :

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

class selection_sort{

public :
void cetak_input();
void cetak_array();
void tukar(int& , int& );
void minimum(  int, int,int&);
void maksimum(  int, int,int&);
void selection();
void selection2();


private:
int a[100];
int n;
};

void selection_sort::cetak_input()
{
cout << "masukkan banyaknya data :"; cin >> n;

for (int i=0; i< n; i++)
{cout << "a["<<i<<"]= ";cin >> a[i];}

}
void selection_sort::cetak_array()
{
cout << "setelah di sorting"<<endl;
for (int j=0; j<n; j++)
{cout << "a["<<j<<"]= "<<a[j]<<endl;}
}

void selection_sort::tukar(int &a, int &b )
{
int temp;

temp=a;
a=b;
b=temp;
}

void selection_sort::minimum(  int dari, int n,int &tempat)
{
int i, min;
min=a[dari];
tempat=dari;
for (i=dari+1; i<n; i++)
{if(a[i]<min)
{
min=a[i];
tempat=i;}
}
}

void selection_sort::maksimum(  int dari, int n,int &tempat)
{
int i, max;
max=a[dari];
tempat=dari;
for (i=dari+1; i<n; i++)
{if(a[i]>max)
{
max=a[i];
tempat=i;}
}
}

void selection_sort::selection()
{
int i, t;
for (i=0; i<n;i++)
{minimum(i, n, t);
tukar(a[i],a[t]);
}
}

void selection_sort::selection2()
{
int i, t;
for (i=0; i<n;i++)
{maksimum(i, n, t);
tukar(a[i],a[t]);
}
}

int main()
{
selection_sort x;
x.cetak_input();
x.selection();
x.cetak_array();
x.selection2();
x.cetak_array();

getch();

return 0;
}


explanation:
The above program is a program to sort the numbers from the largest and the smallest of the selection sort method. Which users will menginputkan integer random set of numbers then the program will automatically sort the data. So that the output will display the data sorted from the smallest and dafta sorted from the largest.

0 komentar:

Posting Komentar

Template by:

Free Blog Templates