Anda di halaman 1dari 6

#include <iostream.h> #include <conio.

h> void void void void void void void void void tukar(int *a, int *b); bubble (int n,int a[]); bubble2 (int n,int a[]); exchange (int n, int a[]); exchange2 (int n, int a[]); selection (int n, int a[]); selection2 (int n, int a[]); insertion (int n,int a[]); insertion2 (int n,int a[]);

void main() { int input,input2,datamasuk; cout << "Sorting Data\n"; for (;;) { Awal : int data [] = {22,10,15,3,8,2}; const int n = 6; Baru : cout << "\nDatanya :" << endl ; for (int k=0; k<n; k++) cout <<" "<< data[k]; //data_awal = data[k]; cout << endl; char lagi; // Proses pemilihan metode sorting cout << "\nPilihan metode sorting :" << endl; cout << "1. Bubble Sort" << endl; cout << "2. Exchange Sort" << endl; cout << "3. Selection Sort" << endl; cout << "4. Insertion Sort" << endl; cout << "\nMasukan pilihan metode sorting = "; cin >> input; cout << "Pilihan anda : "; switch(input) { case 1: cout<<"Bubble Sort"<<endl; break; case 2: cout<<"Exchange Sort"<<endl; break; case 3: cout<<"Selection Sort"<<endl; break; case 4: cout<<"Insertion Sort"<<endl; break; default: { cout<<"Input angka salah, metode sorting tidak ada !!!"< <endl; //goto Salah; }

} // Proses pemilihan pengurutan cout << "\nPilihan pengurutan :" << endl; cout << "1. Ascending" << endl; cout << "2. Descending" << endl; cout << "\nMasukan pilihan Pengurutan = "; cin >> input2; cout << "Pilihan pengurutan anda : "; int ke; switch(input2) { case 1: cout<<"Ascending"<<endl; ke = 1; break; case 2: cout<<"Desending"<<endl; ke = 2; break; default: cout<<"Input angka salah, pilihan pengurutan tidak ada !!!"<<end l; } cout << endl; // proses if (input == 1 && ke==1) bubble (n,data); else if (input == 1 && ke==2) bubble2 (n,data); else if (input == 2 && ke==1) exchange (n,data); else if (input == 2 && ke==2) exchange2 (n,data); else if (input == 3 && ke==1) selection (n,data); else if (input == 3 && ke==2) selection2 (n,data); else if (input == 4 && ke==1) insertion (n,data); else if (input == 4 && ke==2) { insertion2 (n,data); } // Menampilkan hasil cout << "\n\nHasil sorting :\n"; for (int i=0; i<n; i++) cout<<" "<< data[i]; cout << endl; // int hasil_sort; //hasil_sort = data[i]; Salah : cout<<"Teruskan program?"<<"\n"; cout<<"Tekan 'Y' untuk ya, tekan 'T' untuk tidak ="; lagi=getch(); if(lagi == 'y'||lagi == 'Y') { cout<<"\n"<<"\n";

cout<<"Pilihan data:"<<endl; cout<<"1. Data Awal"<<endl; cout<<"2. Data Baru"<<endl; cout<<"Yang dipilih : "; cin>>datamasuk; switch(datamasuk) { case 1: cout<<"data awal"<<endl; goto Awal; case 2: cout<<"data baru"<<endl; goto Baru; default: { cout<<"Input angka salah, pilihan pengurutan tidak ada ! !!"<<endl; goto Salah; } } } else if (lagi=='t'||lagi=='T') cout<<endl<<"selesai"; else { cout<<"Input angka salah, pilihan pengurutan tidak ada ! !!"<<endl; goto Salah; } } } // dibawah ini merupakan kumpulan sub program : void tukar(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } void bubble (int n,int a[]) { for (int i=1; i<n; i++) { for (int j=n-1; j>=i;j--) { if (a[j] <a [j-1]) tukar(&a[j],&a[j-1]); for (int k=0; k<n;k++) cout << " " << a[k]; cout << endl; } } } void bubble2 (int n,int a[]) { for (int i=1; i<n; i++) {

for (int j=n-1; j>=i;j--) { if (a[j] > a [j-1]) tukar(&a[j],&a[j-1]); for (int k=0; k<n;k++) cout << " " << a[k]; cout << endl; } } } void exchange (int n, int a[]) { for (int i=0; i<=n; i++) { for (int j=i+1; j<n; j++) { if (a[i] > a[j]) tukar (&a[i], &a[j]); for (int k=0; k<n;k++) cout << " " << a[k]; cout << endl; } } } void exchange2 (int n, int a[]) { for (int i=0; i<=n; i++) { for (int j=i+1; j<n; j++) { if (a[i] < a[j]) tukar (&a[i], &a[j]); for (int k=0; k<n;k++) cout << " " << a[k]; cout << endl; } } } void selection (int n, int a[]) { for (int i=0; i<n-1; i++) { int temp=i; for (int j=i+1; j<n; j++) { if (a[j] < a[temp]) temp = j; } if(temp != i) tukar(&a[temp],&a[i]); for (int k=0; k<n;k++) cout << " " << a[k]; cout << endl; } }

void selection2 (int n, int a[]) { for (int i=0; i<n-1; i++) { int temp=i; for (int j=i+1; j<n; j++) { if (a[j] > a[temp]) temp = j; } if(temp != i) tukar(&a[temp],&a[i]); for (int k=0; k<n;k++) cout << " " << a[k]; cout << endl; } } void insertion (int n, int a[]) { int i, j, temp; for (i=1; i<n; i++) { temp=a[i]; j = i - 1; while (a[j] > temp && j>=0) { a[j+1] = a[j]; j--; } a[j+1] = temp; for (int k=0; k<n;k++) cout << " " << a[k]; cout << endl; } } void insertion2 (int n, int a[]) { int i, j, temp; for (i=1; i<n; i++) { temp=a[i]; j = i - 1; while (a[j] < temp && j>=0) { a[j+1] = a[j]; j--; } a[j+1] = temp; for (int k=0; k<n;k++) cout << " " << a[k]; cout << endl; }

Anda mungkin juga menyukai