buble sorting
#include <cstdlib>
#include <iostream>
using namespace std;
int baca_data(int A[],int n){
int i;
for(i=0;i<n;i++)
{
cout<<"data ke-"<<i+1<<":";
cin>>A[i];
}
}
int cetak_data(const int A[],int n){
int i;
for(i=0;i<n;i++)
cout << A[i] << "";
cout << "\n";
}
int tukar(int *a,int *b)
{ int temp;
temp= *a;
*a = *b;
*b = temp;
}
int buble_sort(int x[],int n)
{
int i,j;
for(i = 0; i<n-1; i++)
for(j=i+1; j<n;j++)
if(x[i] > x[j]) tukar (&x[i],&x[j]);
}
int main()
{
int data[10],n;
cout << "banyak data:";
cin >> n;
baca_data(data,n);
cetak_data(data,n);
buble_sort(data,n);
cetak_data(data,n);
system("PAUSE");
return EXIT_SUCCESS;
}
insertion sorting
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
class Sorting{
friend istream& operator >> (istream& in,Sorting& a);
friend ostream& operator << (ostream& out,Sorting& a);
public:
void baca_data();
void cetak_data();
void buble_sort();
void insertion_sort();
void selection_sort();
private:
void minimum(int, int,int&);
void tukar(int&,int&);
int data[10],n;
};
istream& operator>>(istream& in,Sorting& a){
cout<<"Banyak data:";
in>>a.n;
for(int i=0;i<a.n;i++){
cout<<"Data ke-"<<i+1<<":";
in>>a.data[i];
}
return in;
}
ostream& operator << (ostream& out,Sorting& a){
for(int i=0;i<a.n;i++)
out<<a.data[i]<<"";
out<<"\n";
return out;
}
void Sorting::tukar(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void Sorting::insertion_sort()
{
int i,j,temp;
cout<<"data pertama sudah ada ditangan kiri,yaitu:"<<data[0]<<"\n";
for(j=1;j<n;j++)
{
temp=data[j];
cout<<"\nData ke-"<<j+1<<"yaitu"<<data[j]<<"diambil dari meja\n";
cout<<"dilakukan penggeseran elemen:\n";
for(i=j-1;(i>=0)&&(data[i]> temp);i--)
{
cout<<"data pada posisi ke-["<<i+1<<"]digeser ke posisi["<<i+2<<"]\n";
data[i+1]=data[i];
}
cout<<"Data baru masuk pada posisi ke-["<<i+2<<"]\n";
data[i+1]=temp;
cout<<"Data saat ini adalah:";
for(int k=0;k<=j;k++)cout<<data[k]<<"";
}
}
int main()
{
Sorting angka;
cin>>angka;
angka.insertion_sort();
cout<<"\nHasil akhir adalah:\n";
cout<<angka;
system("PAUSE");
return EXIT_SUCCESS;
}
selection sorting
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
int baca_data(int A[],int n)
{
int i;
for(i=0;i<n;i++)
{
cout << "Data ke-" <<i+1<<":";
cin >> A[i];
}
}
int cetak_data(const int A[],int n)
{
int i;
for(i=0;i<n;i++)
cout<<""<<A[i];
cout << "\n";
}
int tukar(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int minimum(int A[],int dari,int n,int * tempat)
{
int i,min;
min=A[dari];
*tempat=dari;
for(i=dari+1;i<n;i++)
if(A[i] < min)
{
min=A[i];
*tempat=i;
}
}
int selection_sort(int A[],int n)
{
int i,t;
for(i=0;i<n;i++)
{
minimum(A,i,n,&t);
tukar(&A[i],&A[t]);
}
}
int main(int argc, char *argv[])
{
int data[10],n;
cout<<"banyak data:";
cin>>n;
baca_data(data,n);
cetak_data(data,n);
selection_sort(data,n);
cetak_data(data,n);
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
using namespace std;
int baca_data(int A[],int n){
int i;
for(i=0;i<n;i++)
{
cout<<"data ke-"<<i+1<<":";
cin>>A[i];
}
}
int cetak_data(const int A[],int n){
int i;
for(i=0;i<n;i++)
cout << A[i] << "";
cout << "\n";
}
int tukar(int *a,int *b)
{ int temp;
temp= *a;
*a = *b;
*b = temp;
}
int buble_sort(int x[],int n)
{
int i,j;
for(i = 0; i<n-1; i++)
for(j=i+1; j<n;j++)
if(x[i] > x[j]) tukar (&x[i],&x[j]);
}
int main()
{
int data[10],n;
cout << "banyak data:";
cin >> n;
baca_data(data,n);
cetak_data(data,n);
buble_sort(data,n);
cetak_data(data,n);
system("PAUSE");
return EXIT_SUCCESS;
}
insertion sorting
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
class Sorting{
friend istream& operator >> (istream& in,Sorting& a);
friend ostream& operator << (ostream& out,Sorting& a);
public:
void baca_data();
void cetak_data();
void buble_sort();
void insertion_sort();
void selection_sort();
private:
void minimum(int, int,int&);
void tukar(int&,int&);
int data[10],n;
};
istream& operator>>(istream& in,Sorting& a){
cout<<"Banyak data:";
in>>a.n;
for(int i=0;i<a.n;i++){
cout<<"Data ke-"<<i+1<<":";
in>>a.data[i];
}
return in;
}
ostream& operator << (ostream& out,Sorting& a){
for(int i=0;i<a.n;i++)
out<<a.data[i]<<"";
out<<"\n";
return out;
}
void Sorting::tukar(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void Sorting::insertion_sort()
{
int i,j,temp;
cout<<"data pertama sudah ada ditangan kiri,yaitu:"<<data[0]<<"\n";
for(j=1;j<n;j++)
{
temp=data[j];
cout<<"\nData ke-"<<j+1<<"yaitu"<<data[j]<<"diambil dari meja\n";
cout<<"dilakukan penggeseran elemen:\n";
for(i=j-1;(i>=0)&&(data[i]> temp);i--)
{
cout<<"data pada posisi ke-["<<i+1<<"]digeser ke posisi["<<i+2<<"]\n";
data[i+1]=data[i];
}
cout<<"Data baru masuk pada posisi ke-["<<i+2<<"]\n";
data[i+1]=temp;
cout<<"Data saat ini adalah:";
for(int k=0;k<=j;k++)cout<<data[k]<<"";
}
}
int main()
{
Sorting angka;
cin>>angka;
angka.insertion_sort();
cout<<"\nHasil akhir adalah:\n";
cout<<angka;
system("PAUSE");
return EXIT_SUCCESS;
}
selection sorting
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
int baca_data(int A[],int n)
{
int i;
for(i=0;i<n;i++)
{
cout << "Data ke-" <<i+1<<":";
cin >> A[i];
}
}
int cetak_data(const int A[],int n)
{
int i;
for(i=0;i<n;i++)
cout<<""<<A[i];
cout << "\n";
}
int tukar(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int minimum(int A[],int dari,int n,int * tempat)
{
int i,min;
min=A[dari];
*tempat=dari;
for(i=dari+1;i<n;i++)
if(A[i] < min)
{
min=A[i];
*tempat=i;
}
}
int selection_sort(int A[],int n)
{
int i,t;
for(i=0;i<n;i++)
{
minimum(A,i,n,&t);
tukar(&A[i],&A[t]);
}
}
int main(int argc, char *argv[])
{
int data[10],n;
cout<<"banyak data:";
cin>>n;
baca_data(data,n);
cetak_data(data,n);
selection_sort(data,n);
cetak_data(data,n);
system("PAUSE");
return EXIT_SUCCESS;
}
No comments:
Post a Comment