Contoh Program sort, binary, sequential pada C++
Ini adalah program sorting paling lengkap.
contoh program sort c++ :
#include
#include
int data[100],data2[100];
int n;
void tukar(int a,int b)
{
int t;
t = data[b];
data[b] = data[a];
data[a] = t;
}
void bubble_sort()
{
for(int i=1;i
{
for(int j=n-1;j>=i;j–)
{
if(data[j]
}
}
cout<<”bubble sort selesai!”<
}
void exchange_sort()
{
for (int i=0; i
{
for(int j = (i+1); j
{
if (data [i] > data[j]) tukar(i,j);
}
}
cout<<”exchange sort selesai!”<
}
void selection_sort()
{
int pos,i,j;
for(i=0;i
{
pos = i;
for(j = i+1;j
{
if(data[j] < data[pos]) pos = j;
}
if(pos != i) tukar(pos,i);
}
cout<<”selection sort selesai!”<
}
void insertion_sort()
{
int temp,i,j;
for(i=1;i
{
temp = data[i];
j = i -1;
while(data[j]>temp && j>=0)
{
data[j+1] = data[j];
j–;
}
data[j+1] = temp;
}
cout<<”insertion sort selesai!”<
}
void QuickSort(int L, int R) //the best sort i’ve ever had
{
int i, j;
int mid;
i = L;
j = R;
mid = data[(L+R) / 2];
do
{
while (data[i] < mid) i++;
while (data[j] > mid) j–;
if (i <= j)
{
tukar(i,j);
i++;
j–;
};
} while (i < j);
if (L < j) QuickSort(L, j);
if (i < R) QuickSort(i, R);
}
void Input()
{
cout<<”Masukkan jumlah data = “; cin>>n;
for(int i=0;i
{
cout<<”Masukkan data ke-”<<(i+1)<<” = “; cin>>data[i];
data2[i] = data[i];
}
}
void Tampil()
{
cout<<”Data : “<
for(int i=0;i
{
cout<<<” “;
}
cout<
}
void AcakLagi()
{
for(int i=0;i
{
data[i] = data2[i];
}
cout<<”Data sudah teracak!”<
}
void main()
{
int pil;
clrscr();
do
{
clrscr();
cout<<”Program Sorting Komplit!!!”<
cout<<”*********************************************”<
cout<<” 1. Input Data”<
cout<<” 2. Bubble Sort”<
cout<<” 3. Exchange Sort”<
cout<<” 4. Selection Sort”<
cout<<” 5. Insertion Sort”<
cout<<” 6. Quick Sort”<
cout<<” 7. Tampilkan Data”<
cout<<” 8. Acak Data”<
cout<<” 9. Exit”<
cout<<” Pilihan Anda = “; cin>>pil;
switch(pil)
{
case 1:Input(); break;
case 2:bubble_sort(); break;
case 3:exchange_sort(); break;
case 4:selection_sort(); break;
case 5:insertion_sort(); break;
case 6:QuickSort(0,n-1);
cout<<”quick sort selesai!”<
break;
case 7:Tampil(); break;
case 8:AcakLagi(); break;
}
getch();
}while(pil!=9);
}
contoh program searching (binary) pada c++ :
#include
#include
int data[10] = {1,3,4,7,12,25,40,65,78,90}; //variabel global
int binary_search(int cari)
{
int l,r,m;
int n = 10;
l = 0;
r = n-1;
int ketemu = 0;
while(l<=r && ketemu==0)
{
m = (l+r)/2;
if( data[m] == cari )
ketemu = 1;
else
if (cari < data[m])
r = m-1;
else l = m+1;
}
if(ketemu == 1) return 1; else return 0;
}
void main()
{
clrscr();
int cari,hasil;
cout<<”masukkan data yang ingin dicari = “;
cin>>cari;
hasil = binary_search(cari);
if(hasil == 1)
{
cout<<”Data ada!”<
}
else
if(hasil == 0)
cout<<”Data Tidak ada!”<
getch();
}
contoh program searching (sequential) pada c++ :
#include
#include
void main()
{
clrscr();
int data[8] = {8,10,6,-2,10,7,1,100};
int cari,index;
int ketemu=0;
cout<<”masukkan data yang ingin dicari = “;
cin>>cari;
for(int i=0;i<8;i++)
{
if(data[i] == cari)
{
ketemu=1;
index = i;
break;
}
}
if(ketemu == 1)
{
cout<<”Data ada!”<
cout<<”Data terletak di index ke – “<
}
else cout<<”Data Tidak ada!”<
getch();
}
Ini adalah program sorting paling lengkap.
contoh program sort c++ :
#include
#include
int data[100],data2[100];
int n;
void tukar(int a,int b)
{
int t;
t = data[b];
data[b] = data[a];
data[a] = t;
}
void bubble_sort()
{
for(int i=1;i
{
for(int j=n-1;j>=i;j–)
{
if(data[j]
}
}
cout<<”bubble sort selesai!”<
}
void exchange_sort()
{
for (int i=0; i
{
for(int j = (i+1); j
{
if (data [i] > data[j]) tukar(i,j);
}
}
cout<<”exchange sort selesai!”<
}
void selection_sort()
{
int pos,i,j;
for(i=0;i
{
pos = i;
for(j = i+1;j
{
if(data[j] < data[pos]) pos = j;
}
if(pos != i) tukar(pos,i);
}
cout<<”selection sort selesai!”<
}
void insertion_sort()
{
int temp,i,j;
for(i=1;i
{
temp = data[i];
j = i -1;
while(data[j]>temp && j>=0)
{
data[j+1] = data[j];
j–;
}
data[j+1] = temp;
}
cout<<”insertion sort selesai!”<
}
void QuickSort(int L, int R) //the best sort i’ve ever had
{
int i, j;
int mid;
i = L;
j = R;
mid = data[(L+R) / 2];
do
{
while (data[i] < mid) i++;
while (data[j] > mid) j–;
if (i <= j)
{
tukar(i,j);
i++;
j–;
};
} while (i < j);
if (L < j) QuickSort(L, j);
if (i < R) QuickSort(i, R);
}
void Input()
{
cout<<”Masukkan jumlah data = “; cin>>n;
for(int i=0;i
{
cout<<”Masukkan data ke-”<<(i+1)<<” = “; cin>>data[i];
data2[i] = data[i];
}
}
void Tampil()
{
cout<<”Data : “<
for(int i=0;i
{
cout<<<” “;
}
cout<
}
void AcakLagi()
{
for(int i=0;i
{
data[i] = data2[i];
}
cout<<”Data sudah teracak!”<
}
void main()
{
int pil;
clrscr();
do
{
clrscr();
cout<<”Program Sorting Komplit!!!”<
cout<<”*********************************************”<
cout<<” 1. Input Data”<
cout<<” 2. Bubble Sort”<
cout<<” 3. Exchange Sort”<
cout<<” 4. Selection Sort”<
cout<<” 5. Insertion Sort”<
cout<<” 6. Quick Sort”<
cout<<” 7. Tampilkan Data”<
cout<<” 8. Acak Data”<
cout<<” 9. Exit”<
cout<<” Pilihan Anda = “; cin>>pil;
switch(pil)
{
case 1:Input(); break;
case 2:bubble_sort(); break;
case 3:exchange_sort(); break;
case 4:selection_sort(); break;
case 5:insertion_sort(); break;
case 6:QuickSort(0,n-1);
cout<<”quick sort selesai!”<
break;
case 7:Tampil(); break;
case 8:AcakLagi(); break;
}
getch();
}while(pil!=9);
}
contoh program searching (binary) pada c++ :
#include
#include
int data[10] = {1,3,4,7,12,25,40,65,78,90}; //variabel global
int binary_search(int cari)
{
int l,r,m;
int n = 10;
l = 0;
r = n-1;
int ketemu = 0;
while(l<=r && ketemu==0)
{
m = (l+r)/2;
if( data[m] == cari )
ketemu = 1;
else
if (cari < data[m])
r = m-1;
else l = m+1;
}
if(ketemu == 1) return 1; else return 0;
}
void main()
{
clrscr();
int cari,hasil;
cout<<”masukkan data yang ingin dicari = “;
cin>>cari;
hasil = binary_search(cari);
if(hasil == 1)
{
cout<<”Data ada!”<
}
else
if(hasil == 0)
cout<<”Data Tidak ada!”<
getch();
}
contoh program searching (sequential) pada c++ :
#include
#include
void main()
{
clrscr();
int data[8] = {8,10,6,-2,10,7,1,100};
int cari,index;
int ketemu=0;
cout<<”masukkan data yang ingin dicari = “;
cin>>cari;
for(int i=0;i<8;i++)
{
if(data[i] == cari)
{
ketemu=1;
index = i;
break;
}
}
if(ketemu == 1)
{
cout<<”Data ada!”<
cout<<”Data terletak di index ke – “<
}
else cout<<”Data Tidak ada!”<
getch();
}