Uvod v algoritem razvrščanja mehurčkov

Uvod v algoritem razvrščanja mehurčkov

Razvrščanje je ena najbolj osnovnih operacij, ki jih lahko uporabite za podatke. Elemente lahko razvrščate v različne programske jezike z različnimi algoritmi razvrščanja, kot so Quick Sort, Bubble Sort, Merge Sort, Insertion Sort itd. Bubble Sort je najpreprostejši algoritem med vsemi temi.





V tem članku boste izvedeli o delovanju algoritma za razvrščanje mehurčkov, psevdokodi algoritma za sortiranje mehurčkov, njegove časovno in prostorsko kompleksnost ter njegovo implementacijo v različne programske jezike, kot so C ++, Python, C in JavaScript.





Kako deluje algoritem razvrščanja mehurčkov?

Razvrstitev mehurčkov je najpreprostejši algoritem razvrščanja, ki večkrat stopi po seznamu, primerja sosednje elemente in jih zamenja, če so v napačnem vrstnem redu. Ta koncept je mogoče učinkoviteje razložiti s pomočjo primera. Razmislite o nerazvrščeni matriki z naslednjimi elementi: {16, 12, 15, 13, 19}.





Primer:

Tu primerjamo sosednje elemente in če niso v naraščajočem vrstnem redu, jih zamenjamo.



Psevdokoda algoritma za razvrščanje mehurčkov

V psevdokodi se lahko algoritem razvrščanja mehurčkov izrazi kot:

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

Zgornji algoritem obdeluje vse primerjave, tudi če je matrika že razvrščena. Lahko ga dodatno optimiziramo tako, da ustavimo algoritem, če notranja zanka ni povzročila zamenjave. To bo skrajšalo čas izvajanja algoritma.





Tako se lahko psevdokoda optimiziranega algoritma za razvrščanje mehurčkov izrazi kot:

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

Časovna zapletenost in pomožni prostor algoritma za razvrščanje mehurčkov

Najhujša časovna zapletenost algoritma za razvrščanje mehurčkov je O (n^2). Pojavi se, ko je matrika v padajočem vrstnem redu in jo želite razvrstiti po naraščajočem vrstnem redu ali obratno.





iphone 6s domači gumb ne deluje

Časovna zapletenost algoritma za razvrščanje mehurčkov v najboljšem primeru je O (n). Pojavi se, ko je matrika že razvrščena.

kako ugotoviti, kakšno vrsto matične plošče imate

Povezano: Kaj je zapis Big-O?

Povprečna časovna zapletenost algoritma za razvrščanje mehurčkov je O (n^2). Pojavi se, ko so elementi matrike v zmešanem vrstnem redu.

Pomožni prostor, potreben za algoritem razvrščanja mehurčkov, je O (1).

C ++ Izvajanje algoritma razvrščanja mehurčkov

Spodaj je izvedba algoritma razvrščanja mehurčkov v C ++:

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

Izhod:

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

Python implementacija algoritma za razvrščanje mehurčkov

Spodaj je izvedba algoritma razvrščanja mehurčkov v Pythonu:

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

Izhod:

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

Povezano: Kako uporabljati For Loops v Pythonu

C Izvajanje algoritma za razvrščanje mehurčkov

Spodaj je izvedba C algoritma za razvrščanje mehurčkov:

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

Izhod:

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

JavaScript Izvajanje algoritma razvrščanja mehurčkov

Spodaj je JavaScript implementacija algoritma za razvrščanje mehurčkov:

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

Izhod:

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

Zdaj razumete delovanje algoritma za razvrščanje mehurčkov

Razvrstitev mehurčkov je najpreprostejši algoritem razvrščanja in se uporablja predvsem za razumevanje temeljev razvrščanja. Razvrstitev mehurčkov se lahko izvede tudi rekurzivno, vendar pri tem ne prinaša dodatnih prednosti.

Z uporabo Pythona lahko z lahkoto implementirate algoritem razvrščanja mehurčkov. Če Python ne poznate in želite začeti svojo pot, je začetek s scenarijem 'Hello World' odlična izbira.

Deliti Deliti Cvrkutati E-naslov Kako začeti s Pythonom z uporabo skripta 'Hello World'

Python je danes eden najbolj priljubljenih programskih jezikov. Če želite začeti s svojim prvim skriptom Python, sledite tej vadnici.

Preberite Naprej
Sorodne teme
  • Programiranje
  • Java
  • Python
  • Vadnice za kodiranje
O avtorju Yuvraj Chandra(Objavljenih 60 člankov)

Yuvraj je dodiplomski študent računalništva na Univerzi v Delhiju v Indiji. Navdušen je nad spletnim razvojem Full Stack. Ko ne piše, raziskuje globino različnih tehnologij.

ki me išče na googlu
Več od Yuvraja Chandre

Naročite se na naše novice

Pridružite se našemu glasilu za tehnične nasvete, ocene, brezplačne e -knjige in ekskluzivne ponudbe!

Kliknite tukaj, če se želite naročiti