function BubbleShort (arr) {
let n = arr.length;
let swapped;
for (let i = 0; i < n - 1; i++){
for (let j = 0 ; j < n - i - 1 ; j++){
if (arr[j] > arr[j+1]){
let temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = true;
}
}
if (!swapped) break; // Hentikan jika tidak ada pertukaran
}
return arr;
}
function bubbleSortWithoutSwapped(arr) {
let n = arr.length;
for (let i = 0; i < n - 1; i++){
for (let j = 0 ; j < n - i - 1 ; j++){
if (arr[j] > arr[j+1]){
let temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return arr;
}
// Fungsi untuk mengukur waktu eksekusi
function measureTime(sortFunction, array) {
const arrCopy = [...array]; // Salin array agar tidak mengubah array asli
console.time(sortFunction.name); // Mulai pengukuran waktu
sortFunction(arrCopy); // Jalankan fungsi pengurutan
console.timeEnd(sortFunction.name); // Akhiri pengukuran waktu
}
// console.log('sebelum',[5,2,7,6,11,1], '\n');
// let result = BubbleShort([5,2,7,6,11,1]);
// console.log('sesudah \n');
// console.log(result)
const arr = [5,2,7,6,11,1];
measureTime(BubbleShort, arr);
measureTime(bubbleSortWithoutSwapped, arr);