Pengurutan Stack
#include <iostream.h>
#include <conio.h>
int data[10],data2[10];
int jml_index;
void tukar(int a,int b)
{ int temp;
temp = data[b];
data[b] = data[a];
data[a] = temp;}
void Tampil(int data[])
{ cout<<"Isi array : ";
for (int i=0;i<jml_index;i++)
{cout<<data[i]<<" ";}
cout<<endl;}
void SelectionSortASC()
{ cout<<"Kondisi sebelum selection sort ASC "<<endl;
Tampil(data);
int posisi,i,j;
for(i=0;i<jml_index-1;i++)
{ posisi = i;
for (j=i+1;j<jml_index;j++)
{if (data[j] < data[posisi])
{posisi = j;} }
if (posisi !=i)
{tukar(posisi,i);}
}
cout<<"kondisi setelah selection sort ASC"<<endl;
Tampil(data);
}
void SelectionSortDESC()
{ cout<<"kondisi sebelum selection sort DESC"<<endl;
Tampil(data);
int posisi,i,j;
for(i=0;i<jml_index-1;i++)
{ posisi = i;
for (j=i+1;j<jml_index;j++)
{ if (data[j] > data [posisi])
{posisi = j;}}
if(posisi !=i)
{tukar(posisi,i);}
}
cout<<"kondisi setelah selection sort DESC"<<endl;
Tampil(data);
}
void InsertionSortASC()
{ cout<<"kondisi sebelum insertion sort ASC"<<endl;
Tampil(data2);
int temp,i,j;
for(i=1;i<jml_index;i++)
{
temp = data2[i];
j=i-1;
while (data2[j]>temp && j>=0)
{ data2[j+1] = data2[j];
j--;}
data2[j+1]=temp;
}
cout<<"kondisi setelah Intertion sort ASC"<<endl;
Tampil(data2);
}
void InsertionSortDESC()
{ cout<<"kondisi sebelum Insertion sort DESC"<<endl;
Tampil(data2);
int temp,i,j;
for(i=1;i<jml_index;i++)
{ temp = data2[i];
j=i-1;
while (data2[j]<temp && j>=0)
{ data2[j+1] = data2[j];
j--;}
data2[j+1]=temp;
}
cout<<"kondisi setelah intertion sort DESC"<<endl;
Tampil(data2);
}
void Input()
{
cout<<"masukkan jumlah data (1 s/d 10) = ";
cin>>jml_index;
if (jml_index<1 || jml_index>10)
{cout<<"jml yg anda masukkan salah";}
else
{
for (int i=0;i<jml_index;i++)
{
cout<<"masukkan data ke- "<<(i+1)<<" = ";
cin>>data[i];
data2[i]=data[i];
}
}
}
void Seq_Search(int cari)
{
int posisi=0, ketemu=0;
for(int i=0;i<jml_index;i++)
{
if(data[i]==cari)
{
ketemu = 1;
posisi = i;
break;
}
}
if (ketemu == 1)
{cout<<"Data "<<cari<<" ada pada posisi : "<<posisi;}
else
{cout<<"Data "<<cari<<" TIDAK DITEMUKAN. ";}
}
void Bin_Search(int cari)
{
cout<<"\t== PROSES PENGURUTAN DATA =="<<endl;
SelectionSortASC();
int ketemu=0, kiri=0, kanan=(jml_index-1), tengah;
while (kiri<=kanan && ketemu==0)
{
tengah = (kiri+kanan)/2;
if(data[tengah]==cari)
ketemu =1;
else if (cari<data[tengah])
kanan = tengah-1;
else
kiri = tengah+1;
}
if(ketemu==1)
{cout<<"Data "<<cari<<" ada pada posisi : "<<tengah;}
else
{cout<<"Data "<<cari<<" TIDAK DITEMUKAN. ";}
}
void main()
{
int pil;
clrscr();
do
{
clrscr();
cout<<"1. Input data"<<endl;
cout<<"2. Selection sort ASC"<<endl;
cout<<"3. Selection sort DESC"<<endl;
cout<<"4. Insertion sort ASC"<<endl;
cout<<"5. Insertion sort DESC"<<endl;
cout<<"6. Keluar"<<endl;
cout<<"7. Sequential"<<endl;
cout<<"8. Binary"<<endl;
cout<<"======================="<<endl;
cout<<"Masukkan pilihan = ";cin>>pil;
switch(pil)
{
case 1:Input();break;
case 2:SelectionSortASC();break;
case 3:SelectionSortDESC();break;
case 4:InsertionSortASC();break;
case 5:InsertionSortDESC();break;
case 7:Seq_Search;break;
case 8:Bin_Search;break;
}
getch();
}while (pil!=6);
}
Comments
Post a Comment