Pengantar Algoritma Bubble Sort

Pengantar Algoritma Bubble Sort

Penyortiran adalah salah satu operasi paling dasar yang dapat Anda terapkan pada data. Anda dapat mengurutkan elemen dalam bahasa pemrograman yang berbeda menggunakan berbagai algoritme pengurutan seperti Quick Sort, Bubble Sort, Merge Sort, Insertion Sort, dll. Bubble Sort adalah algoritme paling sederhana di antara semua ini.





Pada artikel ini, Anda akan belajar tentang cara kerja algoritma Bubble Sort, pseudocode dari algoritma Bubble Sort, kompleksitas ruang dan waktu, dan implementasinya dalam berbagai bahasa pemrograman seperti C++, Python, C, dan JavaScript.





Bagaimana Algoritma Bubble Sort Bekerja?

Bubble Sort adalah algoritme pengurutan paling sederhana yang berulang kali menelusuri daftar, membandingkan elemen yang berdekatan, dan menukarnya jika urutannya salah. Konsep ini dapat dijelaskan lebih efisien dengan bantuan sebuah contoh. Pertimbangkan array yang tidak disortir dengan elemen-elemen berikut: {16, 12, 15, 13, 19}.





Contoh:

Di sini elemen yang berdekatan dibandingkan dan jika tidak dalam urutan menaik, mereka ditukar.



Pseudocode dari Algoritma Bubble Sort

Dalam pseudocode , algoritma Bubble Sort dapat dinyatakan sebagai:

bubbleSort(Arr[], size)
// loop to access each array element
for i=0 to size-1 do:
// loop to compare array elements
for j=0 to size-i-1 do:
// compare the adjacent elements
if Arr[j] > Arr[j+1] then
// swap them
swap(Arr[j], Arr[j+1])
end if
end for
end for
end

Algoritma di atas memproses semua perbandingan bahkan jika array sudah diurutkan. Ini dapat dioptimalkan lebih lanjut dengan menghentikan algoritme jika loop dalam tidak menyebabkan pertukaran apa pun. Ini akan mengurangi waktu eksekusi algoritma.





Dengan demikian, pseudocode dari algoritma Bubble Sort yang dioptimalkan dapat dinyatakan sebagai:

bubbleSort(Arr[], size)
// loop to access each array element
for i=0 to size-1 do:
// check if swapping occurs
swapped = false
// loop to compare array elements
for j=0 to size-i-1 do:
// compare the adjacent elements
if Arr[j] > Arr[j+1] then
// swap them
swap(Arr[j], Arr[j+1])
swapped = true
end if
end for
// if no elements were swapped that means the array is sorted now, then break the loop.
if(not swapped) then
break
end if
end for
end

Kompleksitas Waktu dan Ruang Bantu dari Algoritma Bubble Sort

Kompleksitas waktu kasus terburuk dari Algoritma Bubble Sort adalah O(n^2). Itu terjadi ketika array dalam urutan menurun dan Anda ingin mengurutkannya dalam urutan menaik atau sebaliknya.





cara mengembalikan pesan yang terhapus di facebook

Kompleksitas waktu kasus terbaik dari Algoritma Bubble Sort adalah O(n). Itu terjadi ketika array sudah diurutkan.

layanan nox google play telah berhenti

Terkait: Apa itu Notasi Big-O?

Kompleksitas waktu kasus rata-rata dari Algoritma Bubble Sort adalah O(n^2). Itu terjadi ketika elemen-elemen array berada dalam urutan campur aduk.

Ruang tambahan yang diperlukan untuk algoritma Bubble Sort adalah O(1).

Implementasi C++ dari Algoritma Bubble Sort

Di bawah ini adalah implementasi C++ dari algoritma Bubble Sort:

