0% found this document useful (0 votes)
27 views

Script

The document describes a C++ program for sorting algorithms. It includes functions for inputting data, displaying data, and implementing different sorting algorithms like bubble sort, exchange sort, insertion sort and selection sort. The main function allows the user to select a sorting algorithm and calls the corresponding functions to sort and display the data. Additional functions are included to sort data within a given range and display only even numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Script

The document describes a C++ program for sorting algorithms. It includes functions for inputting data, displaying data, and implementing different sorting algorithms like bubble sort, exchange sort, insertion sort and selection sort. The main function allows the user to select a sorting algorithm and calls the corresponding functions to sort and display the data. Additional functions are included to sort data within a given range and display only even numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

tugas 1

#include <iostream>
#include <conio.h>
using namespace std;
int data[10], data2[10];
int n;
void tukar(int a,int b)
{
int t;
t=data[b];
data[b] = data[a];
data[a] = t;
}

void input()
{
cout<<"Masukkan jumlah data = "; cin>>n;
cout<<"--------------------------------"<<endl;
for(int i=0; i<n; i++)
{
cout<<"Masukkan data ke-"<<(i+1)<<" = ";
cin>>data[i]; data2[i] = data[i];
}
cout<<endl;
}

void tampil()
{
for(int i=0;i<n;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}

void bubble_sort()
{
for(int i=1;i<n;i++)//increment
{
for(int j=n-1;j>=i;j--) //decrement
{
if(data[j]<data[j-1]) tukar (j,j-1);
}
tampil();
}
cout<<endl;
}

void exchange_sort()
{
for (int i=0; i<n-1; i++) {
for(int j = (i+1); j<n; j++)
{
if (data [i] > data[j]) tukar(i,j);
}
}
cout<<"exchange sort selesai!"<<endl;
tampil();
}

void insertion_sort()
{
int temp,i,j;
for(i=1;i<n;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!"<<endl;
tampil();
}

void selection_sort()
{
int pos,i,j;
for(i=0;i<n-1;i++)
{
pos = i;
for(j = i+1;j<n;j++)
{
if(data[j] < data[pos]) pos = j;
}
if(pos != i) tukar(pos,i);
}
cout<<"selection sort selesai!"<<endl;
tampil();
}

int main()
{
int pilih ;
char x;
kembali :
cout<<"|=============Selamat Datang di Program Sorting
sederhana===========| \n\n";
cout<<"|****************Silahkan Memilih Menu Teknik
Sorting***************|\n\n" ;
cout<<" 1. Buble \n";
cout<<" 2. Exchange \n";
cout<<" 3. Insertion \n";
cout<<" 4. Selection \n\n ";
cout<<" Masukkan pilihan anda : ";
cin>>pilih;
switch (pilih)
{
case 1 :
{
program :
cout<<endl;
cout<<"*------------------------------*"<<endl;
cout<<"* Selamat datang di aplikasi *"<<endl;
cout<<"* Bubble Sort *"<<endl;
cout<<"*------------------------------*"<<endl;
input();
cout<<"Proses Bubble Sort. . . . . . ."<<endl;
cout<<"-------------------------------"<<endl;
tampil();
bubble_sort();
cout<<"-------------------------------"<<endl;
cout<<" TERIMA KASIH "<<endl;
cout<<"-------------------------------"<<endl;
cout<<"Apakah anda ingin kembali ke menu ? (y/t) : ";
cin>>x;
if( x=='y' || x=='Y')
{
goto kembali;
}
else
goto program;
cout<<endl;
break;
}
case 2 :
{
program_a :
cout<<endl;
cout<<"*------------------------------*"<<endl;
cout<<"* Selamat datang di aplikasi *"<<endl;
cout<<"* exchange Sort *"<<endl;
cout<<"*------------------------------*"<<endl;
input();
exchange_sort();
cout<<"Apakah anda ingin kembali ke menu ? (y/t): \n\n";
cin>>x;
if( x=='y' || x=='Y')
{
goto kembali;
}
else
goto program_a;
break;
}
case 4 :
{
program_b :
cout<<endl;
cout<<"*------------------------------*"<<endl;
cout<<"* Selamat datang di aplikasi *"<<endl;
cout<<"* selection Sort *"<<endl;
cout<<"*------------------------------*"<<endl;
input();
selection_sort();
cout<<"Apakah anda ingin kembali ke menu ? (y/t) \n\n: ";
cin>>x;
if( x=='y' || x=='Y')
{
goto kembali;
}
else
goto program_b;
cout<<endl;
break;
}
case 3 :
{
program_c :
cout<<endl;
cout<<"*------------------------------*"<<endl;
cout<<"* Selamat datang di aplikasi *"<<endl;
cout<<"* insertion Sort *"<<endl;
cout<<"*------------------------------*"<<endl;
input();
insertion_sort();
cout<<"Apakah anda ingin kembali ke menu ? (y/t) \n\n: ";
cin>>x;
if( x=='y' || x=='Y')
{
goto kembali;
}
else
goto program_c;
cout<<endl;
break;
}

}
}

tugas 2

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

using namespace std;

int main()
{
int data[100];
int i, j, tmp, n, o, p, a;
cout<<" Program Mengurutkan Data"<<endl;
cout<<" Dengan Metode Bubble Sort dan Dilengkapi Range
Tertentu"<<endl;
cout<<" Masukkan berapa bilangan yang ingin di sort : ";
cin>>n;
for(i=0; i<n; i++)
{
cout<<"Masukkan bilangan ke "<<(i+1)<<" : ";
cin>>data[i];
}
cout<<"Data sebelum diurutkan : "<<endl;
for(i=0; i<n; i++)
{
cout<<data[i]<<" ";
}
cout<<endl;

cout<<"masukkan range : "<<endl;


cout<<"masukkan x : ";
cin>>o;
cout<<"masukan y : ";
cin>>p;

for(i=o-2; i<=p; i++)


{
for(j=i; j<=p; j++)
{
if(data[i]>data[j])
{
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
}
}
cout<<"Data setelah diurutkan : "<<endl;
for(i=o-1; i<=p; i++)
{
cout<<data[i]<<" ";
}
getch();
}

tugas 3 belom
tugas rumah 1

#include <iostream>

using namespace std;

int data[10], data2[10];

int n;

void tukar(int a,int b)

int t;

t=data[b];

data[b] = data[a];

data[a] = t;

void input()

cout<<"Masukkan jumlah data = "; cin>>n;

cout<<"--------------------------------"<<endl;

for(int i=0; i<n; i++)

cout<<"Masukkan data ke-"<<(i+1)<<" = ";

cin>>data[i]; data2[i] = data[i];

cout<<endl;

}
void tampil()

for(int i=0;i<n;i++)

cout<<data[i]<<" ";

cout<<endl;

void bubble_sort()

for(int i=1;i<n;i++)//increment

for(int j=n-1;j>=i;j--) //decrement

if(data[j]<data[j-1]) tukar (j,j-1);

tampil();

cout<<endl;

void genap ()

int i, bil;

bil=data[i]%2==0;
cout << "Bilangan genap : " << endl;

for(int i=0;i<=n;i++){

if(data[i]%2==0 && data[i]!=0)

cout<<data[i]<<" ";

int main()

cout<<" Selamat datang dalam program sorting"<<endl;

input();

bubble_sort();

genap();

You might also like