Selasa, 01 Maret 2011

buble sort

#include
#include
using namespace std;
void DataRandom();
void DataManual();
void urutkan(int *errei, int ukuranArray);
int main()
{
srand((unsigned)time(NULL));
char menuDiPilih;
cout << "1. data random" << endl;
cout << "2. data manual" << endl;
cin >> menuDiPilih;
if(menuDiPilih=='1')
DataRandom();
else if(menuDiPilih=='2')
DataManual();
system("pause");
return 0;
}
void DataRandom()
{
int ukuranArray, rentangNilai, *errei;
cout << "ukuran array : ";
cin >> ukuranArray;
cout << "rentang nilai : ";
cin >> rentangNilai;
errei = new int[ukuranArray];
cout << "data hasil random:\n";
for(int i=0; i < ukuranArray; i++)
{
errei[i]=rand()%rentangNilai;
cout << errei[i] << " ";
}
cout << endl;
urutkan(errei, ukuranArray);
}
void urutkan(int *errei, int ukuranArray)
{
int index1, index2;
for(index1=0;index1 < ukuranArray-1;index1++)
for(index2=index1+1;index2 if(errei[index1]>errei[index2])
{
errei[index1]+=errei[index2];
errei[index2]=errei[index1]-errei[index2];
errei[index1]-=errei[index2];
}
cout << "data setelah diurutkan dengan buble sort:\n";
for(index1=0;index1 < ukuranArray;index1++)
cout << errei[index1] << " ";
cout << endl;
}
void DataManual()
{
int ukuranArray, *errei;
cout << "ukuran array: ";
cin >> ukuranArray;
errei=new int[ukuranArray];
cout << "mengisi array dari indeks ke 0 sampai ke " << ukuranArray-1 << endl;
for(int i=0;i cin >> errei[i];
urutkan(errei,ukuranArray);
}

Tidak ada komentar:

Posting Komentar