Algoritma :
Deklarasi
i,j : integer
data[] : integer
Deskripsi:
for i <- 1 to n do
for j <- n to i do
if data[j]<data[j-1] then
tukar (j,j-1)
end if
end for
end for
Deklarasi
i,j : integer
data[] : integer
Deskripsi:
for i <- 1 to n do
for j <- n to i do
if data[j]<data[j-1] then
tukar (j,j-1)
end if
end for
end for
end
Program :
#include <iostream.h>
#include <conio.h>
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 bubble(){
for(int i=1;i<=n;i++){
for(int j=n;j>=i;j--){
if(data[j]<data[j-1])
tukar(j,j-1);}}}
int main(){
cout<<"Bubble Sort";
cout<<"\nBanyak data : ";
cin>>n;
for(int i=1;i<=n;i++){
cout<<"Data ke "<<i<<" : ";
cin>>data[i];
data2[i]=data[i];}
bubble();
cout<<"Data setelah di sort : ";
for(int i=1; i<=n; i++){
cout<<" "<<data[i];}
getch();
return 0;}
Program :
#include <iostream.h>
#include <conio.h>
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 bubble(){
for(int i=1;i<=n;i++){
for(int j=n;j>=i;j--){
if(data[j]<data[j-1])
tukar(j,j-1);}}}
int main(){
cout<<"Bubble Sort";
cout<<"\nBanyak data : ";
cin>>n;
for(int i=1;i<=n;i++){
cout<<"Data ke "<<i<<" : ";
cin>>data[i];
data2[i]=data[i];}
bubble();
cout<<"Data setelah di sort : ";
for(int i=1; i<=n; i++){
cout<<" "<<data[i];}
getch();
return 0;}
0 comments:
Post a Comment