Giới thiệu về thuật toán sắp xếp bong bóng

Giới thiệu về thuật toán sắp xếp bong bóng

Sắp xếp là một trong những thao tác cơ bản nhất mà bạn có thể áp dụng cho dữ liệu. Bạn có thể sắp xếp các phần tử trong các ngôn ngữ lập trình khác nhau bằng cách sử dụng các thuật toán sắp xếp khác nhau như Sắp xếp nhanh, Sắp xếp theo bong bóng, Sắp xếp hợp nhất, Sắp xếp chèn, v.v. Bubble Sort là thuật toán đơn giản nhất trong số tất cả các thuật toán này.





Trong bài viết này, bạn sẽ tìm hiểu về hoạt động của thuật toán Bubble Sort, mã giả của thuật toán Bubble Sort, độ phức tạp về thời gian và không gian của nó cũng như việc triển khai nó trong các ngôn ngữ lập trình khác nhau như C ++, Python, C và JavaScript.





Thuật toán sắp xếp bong bóng hoạt động như thế nào?

Bubble Sort là thuật toán sắp xếp đơn giản nhất lặp đi lặp lại các bước trong danh sách, so sánh các phần tử liền kề và hoán đổi chúng nếu chúng không đúng thứ tự. Khái niệm này có thể được giải thích hiệu quả hơn với sự trợ giúp của một ví dụ. Hãy xem xét một mảng không được sắp xếp với các phần tử sau: {16, 12, 15, 13, 19}.





Thí dụ:

Ở đây các phần tử liền kề được so sánh và nếu chúng không theo thứ tự tăng dần, chúng sẽ được hoán đổi.



Mã giả của thuật toán sắp xếp bong bóng

Trong mã giả, thuật toán Sắp xếp bong bóng có thể được biểu thị như sau:

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

Thuật toán trên xử lý tất cả các so sánh ngay cả khi mảng đã được sắp xếp. Nó có thể được tối ưu hóa hơn nữa bằng cách dừng thuật toán nếu vòng lặp bên trong không gây ra bất kỳ sự hoán đổi nào. Điều này sẽ làm giảm thời gian thực thi của thuật toán.





Do đó, mã giả của thuật toán Sắp xếp bong bóng được tối ưu hóa có thể được biểu thị như sau:

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

Độ phức tạp về thời gian và không gian phụ trợ của thuật toán sắp xếp bong bóng

Độ phức tạp thời gian trong trường hợp xấu nhất của Thuật toán sắp xếp bong bóng là O (n ^ 2). Nó xảy ra khi mảng có thứ tự giảm dần và bạn muốn sắp xếp nó theo thứ tự tăng dần hoặc ngược lại.





bật lên trên màn hình chính Android

Độ phức tạp thời gian trong trường hợp tốt nhất của Thuật toán sắp xếp bong bóng là O (n). Nó xảy ra khi mảng đã được sắp xếp.

bạn có thể thay đổi tên psn của bạn không

Có liên quan: Ký hiệu Big-O là gì?

Độ phức tạp thời gian theo trường hợp trung bình của Thuật toán sắp xếp bong bóng là O (n ^ 2). Nó xảy ra khi các phần tử của mảng có thứ tự lộn xộn.

Khoảng trống phụ trợ cần thiết cho thuật toán Sắp xếp bong bóng là O (1).

C ++ Triển khai thuật toán sắp xếp bong bóng

Dưới đây là cách triển khai C ++ của thuật toán Sắp xếp bong bóng:

// 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;
}

Đầu ra:

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

Triển khai thuật toán sắp xếp bong bóng trong Python

Dưới đây là cách triển khai thuật toán Sắp xếp bong bóng trong Python:

# 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)

Đầu ra:

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

Có liên quan: Cách sử dụng For Loops trong Python

C Triển khai thuật toán sắp xếp bong bóng

Dưới đây là cách triển khai C của thuật toán Sắp xếp bong bóng:

// 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;
}

Đầu ra:

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

JavaScript Triển khai Thuật toán Sắp xếp Bong bóng

Dưới đây là triển khai JavaScript của thuật toán Sắp xếp bong bóng:

// 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);

Đầu ra:

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

Bây giờ bạn đã hiểu hoạt động của thuật toán sắp xếp bong bóng

Bubble Sort là thuật toán sắp xếp đơn giản nhất và chủ yếu được sử dụng để hiểu cơ sở của việc sắp xếp. Bubble Sort cũng có thể được thực hiện một cách đệ quy, nhưng nó không cung cấp thêm lợi ích nào để làm như vậy.

Sử dụng Python, bạn có thể triển khai thuật toán Sắp xếp bong bóng một cách dễ dàng. Nếu bạn không quen với Python và muốn bắt đầu cuộc hành trình của mình, bắt đầu bằng tập lệnh 'Hello World' là một lựa chọn tuyệt vời.

Đăng lại Đăng lại tiếng riu ríu E-mail Cách bắt đầu với Python bằng tập lệnh 'Hello World'

Python là một trong những ngôn ngữ lập trình phổ biến nhất được sử dụng hiện nay. Làm theo hướng dẫn này để bắt đầu với tập lệnh Python đầu tiên của bạn.

Đọc tiếp
Chủ đề liên quan
  • Lập trình
  • Java
  • Python
  • Hướng dẫn viết mã
Giới thiệu về tác giả Yuvraj Chandra(60 bài báo đã xuất bản)

Yuvraj là sinh viên ngành Khoa học Máy tính tại Đại học Delhi, Ấn Độ. Anh ấy đam mê Phát triển Web Full Stack. Khi không viết, anh ấy đang khám phá chiều sâu của các công nghệ khác nhau.

làm thế nào dễ dàng để hack một webcam
Xem thêm từ Yuvraj Chandra

Theo dõi bản tin của chúng tôi

Tham gia bản tin của chúng tôi để biết các mẹo công nghệ, đánh giá, sách điện tử miễn phí và các ưu đãi độc quyền!

Bấm vào đây để đăng ký