/******************************************************************************
https://www.onlinegdb.com/online_c++_compiler
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
void swap(int &x, int &y){
int temp;
temp = x;
x = y;
y = temp;
}
void bubbleSort(int data[], int n){
for(int i = 0; i < n - 1;i++){
//elemen i terakhir yang sudah ada
for(int j = 0; j < n - i - 1; j++){
if(data[j] > data[j + 1]) {
swap(data[j], data[j+1]);
}
}
}
}
void showShortedData(int data[], int n){
for(int i = 0; i < n; i++){
printf("%d ", data[i]);
}
printf("\n");
}
int main()
{
//Sorting - Bubble Sort
//adalah algoritma Sorting paling simple, dimana Sorting ini bekerja dengan cara,
//men-swap element yang bersebelahan
//jika ada urutan yang salah
int data[] = {5, 1, 4, 3, 2};
int n = 5;
bubbleSort(data, n);
showShortedData(data, n);
return 0;
}
Tidak ada komentar:
Posting Komentar