satriafu5710

Pengurutan Bubble Sort secara Menaik C++

May 6th, 2021
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void tukarData(int *a, int *b){
  5.  
  6.     int temp = *a;
  7.     *a = *b;
  8.     *b = temp;
  9. }
  10.  
  11. void bubbleShort_menaik(int data[], int n){
  12.  
  13.     for(int i = 0; i < n-1; i++){
  14.  
  15.         for(int j = i + 1; j < n; j++){
  16.  
  17.             if(data[i] > data[j]){
  18.  
  19.                 tukarData(&data[i], &data[j]);
  20.             }
  21.         }
  22.     }
  23. }
  24.  
  25. int main(){
  26.  
  27.     int data[10], n;
  28.  
  29.     cout << "\t Pengurutan Data dengan Algoritma Bubble Sort secara Menaik \n\n";
  30.  
  31.     cout << " Masukkan Banyak Data : ";
  32.     cin >> n;
  33.    
  34.     cout << endl;
  35.  
  36.     for(int i = 0; i < n; i++){
  37.  
  38.         cout << " Data ke " << i + 1 << " : ";
  39.         cin >> data[i];
  40.     }
  41.  
  42.     cout << "\n Data Sebelum Diurutkan : ";
  43.  
  44.     for(int i = 0; i < n; i++){
  45.  
  46.         cout << data[i] << " ";
  47.     }
  48.  
  49.     bubbleShort_menaik(data, n);
  50.  
  51.     cout << "\n Data Setelah Diurutkan : ";
  52.  
  53.     for(int i = 0; i < n; i++){
  54.  
  55.         cout << data[i] << " ";
  56.     }
  57. }
Add Comment
Please, Sign In to add comment