Isang Panimula sa Algorithm ng Bubble Sort

Isang Panimula sa Algorithm ng Bubble Sort

Ang pag-uuri ay isa sa pinakamahalagang operasyon na maaari mong mailapat sa data. Maaari mong pag-uri-uriin ang mga elemento sa iba't ibang mga wika sa pagprograma gamit ang iba't ibang mga pag-uuri ng mga algorithm tulad ng Mabilis na Pag-uri-uriin, Pag-uuri ng Bubble, Pagsamahin ang Pagsunud-sunod, Pag-uri-uriin ang Pagsingit, atbp. Ang Bubble Sort ay ang pinaka-simpleng algorithm sa lahat ng mga ito.





Sa artikulong ito, malalaman mo ang tungkol sa pagtatrabaho ng algorithm ng Bubble Sort, ang pseudocode ng algorithm ng Bubble Sort, ang pagiging kumplikado ng oras at kalawakan, at ang pagpapatupad nito sa iba't ibang mga wika ng pagprograma tulad ng C ++, Python, C, at JavaScript.





Paano Gumagana ang Bubble Sort Algorithm?

Ang Bubble Sort ay ang pinakasimpleng pag-uuri ng algorithm na paulit-ulit na tumatak sa listahan, naghahambing sa mga katabing elemento, at ipinagpapalit kung mali ang pagkakasunud-sunod. Ang konseptong ito ay maaaring ipaliwanag nang mas mahusay sa tulong ng isang halimbawa. Isaalang-alang ang isang hindi nasusunog na array na may mga sumusunod na elemento: {16, 12, 15, 13, 19}.





Halimbawa:

Narito ang mga katabing elemento ay inihambing at kung hindi sila nasa pataas na pagkakasunud-sunod, napalitan sila.



Pseudocode ng Bubble Sort Algorithm

Sa pseudocode, ang Bubble Sort algorithm ay maaaring ipahayag bilang:

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

Pinoproseso ng algorithm sa itaas ang lahat ng mga paghahambing kahit na ang array ay naayos na. Maaari itong ma-optimize nang higit pa sa pamamagitan ng pagtigil sa algorithm kung ang panloob na loop ay hindi naging sanhi ng anumang pagpapalit. Bawasan nito ang oras ng pagpapatupad ng algorithm.





Kaya, ang pseudocode ng na-optimize na algorithm ng Bubble Sort ay maaaring ipahayag bilang:

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

Oras ng pagiging kumplikado at Auxiliary Space ng Bubble Sort Algorithm

Ang pinakapangit na pagkakumplikado ng oras ng Bubble Sort Algorithm ay O (n ^ 2). Ito ay nangyayari kapag ang array ay nasa pababang pagkakasunud-sunod at nais mong pag-uri-uriin ito sa pataas na pagkakasunud-sunod o kabaligtaran.





kung paano gawing transparent ang background sa ilustrador

Ang pagiging kumplikado ng oras na pinakamahusay na kaso ng Bubble Sort Algorithm ay O (n). Ito ay nangyayari kapag ang array ay naiayos na.

disk na tumatakbo sa 100 porsyentong windows 10

Kaugnay: Ano ang Big-O Notation?

Ang pagiging kumplikado ng average-case na oras ng Bubble Sort Algorithm ay O (n ^ 2). Ito ay nangyayari kapag ang mga elemento ng array ay nasa jumbled order.

Ang kinakailangang puwang ng pandiwang pantulong para sa algorithm ng Bubble Sort ay O (1).

C ++ Pagpapatupad ng Bubble Sort Algorithm

Nasa ibaba ang pagpapatupad ng C ++ ng algorithm ng 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;
}

Output:

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

Pagpapatupad ng Python ng Algorithm ng Bubble Sort

Nasa ibaba ang pagpapatupad ng Python ng algorithm ng 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)

Output:

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

Kaugnay: Paano Magamit Para sa Mga Loops sa Python

C Pagpapatupad ng Bubble Sort Algorithm

Nasa ibaba ang pagpapatupad ng C ng algorithm ng 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;
}

Output:

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

Pagpapatupad ng JavaScript ng Algorithm ng Bubble Sort

Nasa ibaba ang pagpapatupad ng JavaScript ng algorithm ng 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);

Output:

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

Ngayon Naiintindihan Mo ang Paggawa ng Bubble Sort Algorithm

Ang Bubble Sort ay ang pinakasimpleng pag-uuri ng algorithm at pangunahing ginagamit upang maunawaan ang mga pundasyon ng pag-uuri. Ang Bubble Sort ay maaari ding ipatupad nang paulit-ulit, ngunit hindi ito nagbibigay ng karagdagang mga pakinabang upang magawa ito.

Gamit ang Python, maaari mong ipatupad ang algorithm ng Bubble Sort nang madali. Kung hindi ka pamilyar sa Python at nais na simulan ang iyong paglalakbay, nagsisimula sa isang script na 'Hello World' ay isang mahusay na pagpipilian.

Magbahagi Magbahagi Mag-tweet Email Paano Magsimula Sa Python Gamit ang 'Hello World' Script

Ang Python ay isa sa pinakatanyag na mga wika sa pag-program na ginagamit ngayon. Sundin ang tutorial na ito upang makapagsimula sa iyong unang script sa Python.

Basahin Susunod
Mga Kaugnay na Paksa
  • Programming
  • Java
  • Sawa
  • Mga Tutorial sa Coding
Tungkol sa May-akda Yuvraj Chandra(60 Mga Artikulo Na-publish)

Si Yuvraj ay isang mag-aaral sa undergraduate na Computer Science sa University of Delhi, India. Masigasig siya sa Full Stack Web Development. Kapag hindi siya nagsusulat, sinisiyasat niya ang lalim ng iba't ibang mga teknolohiya.

Maaari ba akong makakuha ng wifi nang walang isang tagapagbigay ng internet?
Higit pa Mula kay Yuvraj Chandra

Mag-subscribe sa aming newsletter

Sumali sa aming newsletter para sa mga tip sa tech, pagsusuri, libreng ebook, at eksklusibong deal!

Mag-click dito upang mag-subscribe