1) Program Pemangkatan Rekusrsif
#include <cstdlib>
#include <iostream>
using namespace std;
/* Meghitung pangkat a^n, a>0 bilangan bulat tak-negatif
MAsukan : a,n
Keluaran : Nilai Perpangktan */
int pangkat (int a, int n){
if (n==1) return a; //algoritma
else return (a * pangkat(a,n-1));
}
int main(int argc, char *argv[])
{
int a, n;
cout<<"\tProgram Pemangkatan rekursif";
cout<<"\n\nMasukkan Bilangan \t: "; cin>>a;
cout<<"Masukan Pangkat \t: "; cin>>n;
cout<<"Hasilnya \t\t: "<<pangkat(a,n);
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar Hasil :
Gambar : 1 Pemangkatan Rekursif
2)Program Faktorial Rekusrsif
#include <cstdlib>
#include <iostream>
using namespace std;
/* Meghitung Faktorialt n, n bilangan bulat tak-negatif
dimana faktorial rekursif ,fak= n * faktorial(n-1)
MAsukan : n
Keluaran : Nilai Faktorial rekursif */
int faktorial (int n){
int fak;
if (n<=1) fak=1;
else (fak= n * faktorial(n-1)); return fak;
}
int main(int argc, char *argv[])
{
int n;
cout<<"\tProgram Faktorial rekursif";
cout<<"\n\nMasukkan Bilangan : "; cin>>n;
cout<<"Hasil dari "<<n<<"! \t: "<<faktorial(n);
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar: 2 Faktorial Rekursif
3)Program Penukaran Nilai
#include <cstdlib>
#include <iostream>
using namespace std;
/* Mencari nilai tukar a,b,
yang dimana membutuhkan temp untuk memnyimpan tukar sementara
MAsukan : a,b
Keluaran : b,a */
void tukar (int *a, int *b ){
int temp;
temp=*a;
*a=*b;
*b=temp; }
int main(int argc, char *argv[])
{
int a,b;
cout<<"\tProgram Tukar Nilai ";
cout<<"\n\nNilai awal A : "; cin>>a;
cout<<"Nilai awal B : "; cin>>b;
tukar(&a,&b);
cout<<"\nSetelah ditukar\n\nNilai Akhir A : "<<a;
cout<<"\nNilai Akhir B : "<<b;
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar : 3 Hasil Penukaran nilai
4)Program Pemfaktoran
#include <cstdlib>
#include <iostream>
using namespace std;
/* Program Pemfaktoran a,
yang mana i = 2 dan i itu sendiri kurang dari samadengan a-1,
a%i==0
MAsukan : a
Keluaran : Hasil pefaktoran*/
int faktor(int a){
for (int i=2; i<=a-1; i++){
if (a%i==0)
cout<<"\n\t"<<i<<endl;} }
int main(int argc, char *argv[])
{int a;
cout<<"\tProgram Pemfaktoran ";
cout<<"\nAngka Yang Ingin Di faktorkan : ";cin>>a;
cout<<"bilangan faktornya antara lain : ";
faktor(a);
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar: 4 Hasil Pemfaktoran
5)Program Sequential Sort
#include <cstdlib>
#include <iostream>
using namespace std;
/* Program Sequential Sort a,n,
yang mana n adalah banyak data yang akan di sequensial sort,
dan na adalah masukan data yang nantinya akan di sequensial sort
MAsukan : n, a
Keluaran : Hasil Sequensial sort*/
void Input(int a[], int n){
for (int i=1; i<=n; i++){
cout<<"Masukan data ke-"<<i<<" : ";
cin>>a[i];}}
void Cetak(int a[], int n){
for (int i=1; i<=n; i++){
cout<<"\nData ke-"<<i<<" adalah : "<<a[i];}}
void Sequential( int a[], int x, int n){
int i=1, idx;
while ( (i<n) && (a[i] != x))
{i++;}
if (a[i] == x ){
idx=i;
cout<<"data di array ke- "<<idx;}
else cout<<"data tidak ada!";
}
int main(int argc, char *argv[])
{
int n,a[10],x;
cout<<"\tProgram Sequential Sort 21 Maret 2012";
cout<<"\n\nMasukkan banyak data : "; cin>>n;
Input (a,n);
Cetak (a,n);
cout<<"\n\ndata yg di cari? ";
cin>>x;
Sequential (a,x,n);
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar:5 Hasil Sequensial Sort
6)Program Cek Bil Prima
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
/* Program Cek Bilangan Prima x,
dimana x=2 dan x= 3 serta x% i yang habis di bagi tiga
MAsukan : x
Keluaran : HHasil bilangan Prima Atau Bukan*/
int CekPrima(int x){
int y,i;
if(x==1) return false;
if(x==2 || x==3) return true;
else
y = ceil (sqrt(x)); /*pembulatan ke atas akar x*/
for(i=2; i <= y; i++)
if(x % i == 0)
return false;
return true;}
int main(int argc, char *argv[])
{
int x;
cout<<"\tProgram Cek Bil. Prima 21 Maret 2012\n";
cout<<"\nMasukkan angka: ";
cin>>x;
if (CekPrima (x)== true)
cout<<"bilangan prima";
else
cout<<"bukan bilangan prima";
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar: 6 Hasil Program Cek Bil Prima
7)Program pencocokan String
#include <cstdlib>
#include <iostream>
using namespace std;
/* Program Pencocokan String,
Keluaran : Hasil string yang dicocokkan*/
void FungsiGagal(char P[], int F[],int m){
int i,j;
F[0]=0; // inisialisasi awal!
j=0; i=1;
while(i<m){
/* jika i kurang dari panjang pola*/
if(P[i]==P[j]){
F[i]=j+1;
i++;
j++;
}else if(j>0){
j=F[j-1];
}else {
F[i]=0;
i++; } } }
int StringMatch(char T[], char P[]){
int i,j,F[100];
int m=strlen(P);
int n=strlen(T);
FungsiGagal(P,F,m);
i=0; j=0;
while(i<n){
if(T[i]==P[j]){
if(j==m-1)
return i-j;
else{
i++;
j++; }
}else if(j>0){
j=F[j-1];
}else{
i++; } }
return -1; } // tidak ditemukan
int main(int argc, char *argv[])
{
cout<<"\tProgram String Matching 28 Maret 2012\n "<<endl;
char T[1000],P[100];
cout<<"teks : ";
while(gets(T)){
cout<<"pola : ";
gets(P);
int pos=StringMatch(T,P);
if(pos!=-1)
cout<<"Ditemukan pada index ke- "<<pos<<endl;
else
cout<<"tidak ketemu"<<endl; }
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar: 7 Pencocokan String
8)Program Merge Sort
#include <cstdlib>
#include <iostream>
using namespace std;
typedef int larik [10];//tipe data untuk merge
void baca_data(int A[], int n){ /*proses input dan baca data*/
for (int i = 0; i < n; i++){
cout <<"Data ke - " << i + 1 <<" : ";
cin >> A[i];}}
void cetak_data(int A[], int n){ /*cetak data*/
for (int i = 0; i < n; i++){
cout << A[i] <<" ";}}
void tukar_data(int *a, int *b){ /*tukar data*/
int temp;
temp = *a;
*a = *b;
*b = temp;}
void minimum(int A[], int dari, int n, int *tempat){
int i, min;
min = A[dari];
*tempat = dari;
for (i = dari + 1; i < n; i++)
if (A[i] < min){
min = A[i];
*tempat = i;}}
void merge(larik a, int kiri, int tengah, int kanan){
int bagkir, postemp, bykel, i;
larik temp;
bagkir = tengah -1;
postemp = kiri;
bykel = kanan - kiri + 1;
while ((kiri <= bagkir) && (tengah <= kanan))
if ((a[kiri] <= a[tengah])){
temp[postemp] = a[kiri];
postemp = postemp + 1;
kiri = kiri + 1;}
else {
temp[postemp] = a[tengah];
postemp = postemp + 1;
tengah = tengah + 1;}
while ((kiri <= bagkir)){
temp[postemp] = a[kiri];
postemp = postemp + 1;
kiri = kiri + 1;}
while ((tengah <= kanan)){
temp[postemp] = a[tengah];
postemp = postemp + 1;
tengah = tengah + 1;}
for (i = 1; i <= bykel; i++){
a[kanan] = temp[kanan];
kanan = kanan -1;}}
void merge_sort(larik A, int kiri, int kanan){
int tengah;
if (kiri < kanan){
tengah = (kiri + kanan) / 2;
merge_sort(A, kiri, tengah);
merge_sort(A, tengah + 1, kanan);
merge(A, kiri, tengah + 1, kanan);}}
int main(int argc, char *argv[]){
int data[10], n;
cout <<"\tMERGE SORT April 2012\n";
cout <<"\nBanyak data : "; cin >> n;
baca_data(data, n);
cout <<"\nData yang anda masukan : ";
cetak_data(data, n);
merge_sort(data, 0, n - 1);
cout <<"\n\nSetelah urutkan : ";
cetak_data(data, n);
system("PAUSE");
return EXIT_SUCCESS; }
Gambar:8 Marge Sort
9)Program Backtracking N Queen
#include <cstdlib>
#include <iostream>
#define N 5
using namespace std;
/** ukuran papan catur */
void cetak(int letak[N][N]){
int i,j;
cout<<"SUSUNAN PELETAKAN\n\t";
for(i=0; i<N; ++i){
for(j=0; j<N; ++j)
cout<<letak[i][j]<<" ";
cout<<endl<<"\t";
}cout<<"\n";
}
/** return true jika letak Ratu di [baris][kolom] diterima, jika tidak return false */
bool accept(int letak[N][N], int baris, int kolom){
int i,j;
for (i=0; i<N; ++i){ /* check kolom */
if (letak[baris][i])
return false;}
for (i=0; i<N; ++i){ /* check baris */
if (letak[i][kolom])
return false;}
for (i=baris, j=kolom; i>=0 && j>=0; i--, j--){ /* cek diagonal kiri keatas */
if (letak[i][j])
return false;}
for (i=baris, j=kolom; i<N && j>=0; ++i, --j){ /* cek diagonal kiri kebawah */
if (letak[i][j])
return false;}
for (i=baris, j=kolom; i>=0 && j<N; i--, ++j){ /* cek diagonal kanan keatas */
if (letak[i][j])
return false;}
for (i=baris, j=kolom; i<N && j<N; ++i, ++j){ /* cek diagonal kanan kebawah */
if (letak[i][j])
return false;}
return true;} /* return true jika semua test lewat */
void solve_letak(int letak[N][N], int baris){
int i;
if (baris == N){ /* jika semua ratu sudah pada posisi yg tepat */
cetak(letak);
return;}
/* letakkan ratu pada baris yang ada and cek jika letak diterima, Lanjutkan proses sampai semua posisi di temukan*/
for(i=0; i<N; ++i){
if(accept(letak, baris, i)){
letak[baris][i] = 1;
solve_letak(letak, baris+1);}
letak[baris][i] = 0;}
}
int main(int argc, char *argv[])
{
int letak[N][N] = {0};
cout<<"\tProgram Bactracking N Queen 27 MEI 2012\n\n";
solve_letak (letak, 0);
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar: 9 Program Backtracking N Queen
10)Program Titik Terdekat
#include <cstdlib>
#include <iostream>
#include <math.h>
#define SIZE 100
using namespace std;
class Point{
public :
int x, y; };
void closestPair(Point *p, int n, Point &p1, Point &p2 ){
float d, dmin;
int i, j;
dmin = 9999;
for ( i=1; i<n; i++ ) {
for ( j=i+1; j<=n; j++ ) {
d = sqrt( pow(( p[i].x - p[j].x ),2) + pow(( p[i].y - p[j].y ),2) );
if ( d < dmin ) {
dmin = d;
p1 = p[i];
p2 = p[j]; } } } }
int main(int argc, char *argv[]){
int n;
Point p[SIZE];
Point p1,p2;
cout<<"\tProgram Titik terdekat 05 april 2012\n";
cout<<"Masukkan banyak titik : "; cin>>n;
for(int i=1;i<=n;i++){
cout<<"x"<<i<<" : "; cin>>p[i].x;
cout<<"y"<<i<<" : "; cin>>p[i].y;
cout<<endl;}
closestPair(p,n, p1, p2);
cout<<"titik terdekat "<<p1.x<<","<<p1.y<<" dan ";
cout<<p2.x<<","<<p2.y;
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar: 10 Program Mencari Titik terdekat
11)Program Penjadwalan Antrian
#include <cstdlib>
#include <iostream>
#include<conio.h>
using namespace std;
void input(float bil[], int n){
cout<<endl;
for(int c=1;c<=n;c++){
cout<<"Waktu"<<c<<" : ";cin>>bil[c]; } }
void urut(float bil[], int n){
for(int c=1;c<=n;c++){
for(int a=(n);a>=0;a--){
if(bil[a]<bil[a-1]){
int temp;
temp=bil[a];
bil[a]=bil[a-1];
bil[a-1]=temp; } }
}
}
void cetak(float bil[], int n){
cout<<"\nUrutan waktu Dari Tercepat"<<endl;
cout<<"------------------"<<endl;
for(int c=1;c<=n;c++){
cout<<bil[c]<<" ";}
getch();}
int main(int argc, char *argv[]){
int n;
float bil[100];
cout<<"\tProgram penjadwalan Antrian Tercepat 05 April 2012\n\n";
cout<<"banyak pelanggan: ";cin>>n;
input(bil,n);
urut(bil,n);
cetak(bil,n);
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar : 11 Penjadwalan Antrian
12)Program Knapsack Fragtional
#include<iostream.h>
#include <iostream>
using namespace std;
int w[10],p[10],p_no[10],n,m;
float pwr[10];
template <class T>
void swap(T *a,int i,int j){
T temp;
temp=a[j];
a[j]=a[i];
a[i]=temp;
return;}
void display(){
cout<<"\n\n\t======Knapsack=======\n";
cout<<"\nBanyak Produk :\t"<<n<<"\nKapasitas:\t"<<m<<"\n";
cout<<"\nSl.No\t\tBerat\t\tProfit\t\tDensity(P/w)\n";
for(int i=0;i<n;i++)
cout<<p_no[i]<<"\t\t"<<w[i]<<"\t\t"<<p[i]<<"\t\t"<<pwr[i]<<"\n";
}
void knapsack(){
int u,i;
u=m;
float x[10];
for(i=0;i<n;i++)
x[i]=0;
for(i=0;i<n;i++){
if(w[i]>u)
break;
else{
x[i]=1;
u=u-w[i];}
}
if(i<=n)
x[i]=(float)u/w[i];
for(i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(p_no[i]>p_no[j]) {
swap(p_no,i,j);
swap(x,i,j);
swap(p,i,j);
}
}
}
float opt_solution=0;
cout<<"\nBarang Yang dibawa : \n\n";
cout<<"(";
for(i=0;i<n;i++)
cout<<p_no[i]<<" ";
cout<<")";
cout<<"\t\t(";
for(i=0;i<n;i++)
{
cout<<x[i]<<" ";
opt_solution=opt_solution+(p[i]*x[i]);
}
cout<<")";
cout<<"\n\nSolusi Optimal : \t"<<opt_solution;
}
int main(int argc, char *argv[]){
int i;
cout<<"\tProgram Knapsack Fragtional 3 Mei 2012\n\n";
cout<<"\nKapasitas Tas : ";
cin>>m;
cout<<"Banyak Barang : ";
cin>>n;
cout<<"\nKeterangan ke- "<<n<<" barang\n\n";
for(i=0;i<n;i++)
{
p_no[i]=i+1;
cout<<"\nBarang "<<i+1<<"\nBerat:\t";
cin>>w[i];
cout<<"Profit:\t";
cin>>p[i];
pwr[i]=(float)p[i]/w[i];
}
display();
for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(pwr[i]<pwr[j])
{
swap(p_no,i,j);
swap(w,i,j);
swap(p,i,j);
swap(pwr,i,j);
}
}
}
display();
knapsack();
cout<<"\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar: 12 Program Knapsack Fragtional
13)Program Perkalian Matriks
#include<iostream.h>
#include <iostream>
#include <conio.h>
using namespace std;
void baca(int mat[10][10], int baris, int kolom){
int i,j;
for (i=0; i<baris; i++)
for (j=0; j<kolom; j++){
cout<<"Data ["<<i+1<<","<<j+1<<"] : ";
cin>>mat[i][j];}}
void kali(const int M1[10][10], const int M2[10][10], int baris, int kolom, int barkol, int hasil[10][10]){
int i, j, k;
for (i=0; i<baris; i++)
for (j=0; j<kolom; j++){
hasil[i][j]=0;
for (k=0; k<barkol; k++)
hasil[i][j]=hasil[i][j] +M1[i][k] *M2[k][j];} }
void cetak(const int A[10][10], int baris, int kolom){
int i,j;
for (i=0; i<baris; i++){
for (j=0; j<kolom; j++)
cout<<A[i][j]<<" ";
cout<<endl;}
}
int main(int argc, char *argv[]){
int a,b,c;
int M1[10][10], M2[10][10];
int hasil[10][10];
cout<<"\tProgram Peralian Matriks 19 Maret 2012\n\n";
cout<<"banyak baris matriks 1 : "; cin>>a;
cout<<"banyak baris matriks 2 : "; cin>>b;
cout<<"banyak kolom : "; cin>>c;
cout<<"\nMatriks pertama \n";
baca(M1, a, c); cout<<endl;
cetak(M1, a, c);
cout<<"\nMatriks Kedua \n";
baca(M2, c, b); cout<<endl;
cetak(M2, c, b);
kali(M1, M2, a, b, c, hasil);
cout<<"\nHasil Perkalian \n";
cetak(hasil , a, b);
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar: 13 Perkalian Matriks
14) Program Algoritma Dijkstra
#include <cstdlib>
#include <iostream>
#define max 20
#define infinity 9999
using namespace std;
class dijkstra{
private:
int n,graph[max][max],colour[max],start,distance[max],predecessor[max];
enum {green,yellow,red};
public:
void read_graph();
void initialize();
int select_min_distance_lable();
void update(int);
void output();
void function();
};
void dijkstra::read_graph(){
cout<<"masukkan jumlah node = ";
cin>>n;
cout<<"masukkan nilai matrik untuk graf ::\n";
int i,j;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
cout<<"["<<i<<"],["<<j<<"]=";
cin>>graph[i][j];}}
for(i=1;i<=n;i++){
colour[i]=green;}
cout<<"masukkan vertex mulai :: ";
cin>>start;
}
void dijkstra::initialize(){
for(int i=1;i<=n;i++){
if(i==start){
distance[i]=0;}
else{distance[i]=infinity;}
}
for(int j=1;j<=n;j++){
if(graph[start][j]!=0){
predecessor[j]=start;}
else{predecessor[j]=0;}
}
}
int dijkstra::select_min_distance_lable(){
int min=infinity;
int p=0;
for(int i=1;i<=n;i++){
if(colour[i]==green){
if(min>=distance[i]){
min=distance[i];
p=i;
}
}
}
return p;
}
void dijkstra::update(int p){
cout<<"\nupdate jarak = \n";
for(int i=1;i<=n;i++){
if(colour[i]==green){
if(graph[p][i]!=0){
if(distance[i]>graph[p][i]+distance[p]){
distance[i]=graph[p][i]+distance[p];
predecessor[i]=p;
}
}
}
cout<<distance[i]<<'\t';
}
}
void dijkstra::output()
{
cout<<"****** The final paths and the distacnes are ******\n\n";
for(int i=1;i<=n;i++)
{
if(predecessor[i]==0 && i!=start)
{
cout<<"path does not exists between "<<i<<" and the start vertex "
<<start<<endl;
exit(1);
}
cout<<"path for node "<<i<<" is ::\n";
int j=i;
int array[max];
int l=0;
while(predecessor[j]!=0)
{
array[++l]=predecessor[j];
j=predecessor[j];
}
for(int k=l;k>=1;k--)
cout<<array[k]<<"->";
cout<<i<<endl;
cout<<"distance is "<<distance[i]<<endl<<endl<<endl;
}
}
void dijkstra::function()
{
cout<<"\n**********************************************************************\n";
cout<<"*This program is to implement dijkstra's algorithm using colour codes* \n";
cout<<"**********************************************************************\n\n";
read_graph();
initialize(); //repeate until all nodes become red
int flag=0;
int i;
cout<<"\n\n******** The working of the algorithm is **********\n\n";
for(i=1;i<=n;i++)
if(colour[i]!=red)
flag=1;
cout<<"The initial distances are ::\n";
for(i=1;i<=n;i++)
cout<<distance[i]<<'\t';
cout<<endl;
while(flag) {
int p=select_min_distance_lable();
cout<<"\nThe min distance lable that is coloured yellow is "<<p;
colour[p]=yellow;
update(p);
cout<<"\nnode ="<<p<<" is coloured red "<<endl;
colour[p]=red;
flag=0;
for(i=1;i<=n;i++)
if(colour[i]!=red)
flag=1;
cout<<endl<<endl<<endl;
}
output();
}
int main(int argc, char *argv[])
{
dijkstra d;
d.function();
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar:14 Algoritma Djaktra
15)Program Binary Search
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
int X,i,j,k,p;
int L[10]={12,14,15,17,23,25,45,67,68,70}; /*Deklarasi isi array*/
/*Program Binary Search 01 juni 2012*/
/*Variabel p digunakan untuk kode, apakah menaik atau menurun*/
/*Jika p=0, maka data terurut menaik*/
/*Jika p=1, maka data terurut menurun*/
if (L[0]<L[9]){
printf("\tProgram Binary Search \n");
printf("\tData terurut menarik \n");
p=0;}
else{
printf("Data terurut menurun \n");
p=1;}
/*input data X yang akan dicari*/
printf("Data yang dicari="); scanf("%d",&X);
/*Penentuan indeks awal dan akhir semula*/
i=0;
j=10;
/*proses perulangan untuk menentukan nilai tengah k */
do{
k=(i+j)/2;
if (p==0){ //jika data terurut menaik
if (L[k]==X){
printf ("Data ditemukan di elemen %d",k);
getch();
return 0; //langsung keluar program
}
else if(L[k]==X){
i=k;}
else{
j=k;
}}}
while(k!=0); //sampai nilai k=0, iterasi berakhir
printf("data tidak ada!!");
system("PAUSE");
return EXIT_SUCCESS;
}
Gambar: 15 Binary Search
16)Program Bubble Sort
#include <cstdlib>
#include <iostream>
using namespace std;
typedef int larik [10];//tipe data untuk merge
void baca_data(int A[], int n){ //proses input dan baca data
for (int i = 0; i < n; i++){
cout <<"Data ke - " << i + 1 <<" : ";
cin >> A[i];} }
void cetak_data(int A[], int n){ //cetak data
for (int i = 0; i < n; i++){
cout << A[i] <<" "; } }
void tukar_data(int *a, int *b){ //tukar data
int temp;
temp = *a;
*a = *b;
*b = temp; }
void minimum(int A[], int dari, int n, int *tempat){
int i, min;
min = A[dari];
*tempat = dari;
for (i = dari + 1; i < n; i++)
if (A[i] < min){
min = A[i];
*tempat = i; } }
void bubble_sort(int x[], int n){ // bubble sort
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++){
if (x[i] > x [j]) tukar_data (&x[i], &x[j]);}
}
int main(int argc, char *argv[]){
int data[10], n;
cout <<"\tProgram BUBBLE SORT 06 Juni 2012";
cout <<"\n\nMasukan data : ";
cin >> n;
baca_data(data, n);
cout <<"Data yang anda masukan : ";
cetak_data(data, n);
cout<<endl;
bubble_sort(data, n);
cout <<"Setelah ditukar (Bubble Sort) : ";
cetak_data(data, n);
cout <<endl;
system("PAUSE");
return EXIT_SUCCESS;}
Gambar:16 Bubble Sort
17)Program Partition Sort
#include <cstdlib>
#include <iostream>
using namespace std;
typedef int larik [20]; //type data for merge
void read(int A[], int n){ //proses input dan read data
for (int i=0; i<n; i++){
cout <<"Data ke - " << i + 1 <<" : ";
cin >> A[i]; } }
void print(int A[], int n){ //print data
for (int i = 0; i < n; i++){
cout << A[i] <<" ";
}}
void partitionSort(int arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
while (i <= j) { /* partition */
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
if (left < j) /* recursion */
partitionSort(arr, left, j);
if (i < right)
partitionSort(arr, i, right);
}
int main(int argc, char *argv[]){
int data[10], n, pilih;
cout <<"\tProgram PARTITION SORT 06 Juni 2012";
cout <<"\n\nBanyak data : ";
cin >> n;
read(data, n);
cout <<"\nData yang anda masukan : ";
print(data, n);
partitionSort(data, 0, n-1);
cout <<"\nSetelah diurutkan (Partition Sort): ";
print(data, n);
system("PAUSE");
return EXIT_SUCCESS;}
Gambar: 17 Partition Sort