Daftarkan Akun Anda

We'll not spam mate! We promise.

Senin, 09 Maret 2015

Contoh Program C++ Selection Sort dan Bubble Sort

Hai sobat blog kali ini saya ingin share coding selection sort dan bubble sort sobat proggramer pasti udah mengetahui bukan apa itu selection sort dan bubble sort ,untuk mempersingkat waktu langsung aja yu

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int data[10],data2[10];
int n;
void tukar(int a,int b)
{
int t;
t = data[b];
data[b] = data[a];
data[a] = t;
}

void Input()
{
cout<<"Masukkan jumlah data = ";cin>>n;
cout<<"--------------------------------------"<<endl;
for(int i=1;i<=n;i++)
{
cout<<"Masukkan data ke-"<<(i)<<" = ";cin>>data[i];
data2[i] = data[i];
}
cout<<endl;
}

void Tampil()
{
for(int i=1;i<=n;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}

void bubble_sort()
{
for(int i=1;i<=n;i++)
{
for(int j=n;j>=i;j--)
{
if(data[j]<data[j-1]) tukar(j,j-1);
}
Tampil();
}
cout<<endl;
}

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

main()
{
system("cls");
int menu;
char awal;
cout<<" NAMA  : BUDI UTOMO "<<endl;
cout<<" NIM  : 2013142122 "<<endl;
cout<<" KELAS : 03TPLME    \n"<<endl;
cout<<"======================================="<<endl;
cout<<"=             Welcome to              ="<<endl;
cout<<"=     Bubble and Selection Sort       ="<<endl;
cout<<"=======================================\n"<<endl;
cout<<"Daftar Pilihan Menu : "<<endl;
cout<<"1. bubble sort"<<endl;
cout<<"2. Selection sort"<<endl;
cout<<"masukan menu 1-2 =";cin>>menu;
switch(menu)
{
case 1:
system("cls");
cout<<"*-------------------------------------*"<<endl;
cout<<"*      Selamat datang di aplikasi     *"<<endl;
cout<<"*             Bubble Sort             *"<<endl;
cout<<"*-------------------------------------*"<<endl;
Input();
cout<<"Proses Bubble Sort,,,,,,,"<<endl;
cout<<"-------------------------------------"<<endl;
Tampil();
bubble_sort();
cout<<"-------------------------------------"<<endl;
cout<<" TERIMA KASIH "<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"\n"<<endl;
cout<<"Apakah anda ingin kembali ke menu awal ? ( y / n )"<<endl;
cin>>awal;
if(awal=='y')
{
main();
}
break;

case 2:
system("cls");
cout<<"*-------------------------------------*"<<endl;
cout<<"*      Selamat datang di aplikasi     *"<<endl;
cout<<"*            Selection Sort           *"<<endl;
cout<<"*-------------------------------------*"<<endl;
Input();
cout<<"Proses Selection Sort,,,,,,,"<<endl;
cout<<"-------------------------------------"<<endl;
Tampil();
selection_sort();
cout<<"-------------------------------------"<<endl;
cout<<" TERIMA KASIH "<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"\n"<<endl;
cout<<"Apakah anda ingin kembali ke menu awal ? ( y / n )"<<endl;
cin>>awal;
if(awal=='y')
{
main();
}
break;
default:
system("cls");
cout<<"==================================="<<endl;
cout<<"=   WARNING     ="<<endl;
cout<<"= pilihan yang anda masukan salah ="<<endl;
cout<<"==================================="<<endl;
cout<<"\n"<<endl;
cout<<"Apakah anda ingin kembali ke menu awal ? ( y / n )"<<endl;
cin>>awal;
if(awal=='y')
{
main();
}

}
}
nah sekian dari saya semogga bermanfaat

Socializer Widget By Blogger Yard
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 komentar:

Posting Komentar