Buble Sort C++

Posted by Didi Setyapramana On 7:02 PM 0 komentar

#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;
}

Categories:

0 Response for the "Buble Sort C++"

Post a Comment