Membuat Program Untuk Menggabungkan Program Sorting Data
Selamat malam gan .
Bintara kali ini bagi bagi script lagi nih . script yang satu ini adalah untuk menggabungkan program program sorting data yang sudah kita bahas sebelumnya yakni
Buble Sort ,
Selection Sort,
Shell Sort,
Quick Sort, serta
Radix Sort . langsung ae cekidot . .
#include <iostream>
#include <windows.h>
#include <algorith.h>
#include <iomanip.h>
using namespace std;
void bubleSort(int Array[], char urut)
{
int temp;
cout<<" Data Sebelum diurutkan : \n";
for(int d=0; d<8; d++){
cout<<setw(3)<<Array[d];
}
cout<<"\n\n";
for(int i=0; i<8; i++){
if(urut == 'N' || urut =='n'){
for(int ii=1; ii<=8;ii++){
if(Array[ii] > Array[ii+1]){
temp = Array[ii];
Array[ii] = Array[ii+1];
Array[ii+1] = temp;
}
}
}
if(urut == 'T' || urut =='t'){
for(int ii=0; ii<8;ii++){
if(Array[ii+1] >= Array[ii]){
temp = Array[ii];
Array[ii] = Array[ii+1];
Array[ii+1] = temp;
}
}
}
}
cout<<" Data setelah diurutkan : \n";
for(int iii = 1; iii<=8; iii++)
cout<<setw(3)<<Array[iii];
}
void selectionSort(int Array[], char urut){
int i, j, kecil, temp;
cout<<" Data Sebelum diurutkan : \n";
for(int d=0; d<8; d++){
cout<<setw(3)<<Array[d];
}
for(i=0; i<8; i++)
{
kecil=i;
if(urut == 'N' || urut =='n'){
for(j=i; j<=8; j++)
{
if(Array[j] < Array[kecil])
{
kecil = j;
}
}
}
if(urut == 'T' || urut =='t'){
for(j=i; j<8; j++)
{
if(Array[j] >= Array[kecil])
{
kecil = j;
}
}
}
swap(Array[kecil], Array[i]);
}
cout<<"\n Data setelah diurutkan : \n";
for(int iii = 0; iii<8; iii++)
cout<<setw(3)<<Array[iii];
}
void shellSort(int Array[], char urut){
int i, j,d, flag, tmp, length = 8;
cout<<" Data Sebelum diurutkan : \n";
for(int d=0; d<8; d++){
cout<<setw(3)<<Array[d];
}
d = length;
flag = 1;
while(flag || (d > 1)){
flag = 0;
d = (d + 1)/2;
for(i = 0; i < (length - d);i++){
if(urut == 'N' || urut =='n'){
if(Array[i] > Array[i+d])
{
tmp = Array[i+d];
Array[i + d] = Array[i];
Array[i] = tmp;
flag = i;
}
}
}
for(i = 0; i <=((length-d)-1);i++){
if(urut == 'T' || urut =='t'){
if(Array[i+d] > Array[i])
{
tmp = Array[i+d];
Array[i + d] = Array[i];
Array[i] = tmp;
flag = i;
}
}
}
}
cout<<"\n Data Setelah diurutkan : \n";
for (i = 0; i < 8; i++)
{
cout<<setw(3)<<Array[i];
}
}
int main(){
system("title compiled : SORTING DATA");
int Array[8];
int length = 8;
int i, j, d;
int tmp, flag;
int pilihan;
char urut;
cout<<" ============================================ "<<endl;
cout<<" SORTING DATA "<<endl;
cout<<" ============================================ "<<endl;
cout<<" [1] Buble Sort "<<endl;
cout<<" [2] Selection Sort "<<endl;
cout<<" [3] Shell Sort "<<endl;
cout<<" [4] Exchange Sort "<<endl;
cout<<" [5] Quick Sort "<<endl;
cout<<" [6] Radix Sort "<<endl<<endl;
cout<<" Pilih metode sorting : "; cin>>pilihan;
cout<<" Pilih pengurutan Naik (N) / Turun (T) : "; cin>>urut;
cout<<" ============================================ "<<endl;
for (i = 0; i < length ; i++)
{
cout << " Masukkan angka ke "<<i+1<<" : ";
cin >> Array[i];
}
if(pilihan == 1){
bubleSort(Array, urut);
}
else if(pilihan == 2){
selectionSort(Array, urut);
}
else if(pilihan == 3){
shellSort(Array, urut);
}
}
Untuk Hasil e , seperti biasa , silakan compile sendiri .
Sekian dulu tulisan kali ini, semoga bermanfaat, dan salam sukses
Bintara
Related Post: