dimana sorting data buble sort adalah sorting paling mudah dan sorting paling lambat di pemograman C++.
Konsep dasarnya yaitu : “Melakukan pembandingan antara ’data[n] dengan data[n+1]’ atau antara ’data[n] dengan data[n-1]’ kemudian jika lebih kecil/besar dilakukan swap (pertukaran). Pada setiap iterasi dapat terjadi beberapa kali swap(pertukaran) atau tidak sama sekali. Jumlah iterasi ditentukan oleh banyaknya data atau ‘N’. Iterasi=N-1.”
karena saya membuat sorting ini sebagai tugas kuliah saya maka saya ambil data data yang ada di kampus saya berupa NRP , IP dan Nama Mahasiswa.
Coding C++ nya seperti ini !!!
/*
NAMA PROGRAM : INPUT,VIEW,SORT DAN SAVE DATA
NAMA PROGRAMER : HANAFI SUKAWA GINTING
*/
#include<constream.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
#include<dos.h>
#include<string.h>
class sorting
{
public:
void sortnrpa();
void sortnrpb();
void sortipa();
void sortipb();
void sortingnrp();
void sortingip();
void sortingnama();
void sortnama1();
void sortnama2();
}clssort;
class menu
{
public:
void input();
void output();
void quit();
void sort();
void save();
void menu2();
struct datalpkia
{
char nama[20];
long int nrp;
long float ip;
}strlpkia[100];
}clsslpkia;
long i,d;
long x,y,tmp,tam;
char temp[20];
long float sum[100];
long float avg;
void main()
{
clsslpkia.menu2();
}
void menu::menu2()
{
clrscr();
int pil;
do
{
clrscr();
gotoxy(12,4);cout<<"Data Mahasiswa LPKIA"<<endl;
gotoxy(12,6);cout<<"1.Input Data "<<endl;
gotoxy(12,7);cout<<"2.View"<<endl;
gotoxy(12,8);cout<<"3.sort"<<endl;
gotoxy(12,9);cout<<"4.Save"<<endl;
gotoxy(12,10);cout<<"5.QUit"<<endl;
gotoxy(12,12);cout<<"Masukan Pilihan [1 - 5] :";
cin>>pil;
switch(pil)
{
case 1:clsslpkia.input();break;
case 2:clsslpkia.output();break;
case 3:clsslpkia.sort();break;
case 4:clsslpkia.save();break;
case 5:clrscr();break;
default : cout<<"Pilihan Tidak ada"<<endl;
}
}
while(pil !=5);
getch();
}
void menu::sort()
{
int lih;
do
{
clrscr();
gotoxy(12,4);cout<<"Pilih Sorting"<<endl;
gotoxy(12,6);cout<<"1.NRP "<<endl;
gotoxy(12,7);cout<<"2.IP"<<endl;
gotoxy(12,8);cout<<"3.Nama"<<endl;
gotoxy(12,9);cout<<"4.Back to menu"<<endl;
gotoxy(12,11);cout<<"Masukan Pilihan [1 - 4] :";
cin>>lih;
switch(lih)
{
case 1:clssort.sortingnrp();break;
case 2:clssort.sortingip();break;
case 3:clssort.sortingnama();break;
case 4:clrscr();break;
default : cout<<"Pilihan Tidak ada"<<endl;
}
}
while(lih !=4);
}
void sorting::sortingnrp()
{
int lih;
do
{
clrscr();
gotoxy(12,4);cout<<"Pilih Sorting NRP"<<endl;
gotoxy(12,6);cout<<"1.Ascending "<<endl;
gotoxy(12,7);cout<<"2.descending"<<endl;
gotoxy(12,8);cout<<"3.Back"<<endl;
gotoxy(12,10);cout<<"Masukan Pilihan [1 - 2] :";
cin>>lih;
switch(lih)
{
case 1:clssort.sortnrpa();break;
case 2:clssort.sortnrpb();break;
case 3:clsslpkia.sort();break;
default : cout<<"Pilihan Tidak ada"<<endl;
}
}
while(lih !=3);
}
void sorting::sortingip()
{
int lih;
do
{
clrscr();
gotoxy(12,4);cout<<"Pilih Sorting IP"<<endl;
gotoxy(12,6);cout<<"1.Ascending "<<endl;
gotoxy(12,7);cout<<"2.descending"<<endl;
gotoxy(12,8);cout<<"3.Back"<<endl;
gotoxy(12,10);cout<<"Masukan Pilihan [1 - 2] :";
cin>>lih;
switch(lih)
{
case 1:clssort.sortipa();break;
case 2:clssort.sortipb();break;
case 3:clsslpkia.sort();break;
default : cout<<"Pilihan Tidak ada"<<endl;
}
}
while(lih !=3);
}
void sorting::sortingnama()
{
int lih;
do
{
clrscr();
gotoxy(12,4);cout<<"Pilih Sorting Nama"<<endl;
gotoxy(12,6);cout<<"1.Ascending "<<endl;
gotoxy(12,7);cout<<"2.descending"<<endl;
gotoxy(12,8);cout<<"3.Back"<<endl;
gotoxy(12,10);cout<<"Masukan Pilihan [1 - 2] :";
cin>>lih;
switch(lih)
{
case 1:clssort.sortnama1();break;
case 2:clssort.sortnama2();break;
case 3:clsslpkia.sort();break;
default : cout<<"Pilihan Tidak ada"<<endl;
}
}
while(lih !=3);
}
void menu::input()
{
clrscr();
gotoxy(12,12);cout<<"Masukan Jumlah Data :";
cin>>d;
clrscr();
sum[0] = 0;
for(i=0;i<d;i++)
{
cout<<"DATA KE "<<i+1<<"\n"<<endl;
cout<<"Masukan Nama : ";
gets (clsslpkia.strlpkia[i].nama);
cout<<"Masukan Nrp : ";
cin>>clsslpkia.strlpkia[i].nrp;
cout<<"Masukan IP : ";
cin>>clsslpkia.strlpkia[i].ip;
cout<<"\n"<<endl;
sum[i + 1] = sum[i] + clsslpkia.strlpkia[i].ip;
}
avg = sum[d] / d;
quit();
getch();
}
void sorting::sortnrpa()
{
for(x=0;x<d-1;x++)
{
for(y=0;y<d-1;y++)
{
if (clsslpkia.strlpkia[y].nrp>clsslpkia.strlpkia[y+1].nrp)
{
tmp=clsslpkia.strlpkia[y].nrp;
clsslpkia.strlpkia[y].nrp=clsslpkia.strlpkia[y+1].nrp;
clsslpkia.strlpkia[y+1].nrp=tmp;
tam=clsslpkia.strlpkia[y].ip;
clsslpkia.strlpkia[y].ip=clsslpkia.strlpkia[y+1].ip;
clsslpkia.strlpkia[y+1].ip=tam;
strcmp(clsslpkia.strlpkia[y].nama,clsslpkia.strlpkia[y+1].nama);
strcpy (temp,clsslpkia.strlpkia[y].nama);
strcpy (clsslpkia.strlpkia[y].nama,clsslpkia.strlpkia[y+1].nama);
strcpy (clsslpkia.strlpkia[y+1].nama,temp);
}
}
}
clsslpkia.output();
}
void sorting::sortnrpb()
{
for(x=0;x<d-1;x++)
{
for(y=0;y<d-1;y++)
{
if (clsslpkia.strlpkia[y].nrp<clsslpkia.strlpkia[y+1].nrp)
{
tmp=clsslpkia.strlpkia[y].nrp;
clsslpkia.strlpkia[y].nrp=clsslpkia.strlpkia[y+1].nrp;
clsslpkia.strlpkia[y+1].nrp=tmp;
tam=clsslpkia.strlpkia[y].ip;
clsslpkia.strlpkia[y].ip=clsslpkia.strlpkia[y+1].ip;
clsslpkia.strlpkia[y+1].ip=tam;
strcmp(clsslpkia.strlpkia[y].nama,clsslpkia.strlpkia[y+1].nama);
strcpy (temp,clsslpkia.strlpkia[y].nama);
strcpy (clsslpkia.strlpkia[y].nama,clsslpkia.strlpkia[y+1].nama);
strcpy (clsslpkia.strlpkia[y+1].nama,temp);
}
}
}
clsslpkia.output();
}
void sorting::sortipa()
{
for(x=0;x<d-1;x++)
{
for(y=0;y<d-1;y++)
{
if (clsslpkia.strlpkia[y].ip>clsslpkia.strlpkia[y+1].ip)
{
tam=clsslpkia.strlpkia[y].ip;
clsslpkia.strlpkia[y].ip=clsslpkia.strlpkia[y+1].ip;
clsslpkia.strlpkia[y+1].ip=tam;
tmp=clsslpkia.strlpkia[y].nrp;
clsslpkia.strlpkia[y].nrp=clsslpkia.strlpkia[y+1].nrp;
clsslpkia.strlpkia[y+1].nrp=tmp;
strcmp(clsslpkia.strlpkia[y].nama,clsslpkia.strlpkia[y+1].nama);
strcpy (temp,clsslpkia.strlpkia[y].nama);
strcpy (clsslpkia.strlpkia[y].nama,clsslpkia.strlpkia[y+1].nama);
strcpy (clsslpkia.strlpkia[y+1].nama,temp);
}
}
}
clsslpkia.output();
}
void sorting::sortipb()
{
for(x=0;x<d-1;x++)
{
for(y=0;y<d-1;y++)
{
if (clsslpkia.strlpkia[y].ip<clsslpkia.strlpkia[y+1].ip)
{
tam=clsslpkia.strlpkia[y].ip;
clsslpkia.strlpkia[y].ip=clsslpkia.strlpkia[y+1].ip;
clsslpkia.strlpkia[y+1].ip=tam;
tmp=clsslpkia.strlpkia[y].nrp;
clsslpkia.strlpkia[y].nrp=clsslpkia.strlpkia[y+1].nrp;
clsslpkia.strlpkia[y+1].nrp=tmp;
strcmp(clsslpkia.strlpkia[y].nama,clsslpkia.strlpkia[y+1].nama);
strcpy (temp,clsslpkia.strlpkia[y].nama);
strcpy (clsslpkia.strlpkia[y].nama,clsslpkia.strlpkia[y+1].nama);
strcpy (clsslpkia.strlpkia[y+1].nama,temp);
}
}
}
clsslpkia.output();
}
void sorting::sortnama1()
{
for(x=0;x<d-1;x++)
{
for(y=0;y<d-1;y++)
{
if (strcmp(clsslpkia.strlpkia[y].nama,clsslpkia.strlpkia[y+1].nama)>0)
{
strcpy (temp,clsslpkia.strlpkia[y].nama);
strcpy (clsslpkia.strlpkia[y].nama,clsslpkia.strlpkia[y+1].nama);
strcpy (clsslpkia.strlpkia[y+1].nama,temp);
tmp=clsslpkia.strlpkia[y].nrp;
clsslpkia.strlpkia[y].nrp=clsslpkia.strlpkia[y+1].nrp;
clsslpkia.strlpkia[y+1].nrp=tmp;
tam=clsslpkia.strlpkia[y].ip;
clsslpkia.strlpkia[y].ip=clsslpkia.strlpkia[y+1].ip;
clsslpkia.strlpkia[y+1].ip=tam;
}
}
}
clsslpkia.output();
}
void sorting::sortnama2()
{
for(x=0;x<d-1;x++)
{
for(y=0;y<d-1;y++)
{
if (strcmp(clsslpkia.strlpkia[y].nama,clsslpkia.strlpkia[y+1].nama)<0)
{
strcpy (temp,clsslpkia.strlpkia[y].nama);
strcpy (clsslpkia.strlpkia[y].nama,clsslpkia.strlpkia[y+1].nama);
strcpy (clsslpkia.strlpkia[y+1].nama,temp);
tmp=clsslpkia.strlpkia[y].nrp;
clsslpkia.strlpkia[y].nrp=clsslpkia.strlpkia[y+1].nrp;
clsslpkia.strlpkia[y+1].nrp=tmp;
tam=clsslpkia.strlpkia[y].ip;
clsslpkia.strlpkia[y].ip=clsslpkia.strlpkia[y+1].ip;
clsslpkia.strlpkia[y+1].ip=tam;
}
}
}
clsslpkia.output();
}
void menu::output()
{
clrscr();
gotoxy(19,2);cout<<"Data Data Mahasiswa "<<endl;
gotoxy(8,3);cout<<"ÉÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"<<endl;
gotoxy(8,4);cout<<"º NO º NRP º IP º Nama Mahasiswaº"<<endl;
gotoxy(8,5);cout<<"ÌÍÍÍÍÍÍÍÎÍÍÍÍÍÍÍÍÍÎÍÍÍÍÍÍÍÍÍÎÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹"<<endl;
for(i=0;i<=d;i++)
{
gotoxy(8,6+i);cout<<"º º º º º"<<endl;
for(x=1;x<=4;x++)
for(y=0;y<d;y++)
{
gotoxy(x*9+1,y+6);
{
if(x==1) cout<<y+1;
if(x==2)cout<<clsslpkia.strlpkia[y].nrp<<endl;
if(x==3)cout<<clsslpkia.strlpkia[y].ip<<endl;
if(x==4)cout<<clsslpkia.strlpkia[y].nama<<endl;
}
}
gotoxy(8,7+i); cout<<"ÈÍÍÍÍÍÍÍÊÍÍÍÍÍÍÍÍÍÊÍÍÍÍÍÍÍÍÍÊÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ"<<endl;
}
cout<<endl;
gotoxy(8,10+i);cout << "Rata-rata IP adalah " <<avg<<endl;
getch();
gotoxy(8,13+i);quit();
getch();
}
void menu::save()
{
char * npile;
gotoxy(12,15);cout<<"Masukan Nama file : ";cin>>npile;
ofstream hasil;
hasil.open(npile);
hasil<<"DATA - DATA MAHASISWA"<<endl;
for(y=0;y<d;y++)
{
for(x=1;x<=4;x++)
{
if(x==1) hasil<<"DATA Ke - "<<y+1<<endl;
if(x==2) hasil<<"NRP : "<<clsslpkia.strlpkia[y].nrp<<endl;
if(x==3) hasil<<"Nama : "<<clsslpkia.strlpkia[y].nama<<endl;
if(x==4) hasil<<"IP : "<<clsslpkia.strlpkia[y].ip<<endl;
}
}
hasil.close();
gotoxy(12,17);cout<<"data tersimpan dengan nama file "<<npile<<endl;
getch();
}
void menu::quit()
{
cout<<"Press any key to continue"<<endl;
}