Jumat, 08 Juli 2011

bubblesort program

#include <cstdlib>
#include <iostream>

using namespace std;
void baca_data(int A[], int n){
     int i;
     for(i=0; i<n; i++)
     { cout<<"Data ke-"<<i+1<<" : ";
     cin>> A[i];
               }

               }
     void cetak_data(const int A[], int n){
          int i;
          for(i=0; i<n; i++)
          cout<<A[i]<<" ";
          cout<<"\n";
          }
     void tukar (int *a, int *b)
     {
     int temp;
     temp= *a;
     *a= *b;
     *b= temp;
     }
void tampil_data(int data[], int n)
{
     int i;
     for(i=0; i<n; i++){
     cout << data[i] << " ";
     cout << "\n";
     }
}

void bubble_sort(int data[], int n)
{
     int fase;
     int j;
     int temp;
     int tukar_data;
     fase = 1;
     tukar_data = 1;
      while (fase < n-1 && tukar_data){
      tukar_data = 0;
      for (j=0; j<n-fase; j++)
      if (data[j] > data[j+1]){
         tukar_data = 1;
         temp = data[j];
         data[j] = data[j+1];
         data[j+1] = temp;
         }
         cout << "Fase " << fase << " : ";
         tampil_data(data, n);
         fase++;
         }
         }
    


int main(int argc, char *argv[])
{
    int data[10], n;
    cout<<"Banyak data : ";
    cin>>n;
    baca_data(data,n);
    cetak_data(data,n);
    bubble_sort(data,n);
    cetak_data(data,n);
    cout << "Hasil Pengurutan : ";
    tampil_data(data, n);
    system("PAUSE");
    return EXIT_SUCCESS;
}

0 komentar:

Posting Komentar

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Blogger Theme by Lasantha - Premium Blogger Templates | Affiliate Network Reviews