Bubble Sort Algorithm Pseudo-Code - Hi friends, here I will share a little about c + + programming material is the material of the bubble sort, Bubble sort is sorting the data to be exchanged are the smallest or the largest value to the end position of the data value to be sorted. And so on until all the lists in a sorted state. Basic processes that occur in this algorithm is the process of exchanging value (swapping).
Sort Buble is divided into two kinds:
- Sort Bubble Ascending is sorting the data from small to large
- Sort Bubble Descending is sorting the data from the large to the small
okay friends everything, for more details, I will give employees an example of Bubble and Bubble Sort Sort Ascending Descending.
BUBLE
SORT ASCENDING
|
#include <iostream.h>
#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 bubble_sort(){
for(int i=1;i<=n-1;i++)
{
for(int j=n;j>=i;j--)
{
if(data[j]<data[j-1])
tukar(j,j-1);
}
}
}
int main(){
cout<<"PROGRAM BUBLE SORT
ASCENDING"<<endl;
cout<<"Masukkan Jumlah Data Anda : ";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"Masukkan data ke
"<<i<<":";
cin>>data[i];
data2[i]=data[i];
}
bubble_sort();
cout<<"\n\n";
//tampillkan data
cout<<"Data Setelah di Sort : ";
for(int i=1; i<=n;i++)
{
cout<<" "<<data[i];
}
cout<<"\n\nSorting Selesai";
getch();
return 0;
}
|
BUBLE
SORT DESCENDING
|
#include <iostream.h>
#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 bubble_sort(){
for(int i=1+1;i<=n;i++)
{
for(int j=n;j>=i;j--)
{
if(data[j]>data[j-1])
tukar(j,j-1);
}
}
}
int main(){
cout<<"PROGRAM BUBLE SORT
DESCENDING"<<endl;
cout<<"Masukkan Jumlah Data Anda : ";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"Masukkan data ke
"<<i<<":";
cin>>data[i];
data2[i]=data[i];
}
bubble_sort();
cout<<"\n\n";
//tampillkan data
cout<<"Data Setelah di Sort : ";
for(int i=1; i<=n;i++)
{
cout<<" "<<data[i];
}
cout<<"\n\nSorting Selesai";
getch();
return 0;
}
|
EmoticonEmoticon