#include<iostream.h>
int n, data[100];
void tukar(int a, int b)
{
int tmp;
tmp=data[a];
data[a]=data[b];
data[b]=tmp;
}
void main ()
{
cout<<"Input Jumlah Anggota Array : ";
cin>>n;
for (int i=0; i<n; i++)
{
cout<<"Array A ke - "<<i+1<<" = ";
cin>>data[i];
}
cout<<endl;
int pilih;
cout<<"===================="<<endl;
cout<<" Menu pilihan : "<<endl;
cout<<"===================="<<endl;
cout<<"1. Bubble Sort"<<endl;
cout<<"2. Selection Sort"<<endl;
cout<<"3. Insertion Sort"<<endl;
cout<<"4. Keluar Program"<<endl;
cout<<"===================="<<endl;
cout<<"Pilihan:";
cin>>pilih;
switch (pilih)
{
case 1:
{
cout<<"1. Bubble Sort"<<endl;
int i, j;
cout<<"data array = ";
for(i=0; i<n; i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
for(i=1; i<=n; i++)
{
for(j=n-1; j>=i; j--)
{
if(data[j]<data[j-1]) tukar(j,j-1);
}
}
cout<<endl;
cout<<"data hasil sort : "<<endl;
for(int k=0; k<n; k++)
{
cout<<data[k]<<" ";
}
cout<<endl;
}
break;
case 2:
{
cout<<"2. Selection Sort"<<endl;
int i, j, pos=0;
cout<<"data array = ";
for(i=0; i<n; i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
for(i=0; 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);
}
cout<<endl;
cout<<"data hasil sort : "<<endl;
for(i=0; i<n; i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
break;
case 3:
{
cout<<"3. Insertion Sort"<<endl;
int temp,i,j;
for(i=1;i<n;i++)
{
temp = data[i];
j = i-1;
while(data[j]>temp && j>=0)
{
data[j+1] = data[j];
j--;
}
data[j+1] = temp;
}
cout<<endl;
cout<<"data hasil sort : "<<endl;
for(i=0; i<n; i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
break;
cout<<endl;
}
}