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

Berikut Ini Contoh Buble Sort, Beserta Dengan Metode Pengurutan (Sorting) Yang Ada

This document contains code for implementing various sorting algorithms in C++ including bubble sort, exchange sort, selection sort, insertion sort, and quicksort. It defines functions for each sorting algorithm, takes user input for data, sorts the data using the selected algorithm, and outputs the results. The code allows the user to input data, select a sorting algorithm, display the sorted data, and re-shuffle the data for multiple trials.

Uploaded by

Cynthia Christy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Berikut Ini Contoh Buble Sort, Beserta Dengan Metode Pengurutan (Sorting) Yang Ada

This document contains code for implementing various sorting algorithms in C++ including bubble sort, exchange sort, selection sort, insertion sort, and quicksort. It defines functions for each sorting algorithm, takes user input for data, sorts the data using the selected algorithm, and outputs the results. The code allows the user to input data, select a sorting algorithm, display the sorted data, and re-shuffle the data for multiple trials.

Uploaded by

Cynthia Christy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Berikut ini contoh Buble Sort,beserta dengan metode pengurutan (sorting)

yang ada:
#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 buble_sort()
{
for(int i=1;i
{
for(int j=n-1;j>=i;j--)
{
if(data [j]
}
}
cout<<"=================="<
cout<<"Bubble sort selesai..!!!"<
cout<<"==================="<
}
void exchange_sort()
{
for (int i=0; i
{
for(int j=(i+1); j
{
if (data [i]>data[j]) tukar(i,j);
}
}
cout<<"==================="<
cout<<"Exchange sort selesai..!!"<
cout<<"==================="<

}
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<<"==================="<
cout<<"Selection sort selesai"<
cout<<"==================="<
}
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<<"==================="<
cout<<"insertion sort selesai...!!!"<
cout<<"==================="<
}
void QuickSort (int L,int R)
{

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
if (L
if (i
}
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<<data[i]<<" ";=""
}
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 Complete...!!"<
cout<<"============================="<
cout<<"1. Input Data"<
cout<<"2. Buble Sort"<
cout<<"3. Exchange Sort"<
cout<<"4. Selection Sort"<
cout<<"5. Insertion Sort"<
cout<<"6. Quick Sort"<
cout<<"7. Tampilkan Data"<
cout<<"8. Acak Lagi"<
cout<<"9. Exit"<
cout<<"Masukkan Pilihan Anda : ";
cin>>pil;
switch(pil)
{
case 1:input(); break;
case 2:buble_sort(); break;
case 3:exchange_sort(); break;
case 4:selection_sort(); break;
case 5:insertion_sort(); break;
case 6:QuickSort(0,n-1); break;
case 7:tampil(); break;
case 8:AcakLagi(); break;
}
getch();
}while(pil!=9);
}
</data[i]<<">

You might also like