5 CONTOH PROGRAM SORTING(SELECTION, BUBBLE, INSERTION, QUICK , MERGE) PADA C++
5 CONTOH PROGRAM SORTING(SELECTION, BUBBLE, INSERTION, QUICK , MERGE) PADA C++
PROGRAM 1 : SELECTION SORT
#include <cstdlib>#include <iostream>
#include <conio.h>
using namespace std;
void print(int[], int);
void selection_sort(int [], int);
//selection sort
void selection_sort(int ar[], int size){
int min_ele_loc;
for(int i=0;i<9;++i){
//find minimum element in the unsorted array
//assume it's the first element;
min_ele_loc=i;
//loop through the array to find it
for(int j=i+1;j<10;++j){
if(ar[j]<ar[min_ele_loc]){ //found new minimum position
min_ele_loc=j;
}
}
//swap the values
swap(ar[i], ar[min_ele_loc]);
}
}
//print the array
void print(int temp_ar[], int size){
for(int i=0;i<size;++i){
cout<<temp_ar[i]<<" ";
}
cout<<endl;
}
//driver function
int main(){
int min_ele_loc;
int ar[]={10,2,45,18,16,30,29,1,1,100};
cout<<"Array Initially : ";
print(ar,10);
selection_sort(ar,10);
cout<<"Array after selection sort : ";
print (ar,10);
//return 0;
getch();
}
PROGRAM 2 : BUBBLE SORT
#include <cstdlib>#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main(){
int nilai[8];
int temp;
cout<<"Data sebelum diurutkan"<<endl;
for(int ctr=0;ctr<8;ctr++){
cout<<"Masukkan Data ke "<<ctr+1<<" : ";
cin>>nilai[ctr];
}
cout<<endl;
cout<<endl;
for(int i=0;i<8;i++){
for(int ii=0;ii<8;ii++){
if(nilai[ii]>nilai[ii+1]){
temp=nilai[ii];
nilai[ii]=nilai[ii+1];
nilai[ii+1]=temp;
}
}
}
cout<<"Data setelah diurutkan"<<endl;
for(int iii=0;iii<8;iii++){
cout<<setw(3)<<nilai[iii]<<" ";
}
getch();
}
PROGRAM 3 : INSERTION SORT
#include <cstdlib>#include <iostream>
#include <conio.h>
using namespace std;
int data[10], data2[10];
int Max,i,j;
void straighinsertsort(){
int i,j,x;
for(i=1;i<=Max;i++){
x=data[i];
j=i-1;
while(x<data[j]){
data[j+1]=data[j]; //data [j] dimasukkan ke data [j+1]
j--;
}
data[j+1]=x; //x dimasukkan pada data[j+1]
}
}
int main(){
cout<<"PROGRAM INSERTION SORT \n"<<endl;
cout<<"Masukkan Jumlah Data : ";
cin>>Max;
for(int i=1;i<=Max;i++){
cout<<"Masukkan data ke "<<i<<" : ";
cin>>data[i];
data2[i]=data[i];
}
straighinsertsort();
cout<<"\n data setelah di sort : ";
for(i=1; i<=Max; i++){
cout<<" "<<data[i];
}
getch();
}
PROGRAM 4 : QUICK SORT
#include <iostream>#include <conio.h>
using namespace std;
void tampilkan_larik(int data[], int n)
{
int i;
for (i = 1; i <= n; i++)
cout << data[i] << " ";
cout << "\n";
}
int partisi(int data[], int awal, int akhir)
{
int x, i, j, simpan;
//x=data[awal];
i = awal;
j = akhir;
while (1)
{
while (data[i]<data[awal])
i = i + 1;
while (data[j]>data[awal])
j = j - 1;
if (i<j)
{
//tukarkan data
simpan = data[i];
data[i] = data[j];
data[j] = simpan;
}
else
return j;
}
}
void quick_sort(int data[], int awal, int akhir)
{
int q;
if (awal<akhir)
{
q = partisi(data, awal, akhir);
quick_sort(data, awal, q);
quick_sort(data, q + 1, akhir);
}
}
int main()
{
int i, j, n, data[100];
cout << "Masukkan Jumlah element Array :";cin>>n;
for (i = 1; i <= n; i++)
{
cout << "Masukkan Elemen ke-" << i << " :"<<endl;
cin >> data[i];
}
cout << "Data sebelum diurut: " << endl;
for (j = 1; j <= n; j++)
{
cout << data[j] << " ";
}
quick_sort(data, 1, n);
//hasil pengurutan
cout << endl;
cout << endl;
cout << "Data Setelah Di Urut Dengan QucikSort:\n";
tampilkan_larik(data, n);
getch();
}
PROGRAM 5 : MERGE SORT
#include <iostream>#include<conio.h>
using namespace std;
void merge(int low, int mid, int up);
void mergeSort(int low, int up);
int a[50];
int main()
{
int jumlahBil, i;
cout << "Masukkan Jumlah element Array : " << endl;
cin >> jumlahBil;
for (int i = 0; i<jumlahBil; i++)
{
cout << "Masukkan Elemen ke-" << i + 1 << " : " << endl;
cin >> a[i];
}
{
cout << "Data Setelah Di Urut Dengan Merge Sorting : "<<endl;
}
mergeSort(0, jumlahBil);
for (i = 1; i <= jumlahBil; i++)
cout << a[i] << " ";
cout << endl;
getch();
}
void merge(int low, int mid, int up)
{
int h, i, j, k;
int b[50];
h = low;
i = low;
j = mid + 1;
while ((h <= mid) && (j <= up))
{
if (a[h] < a[j])
{
b[i] = a[h];
h++;
}
else
{
b[i] = a[j];
j++;
}
i++;
}
if (h>mid)
{
for (k = j; k <= up; k++) {
b[i] = a[k];
i++;
}
}
else
{
for (k = h; k <= mid; k++)
{
b[i] = a[k];
i++;
}
}
for (k = low; k <= up; k++) a[k] = b[k];
}
void mergeSort(int low, int up)
{
int mid;
if (low<up)
{
mid = (low + up) / 2;
mergeSort(low, mid);
mergeSort(mid + 1, up);
merge(low, mid, up);
}
}