daily pastebin goal
16%
SHARE
TWEET

Untitled

RakhaPratama Feb 17th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /******************************************************
  2. Nama file: bubble_sort.cpp
  3. *******************************************************/
  4.  
  5. #include <iostream>
  6.  
  7. void bubbleSort(int *array, int size) {  
  8.    for (auto i {0}; i<size-1; i++) {
  9.       for (auto j {size-1}; j>i; j--) {
  10.          if (array[j] < array[j-1]) {
  11.             int temp = array[j];
  12.             array[j] = array[j-1];
  13.             array[j-1] = temp;
  14.          }
  15.       }
  16.    }
  17. }
  18.  
  19. void cetakArray(int array[], int size) {
  20.    std::cout<<"[";
  21.    for (auto i {0}; i<size; i++) {
  22.       std::cout<<array[i];
  23.       if (i != size-1) std::cout<<", ";
  24.    }
  25.    std::cout<<"]"<<std::endl;
  26. }
  27.  
  28. int main() {
  29.    int data[8] {9, 7, 10, 8, 12, 11, 14, 13};
  30.  
  31.    /* menampilkan elemen array sebelum diurutkan */
  32.    std::cout<<"Sebelum diurutkan: ";
  33.    cetakArray(data, 8);
  34.  
  35.    /* mengurutkan array */
  36.    bubbleSort(data, 8);
  37.  
  38.    /* menampilkan elemen array setelah diurutkan */
  39.    std::cout<<"Setelah diurutkan: ";
  40.    cetakArray(data, 8);
  41.  
  42.    return 0;
  43. }
  44.  
  45. //saya copy dari kaset (bukan kodingan sendiri) wkwkwkw
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top