Sắp xếp bong bóng là một thuật toán sắp xếp so sánh hai phần tử liền kề và hoán đổi chúng cho đến khi chúng theo thứ tự dự định
Giống như sự chuyển động của bong bóng khí trong nước nổi lên mặt nước, mỗi phần tử của mảng sẽ di chuyển đến cuối mỗi lần lặp. Do đó, nó được gọi là sắp xếp bong bóng
Hoạt động của Sắp xếp bong bóng
Giả sử chúng ta đang cố sắp xếp các phần tử theo thứ tự tăng dần
1. Lần lặp đầu tiên [So sánh và Hoán đổi]
- Bắt đầu từ chỉ mục đầu tiên, so sánh các yếu tố đầu tiên và thứ hai
- Nếu phần tử đầu tiên lớn hơn phần tử thứ hai, chúng được đổi chỗ
- Bây giờ, so sánh các yếu tố thứ hai và thứ ba. Trao đổi chúng nếu chúng không theo thứ tự
- Quá trình trên cứ tiếp tục cho đến phần tử cuối cùng. So sánh các phần tử liền kề
2. Vòng lặp còn lại
Quá trình tương tự diễn ra cho các lần lặp còn lại
Sau mỗi lần lặp, phần tử lớn nhất trong số các phần tử chưa sắp xếp được đặt ở cuối
Trong mỗi lần lặp, việc so sánh diễn ra cho đến phần tử chưa sắp xếp cuối cùng
Mảng được sắp xếp khi tất cả các phần tử chưa sắp xếp được đặt vào đúng vị trí của chúng
Thuật toán sắp xếp bong bóng
bubbleSort[array]
for i rightElement
swap leftElement and rightElement
end bubbleSort
Mã sắp xếp bong bóng trong Python, Java và C/C++
# Bubble sort in Python
def bubbleSort[array]:
# loop to access each array element
for i in range[len[array]]:
# loop to compare array elements
for j in range[0, len[array] - i - 1]:
# compare two adjacent elements
# change > to < to sort in descending order
if array[j] > array[j + 1]:
# swapping elements if elements
# are not in the intended order
temp = array[j]
array[j] = array[j+1]
array[j+1] = temp
data = [-2, 45, 0, 11, -9]
bubbleSort[data]
print['Sorted Array in Ascending Order:']
print[data]
// Bubble sort in Java
import java.util.Arrays;
class Main {
// perform the bubble sort
static void bubbleSort[int array[]] {
int size = array.length;
// loop to access each array element
for [int i = 0; i < size - 1; i++]
// loop to compare array elements
for [int j = 0; j < size - i - 1; j++]
// compare two adjacent elements
// change > to < to sort in descending order
if [array[j] > array[j + 1]] {
// swapping occurs if elements
// are not in the intended order
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
public static void main[String args[]] {
int[] data = { -2, 45, 0, 11, -9 };
// call method using class name
Main.bubbleSort[data];
System.out.println["Sorted Array in Ascending Order:"];
System.out.println[Arrays.toString[data]];
}
}
// Bubble sort in C
#include
// perform the bubble sort
void bubbleSort[int array[], int size] {
// loop to access each array element
for [int step = 0; step < size - 1; ++step] {
// loop to compare array elements
for [int i = 0; i < size - step - 1; ++i] {
// compare two adjacent elements
// change > to < to sort in descending order
if [array[i] > array[i + 1]] {
// swapping occurs if elements
// are not in the intended order
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}
// print array
void printArray[int array[], int size] {
for [int i = 0; i < size; ++i] {
printf["%d ", array[i]];
}
printf["\n"];
}
int main[] {
int data[] = {-2, 45, 0, 11, -9};
// find the array's length
int size = sizeof[data] / sizeof[data[0]];
bubbleSort[data, size];
printf["Sorted Array in Ascending Order:\n"];
printArray[data, size];
}
// Bubble sort in C++
#include
using namespace std;
// perform bubble sort
void bubbleSort[int array[], int size] {
// loop to access each array element
for [int step = 0; step < size; ++step] {
// loop to compare array elements
for [int i = 0; i < size - step; ++i] {
// compare two adjacent elements
// change > to < to sort in descending order
if [array[i] > array[i + 1]] {
// swapping elements if elements
// are not in the intended order
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}
// print array
void printArray[int array[], int size] {
for [int i = 0; i < size; ++i] {
cout