บทนำสู่ Bubble Sort Algorithm

บทนำสู่ Bubble Sort Algorithm

การเรียงลำดับเป็นหนึ่งในการดำเนินการพื้นฐานที่สุดที่คุณสามารถนำไปใช้กับข้อมูลได้ คุณสามารถจัดเรียงองค์ประกอบในภาษาการเขียนโปรแกรมต่างๆ โดยใช้อัลกอริธึมการจัดเรียงต่างๆ เช่น Quick Sort, Bubble Sort, Merge Sort, Insertion Sort เป็นต้น Bubble Sort เป็นอัลกอริธึมที่ง่ายที่สุดในบรรดาสิ่งเหล่านี้





ในบทความนี้ คุณจะได้เรียนรู้เกี่ยวกับการทำงานของอัลกอริธึม Bubble Sort รหัสเทียมของอัลกอริธึม Bubble Sort ความซับซ้อนของเวลาและพื้นที่ และการใช้งานในภาษาการเขียนโปรแกรมต่างๆ เช่น C++, Python, C และ JavaScript





อัลกอริธึมการเรียงลำดับฟองทำงานอย่างไร

Bubble Sort คืออัลกอริธึมการจัดเรียงที่ง่ายที่สุดที่ทำตามขั้นตอนซ้ำๆ ในรายการ เปรียบเทียบองค์ประกอบที่อยู่ติดกัน และสลับองค์ประกอบเหล่านั้นหากเรียงผิด แนวคิดนี้สามารถอธิบายได้อย่างมีประสิทธิภาพมากขึ้นโดยใช้ตัวอย่าง พิจารณาอาร์เรย์ที่ไม่เรียงลำดับด้วยองค์ประกอบต่อไปนี้: {16, 12, 15, 13, 19}





ตัวอย่าง:

องค์ประกอบที่อยู่ติดกันจะถูกเปรียบเทียบที่นี่ และหากพวกมันไม่เรียงลำดับจากน้อยไปหามาก พวกมันจะถูกสลับ



Pseudocode ของ Bubble Sort Algorithm

ใน pseudocode อัลกอริทึม Bubble Sort สามารถแสดงเป็น:

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

อัลกอริธึมข้างต้นประมวลผลการเปรียบเทียบทั้งหมดแม้ว่าอาร์เรย์จะเรียงลำดับแล้วก็ตาม สามารถปรับให้เหมาะสมเพิ่มเติมได้โดยการหยุดอัลกอริทึมหากวงในไม่ได้ทำให้เกิดการสลับใดๆ สิ่งนี้จะลดเวลาดำเนินการของอัลกอริทึม





ดังนั้น pseudocode ของอัลกอริธึม Bubble Sort ที่ปรับให้เหมาะสมสามารถแสดงเป็น:

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

ความซับซ้อนของเวลาและพื้นที่เสริมของ Bubble Sort Algorithm

ความซับซ้อนของเวลาที่แย่ที่สุดของ Bubble Sort Algorithm คือ O(n^2) เกิดขึ้นเมื่ออาร์เรย์อยู่ในลำดับจากมากไปหาน้อยและคุณต้องการเรียงลำดับจากน้อยไปหามากหรือในทางกลับกัน





วิธีดึงข้อความที่ถูกลบบน facebook

ความซับซ้อนของเวลาในกรณีที่ดีที่สุดของ Bubble Sort Algorithm คือ O(n) เกิดขึ้นเมื่อจัดเรียงอาร์เรย์แล้ว

คีย์บอร์ดไร้สายของ apple เชื่อมต่อไม่ได้

ที่เกี่ยวข้อง: สัญกรณ์ Big-O คืออะไร?

ความซับซ้อนของเวลาเฉลี่ยกรณีของอัลกอริธึม Bubble Sort คือ O(n^2) เกิดขึ้นเมื่อองค์ประกอบของอาร์เรย์อยู่ในลำดับที่สับสน

พื้นที่เสริมที่จำเป็นสำหรับอัลกอริธึม Bubble Sort คือ O(1)

การใช้ C++ ของ Bubble Sort Algorithm

ด้านล่างนี้คือการใช้งาน C++ ของอัลกอริทึม 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;
}

เอาท์พุท:

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

การใช้งาน Python ของ Bubble Sort Algorithm

ด้านล่างนี้คือการใช้งาน Python ของอัลกอริทึม 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)

เอาท์พุท:

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

ที่เกี่ยวข้อง: วิธีใช้สำหรับลูปใน Python

C การดำเนินการของ Bubble Sort Algorithm

ด้านล่างนี้คือการใช้ C ของอัลกอริทึม 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;
}

เอาท์พุท:

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

การใช้งาน JavaScript ของ Bubble Sort Algorithm

ด้านล่างนี้คือการใช้งาน JavaScript ของอัลกอริทึม 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);

เอาท์พุท:

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

ตอนนี้คุณเข้าใจการทำงานของ Bubble Sort Algorithm แล้ว

Bubble Sort เป็นอัลกอริธึมการเรียงลำดับที่ง่ายที่สุดและใช้เพื่อทำความเข้าใจพื้นฐานของการเรียงลำดับเป็นหลัก Bubble Sort สามารถใช้ซ้ำได้ แต่ไม่มีข้อดีเพิ่มเติมในการทำเช่นนั้น

เมื่อใช้ Python คุณสามารถใช้อัลกอริธึม Bubble Sort ได้อย่างง่ายดาย หากคุณไม่คุ้นเคยกับ Python และต้องการเริ่มต้นการเดินทาง การเริ่มต้นด้วยสคริปต์ 'Hello World' เป็นทางเลือกที่ดี

แบ่งปัน แบ่งปัน ทวีต อีเมล วิธีเริ่มต้นใช้งาน Python โดยใช้สคริปต์ 'Hello World'

Python เป็นหนึ่งในภาษาโปรแกรมที่นิยมใช้กันมากที่สุดในปัจจุบัน ทำตามบทช่วยสอนนี้เพื่อเริ่มต้นกับสคริปต์ Python แรกของคุณ

อ่านต่อไป
หัวข้อที่เกี่ยวข้อง
  • การเขียนโปรแกรม
  • Java
  • Python
  • บทเรียนการเข้ารหัส
เกี่ยวกับผู้เขียน ยุวราช จันทรา(60 บทความที่ตีพิมพ์)

Yuvraj เป็นนักศึกษาระดับปริญญาตรีสาขาวิทยาการคอมพิวเตอร์ที่มหาวิทยาลัยเดลี ประเทศอินเดีย เขาหลงใหลเกี่ยวกับ Full Stack Web Development เมื่อไม่ได้เขียน เขากำลังสำรวจความลึกของเทคโนโลยีต่างๆ

หลักสูตรการเขียนโปรแกรมออนไลน์ฟรีพร้อมใบรับรอง
เพิ่มเติมจาก Yuvraj Chandra

สมัครรับจดหมายข่าวของเรา

เข้าร่วมจดหมายข่าวของเราสำหรับเคล็ดลับทางเทคนิค บทวิจารณ์ eBook ฟรี และดีลพิเศษ!

คลิกที่นี่เพื่อสมัครสมาชิก