Program Exchange Sort Dalam Bahasa C++
Setelah sorting dengan metode Quick Sort dan Insertion Sort, kali ini adalah program sorting menggunakan metode "Exchange Sort". Berikut source code nya.
#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 exchange_sort()
{
for (int i=0; i<=n; i++)
{
for(int j = (i+1); j<=n; j++)
{
if (data [i] > data[j]) tukar(i,j);
}
}
}
int main ()
{
cout<<"PROGRAM EXCHANGE SORT"<<endl<<endl;
//Input Data
cout<<"Masukkan Jumlah Data : ";
cin>>n;
cout<<endl;
for(int i=1;i<=n;i++)
{
cout<<"Masukkan data ke "<<i<<" : ";
cin>>data[i];
data2[i]=data[i];
}
exchange_sort();
cout<<"\n\n";
//tampilkan data
cout<<"Data Setelah di Sort : ";
for(int i=1; i<=n; i++)
{
cout<<" "<<data[i];
}
cout<<"\n\nSorting Selesai";
getch();
}
#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 exchange_sort()
{
for (int i=0; i<=n; i++)
{
for(int j = (i+1); j<=n; j++)
{
if (data [i] > data[j]) tukar(i,j);
}
}
}
int main ()
{
cout<<"PROGRAM EXCHANGE SORT"<<endl<<endl;
//Input Data
cout<<"Masukkan Jumlah Data : ";
cin>>n;
cout<<endl;
for(int i=1;i<=n;i++)
{
cout<<"Masukkan data ke "<<i<<" : ";
cin>>data[i];
data2[i]=data[i];
}
exchange_sort();
cout<<"\n\n";
//tampilkan data
cout<<"Data Setelah di Sort : ";
for(int i=1; i<=n; i++)
{
cout<<" "<<data[i];
}
cout<<"\n\nSorting Selesai";
getch();
}
Tidak ada komentar: