Rabu, 18 Mei 2011

Program C++ tentang Bubble Sort


Haiiii temann,,, pada kali ini saya sedikit memberi teman-teman ilmu walaupun hanya sedikit semogaa dapat bergunaa... hehhehhe amieen... Saya membahas tentang sorting yaitu khususnya bubble sort. Silahkan teman-tman liat algoritma dan programnya di bawah ini beserta outputnya ,,, hehhehehe .. check it out ....

Algoritma
Procedure Bubble Sort ( input/output data : larik, input n : byte )
Deskripsi
     for i <= 1 to n do
        for j <= n to i do
            if data[j] < data[j-1] then
               tukar(j,j-1)
            endif
        endfor
     endfor

Berikut programnya :
#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_sort()
{
  for(int i=1;i<=n;i++){
   for(int j=n; j>=i; j--){
    if(data[j] < data[j-1]) tukar(j,j-1);
   }
  }
}

void main()
{
 cout<<"===PROGRAM BUBBLE SORT==="<<endl;
 cout<<"Masukkan Jumlah Data : ";
 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";
 cout<<"Data Setelah di Sort : ";
 for(int i=1; i<=n; i++)
 {
  cout<<" "<<data[i];
 }
 cout<<"\n\nSorting dengan bubble sort Selesai";
 getch();
}

Output :

Program Explanation:
At this time I will explain about the sorting program which in the above program is a bubble sort. Bubble sort method is a method that bases the exchange 2 pieces sorted elements to achieve the desired state. Each data (eg the first data) will be compared with existing data in the next (data from the second to complete). Which is how the program if the first data is larger than the existing data in the data afterwards, conducted onsite place or position data. Similarly, for the second data until the last data carried in a similar manner.

Ilustrasi
Diberikan sebuah data : [8,4,7,3,1,2,6,5]
Mengurutkan bilangan atau data di atas dengan bubble sort sebagaimana cara kerjanya seperti di bawah ini :



 Sekiaan dari saya temaan...
Semoga bermanfaat yha... thankz :)

2 komentar:

Anonymous mengatakan...

Terima Kasih..
Blog ini sangat membantu saya belajar pemrogrman C++ ..

B.B.B mengatakan...

mantap deh beb.,.,

Posting Komentar

Template by:

Free Blog Templates