// C++ implementation of the
// optimised Bubble Sort algorithm
#include
using namespace std;
// Function to perform Bubble Sort
void bubbleSort(int arr[], int size) {
// Loop to access each element of the array
for (int i=0; i<(size-1); i++) {
// Variable to check if swapping occurs
bool swapped = false;
// loop to compare two adjacent elements of the array
for (int j = 0; j <(size-i-1); j++) {
// Comparing two adjacent array elements
if (arr[j] > arr[j + 1]) {
// Swap both elements if they're
// not in correct order
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped that means the array is sorted now,
// then break the loop.
if (swapped == false) {
break;
}
}
}
// Prints the elements of the array
void printArray(int arr[], int size) {
for (int i = 0; i cout << arr[i] << ' ';
}
cout << endl;
}
int main() {
int arr[] = {16, 12, 15, 13, 19};
// Finding the length of the array
int size = sizeof(arr) / sizeof(arr[0]);
// Printing the given unsorted array
cout << 'Unsorted Array: ' << endl;
printArray(arr, size);
// Calling bubbleSort() function
bubbleSort(arr, size);
// Printing the sorted array
cout << 'Sorted Array in Ascending Order:' << endl;
printArray(arr, size);
return 0;
}

Keluaran:

Unsorted Array:
16 12 15 13 19
Sorted Array in Ascending Order:
12 13 15 16 19

Implementasi Python dari Algoritma Bubble Sort

Di bawah ini adalah implementasi Python dari algoritma Bubble Sort:

# Python implementation of the
# optimised Bubble Sort algorithm

# Function to perform Bubble Sort
def bubbleSort(arr, size):
# Loop to access each element of the list
for i in range (size-1):
# Variable to check if swapping occurs
swapped = False
# loop to compare two adjacent elements of the list
for j in range(size-i-1):
# Comparing two adjacent list elements
if arr[j] > arr[j+1]:
temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
swapped = True
# If no elements were swapped that means the list is sorted now,
# then break the loop.
if swapped == False:
break
# Prints the elements of the list
def printArray(arr):
for element in arr:
print(element, end=' ')
print('')

arr = [16, 12, 15, 13, 19]
# Finding the length of the list
size = len(arr)
# Printing the given unsorted list
print('Unsorted List:')
printArray(arr)
# Calling bubbleSort() function
bubbleSort(arr, size)
# Printing the sorted list
print('Sorted List in Ascending Order:')
printArray(arr)

Keluaran:

Unsorted List:
16 12 15 13 19
Sorted List in Ascending Order:
12 13 15 16 19

Terkait: Cara Menggunakan For Loop dengan Python

C Implementasi Algoritma Bubble Sort

Di bawah ini adalah implementasi C dari algoritma Bubble Sort:

// C implementation of the
// optimised Bubble Sort algorithm
#include
#include
// Function to perform Bubble Sort
void bubbleSort(int arr[], int size) {
// Loop to access each element of the array
for (int i=0; i<(size-1); i++) {
// Variable to check if swapping occurs
bool swapped = false;
// loop to compare two adjacent elements of the array
for (int j = 0; j <(size-i-1); j++) {
// Comparing two adjacent array elements
if (arr[j] > arr[j + 1]) {
// Swap both elements if they're
// not in correct order
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped that means the array is sorted now,
// then break the loop.
if (swapped == false) {
break;
}
}
}
// Prints the elements of the array
void printArray(int arr[], int size) {
for (int i = 0; i printf('%d ', arr[i]);
}
printf(' ⁠n ');
}
int main() {
int arr[] = {16, 12, 15, 13, 19};
// Finding the length of the array
int size = sizeof(arr) / sizeof(arr[0]);
// Printing the given unsorted array
printf('Unsorted Array: ⁠n');
printArray(arr, size);
// Calling bubbleSort() function
bubbleSort(arr, size);
// Printing the sorted array
printf('Sorted Array in Ascending Order: ⁠n');
printArray(arr, size);
return 0;
}

Keluaran:

Unsorted Array:
16 12 15 13 19
Sorted Array in Ascending Order:
12 13 15 16 19

Implementasi JavaScript dari Algoritma Bubble Sort

Di bawah ini adalah implementasi JavaScript dari algoritma Bubble Sort:

// JavaScript implementation of the
// optimised Bubble Sort algorithm
// Function to perform Bubble Sort
function bubbleSort(arr, size) {
// Loop to access each element of the array
for(let i=0; i // Variable to check if swapping occurs
var swapped = false;
// loop to compare two adjacent elements of the array
for(let j=0; j // Comparing two adjacent array elements
if(arr[j] > arr[j+1]) {
// Swap both elements if they're
// not in correct order
let temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = true;
}
// If no elements were swapped that means the array is sorted now,
// then break the loop.
if (swapped == false) {
break;
}
}
}
}
// Prints the elements of the array
function printArray(arr, size) {
for (let i=0; i document.write(arr[i] + ' ');
}
document.write('
')
}

var arr = [16, 12, 15, 13, 19];
// Finding the length of the array
var size = arr.length;
// Printing the given unsorted array
document.write('Unsorted Array:
');
printArray(arr, size);
// Calling bubbleSort() function
bubbleSort(arr, size);
// Printing the sorted array
document.write('Sorted Array in Ascending Order:
');
printArray(arr, size);

Keluaran:

Unsorted Array:
16 12 15 13 19
Sorted Array in Ascending Order:
12 15 13 16 19

Sekarang Anda Memahami Cara Kerja Algoritma Bubble Sort

Bubble Sort adalah algoritma pengurutan paling sederhana dan terutama digunakan untuk memahami dasar-dasar pengurutan. Bubble Sort juga dapat diimplementasikan secara rekursif, tetapi tidak memberikan keuntungan tambahan untuk melakukannya.

Menggunakan Python, Anda dapat mengimplementasikan algoritma Bubble Sort dengan mudah. Jika Anda tidak terbiasa dengan Python dan ingin memulai perjalanan Anda, memulai dengan skrip 'Hello World' adalah pilihan yang tepat.

Membagikan Membagikan Menciak Surel Bagaimana Memulai Dengan Python Menggunakan Skrip 'Hello World'

Python adalah salah satu bahasa pemrograman yang paling populer digunakan saat ini. Ikuti tutorial ini untuk memulai dengan skrip Python pertama Anda.

Baca Selanjutnya
Topik-topik yang berkaitan
  • Pemrograman
  • Jawa
  • Python
  • Tutorial Pengkodean
Tentang Penulis Yuvraj Chandra(60 Artikel Diterbitkan)

Yuvraj adalah mahasiswa sarjana Ilmu Komputer di University of Delhi, India. Dia bersemangat tentang Pengembangan Web Full Stack. Ketika dia tidak menulis, dia menjelajahi kedalaman teknologi yang berbeda.

apakah tiktok dilarang di AS
More From Yuvraj Chandra

Berlangganan newsletter kami

Bergabunglah dengan buletin kami untuk kiat teknologi, ulasan, ebook gratis, dan penawaran eksklusif!

Klik di sini untuk berlangganan