satriafu5710

Pengurutan Bubble Sort secara Menurun C++

May 8th, 2021
1,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 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 bubbleSort_menurun(int data[], int n){
  12.  
  13.     for(int i = 0; i < n; 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 Mengurutkan Data secara Menurun dengan Algoritma Bubble Sort \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.     for(int i = 0; i < n; i++){
  44.  
  45.         cout << data[i] << " ";
  46.     }
  47.  
  48.     bubbleSort_menurun(data, n);
  49.  
  50.     cout << "\n Data Setelah Diurutkan : ";
  51.     for(int i = 0; i < n; i++){
  52.  
  53.         cout << data[i] << " ";
  54.     }
  55. }
Add Comment
Please, Sign In to add comment