Pengenalan Algoritma Bubble Sort

Pengenalan Algoritma Bubble Sort

Menyusun adalah salah satu operasi paling asas yang boleh anda gunakan untuk data. Anda boleh menyusun elemen dalam bahasa pengaturcaraan yang berbeza menggunakan pelbagai algoritma penyortiran seperti Quick Sort, Bubble Sort, Merge Sort, Insertion Sort, etc. Bubble Sort adalah algoritma yang paling mudah di antara semua ini.





Dalam artikel ini, anda akan belajar mengenai cara kerja algoritma Bubble Sort, kode semu algoritma Bubble Sort, kerumitan masa dan ruangnya, dan pelaksanaannya dalam pelbagai bahasa pengaturcaraan seperti C ++, Python, C, dan JavaScript.





Bagaimana Algoritma Bubble Sort berfungsi?

Bubble Sort adalah algoritma penyortiran termudah yang berulang kali melalui senarai, membandingkan elemen bersebelahan, dan menukarnya jika berada dalam urutan yang salah. Konsep ini dapat dijelaskan dengan lebih berkesan dengan bantuan contoh. Pertimbangkan susunan yang tidak disusun dengan elemen berikut: {16, 12, 15, 13, 19}.





Contoh:

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



Pseudokod Algoritma Susun Gelembung

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 walaupun array sudah disusun. Ia dapat dioptimumkan lebih jauh dengan menghentikan algoritma jika gelung dalam tidak menyebabkan pertukaran. Ini akan mengurangkan masa pelaksanaan algoritma.





Oleh itu, pseudokod algoritma Bubble Sort yang dioptimumkan 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

Kerumitan Masa dan Ruang Tambahan Algoritma Bubble Sort

Kerumitan masa terburuk Algoritma Bubble Sort adalah O (n ^ 2). Ia berlaku apabila susunan berada dalam urutan menurun dan anda mahu menyusunnya mengikut tertib menaik atau sebaliknya.





linux distro yang menjalankan program windows

Kerumitan masa terbaik bagi Algoritma Bubble Sort adalah O (n). Ia berlaku apabila array sudah disusun.

cara mempercepat data mudah alih

Berkaitan: Apa itu Notasi Big-O?

Kerumitan masa kes rata-rata Algoritma Bubble Sort adalah O (n ^ 2). Ia berlaku apabila unsur-unsur array berada dalam urutan yang tidak rata.

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

C ++ Pelaksanaan Algoritma Bubble Sort

Berikut adalah pelaksanaan C ++ 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;
}

Pengeluaran:

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

Python Pelaksanaan Algoritma Bubble Sort

Berikut adalah pelaksanaan Python 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)

Pengeluaran:

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

Berkaitan: Cara Menggunakan Untuk Gelung di Python

C Pelaksanaan Algoritma Bubble Sort

Berikut adalah pelaksanaan C 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;
}

Pengeluaran:

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

Pelaksanaan JavaScript Algoritma Bubble Sort

Berikut adalah pelaksanaan JavaScript 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);

Pengeluaran:

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

Kini Anda Memahami Cara Kerja Algoritma Bubble Sort

Bubble Sort adalah algoritma penyortiran termudah dan digunakan terutamanya untuk memahami asas penyortiran. Bubble Sort juga dapat dilaksanakan secara berulang, tetapi tidak memberikan kelebihan tambahan untuk melakukannya.

Dengan menggunakan Python, anda dapat melaksanakan algoritma Bubble Sort dengan mudah. Sekiranya anda tidak biasa dengan Python dan ingin memulakan perjalanan anda, mulakan dengan skrip 'Hello World' adalah pilihan yang tepat.

Berkongsi Berkongsi Tweet E-mel Cara Bermula Dengan Python Menggunakan Skrip 'Hello World'

Python adalah salah satu bahasa pengaturcaraan paling popular yang digunakan sekarang. Ikuti tutorial ini untuk memulakan skrip Python pertama anda.

Baca Seterusnya
Topik-topik yang berkaitan
  • Pengaturcaraan
  • Jawa
  • Python
  • Tutorial Pengekodan
Mengenai Pengarang Yuvraj Chandra(60 Artikel Diterbitkan)

Yuvraj adalah pelajar sarjana Sains Komputer di University of Delhi, India. Dia meminati Pembangunan Web Stack Penuh. Ketika dia tidak menulis, dia meneroka kedalaman teknologi yang berbeza.

cara memaksa mematikan mac
Lagi Dari Yuvraj Chandra

Langgan buletin kami

Sertailah buletin kami untuk mendapatkan petua, ulasan, ebook percuma, dan tawaran eksklusif!

Klik di sini untuk melanggan