Sissejuhatus mullide sortimise algoritmi

Sissejuhatus mullide sortimise algoritmi

Sortimine on üks põhilisemaid toiminguid, mida saate andmetele rakendada. Saate sortida elemente erinevates programmeerimiskeeltes, kasutades erinevaid sortimisalgoritme, nagu kiirsortimine, mullide sortimine, ühendamise sortimine, sisestamise sorteerimine jne. Mullide sortimine on kõigi nende seas kõige lihtsam algoritm.





Selles artiklis saate teada Bubble Sort algoritmi toimimisest, Bubble Sort algoritmi pseudokoodist, selle aja- ja ruumikompleksusest ning selle rakendamisest erinevates programmeerimiskeeltes nagu C ++, Python, C ja JavaScript.





Kuidas mullide sortimise algoritm töötab?

Mullide sorteerimine on lihtsaim sortimisalgoritm, mis loendis korduvalt läbi astub, kõrvutab külgnevaid elemente ja vahetab need, kui need on vales järjekorras. Seda mõistet saab näite abil tõhusamalt selgitada. Kaaluge sorteerimata massiivi, mis sisaldab järgmisi elemente: {16, 12, 15, 13, 19}.





Näide:

Siin võrreldakse külgnevaid elemente ja kui need ei ole kasvavas järjekorras, vahetatakse need.



Mullide sortimise algoritmi pseudokood

Pseudokoodis saab mullide sortimise algoritmi väljendada järgmiselt:

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

Ülaltoodud algoritm töötleb kõiki võrdlusi isegi siis, kui massiiv on juba sorteeritud. Seda saab veelgi optimeerida, peatades algoritmi, kui sisemine silmus ei põhjustanud vahetust. See lühendab algoritmi täitmise aega.





Seega saab optimeeritud mullide sortimise algoritmi pseudokoodi väljendada järgmiselt:

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

Mullide sortimise algoritmi ajaline keerukus ja abiruum

Mullide sortimise algoritmi halvim ajaline keerukus on O (n^2). See juhtub siis, kui massiiv on kahanevas järjekorras ja soovite sortida kasvavas järjekorras või vastupidi.





ma ei saanud oma amazoni tellimust

Mullide sortimise algoritmi parima aja aja keerukus on O (n). See juhtub siis, kui massiiv on juba sorteeritud.

pvr iptv lihtne klient m3u esitusloendi URL 2016

Seotud: Mis on Big-O märge?

Mullide sortimise algoritmi keskmine juhtumite ajaline keerukus on O (n^2). See juhtub siis, kui massiivi elemendid on segatud järjekorras.

Mullide sortimise algoritmi jaoks vajalik lisaruum on O (1).

C ++ mullide sortimise algoritmi rakendamine

Allpool on mullide sortimise algoritmi C ++ juurutamine:

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

Väljund:

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

Mullide sortimise algoritmi rakendamine Pythonis

Allpool on mullide sortimise algoritmi Pythoni rakendamine:

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

Väljund:

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

Seotud: Kuidas kasutada silmuste jaoks Pythonis

C Mullide sortimise algoritmi rakendamine

Allpool on mullide sortimise algoritmi C rakendamine:

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

Väljund:

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

Mullide sortimise algoritmi JavaScripti rakendamine

Allpool on mullide sortimise algoritmi JavaScripti rakendus:

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

Väljund:

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

Nüüd saate aru mullide sortimise algoritmi toimimisest

Bubble Sort on lihtsaim sortimisalgoritm ja seda kasutatakse peamiselt sortimise aluste mõistmiseks. Bubble Sorti saab rakendada ka rekursiivselt, kuid see ei anna sellele täiendavaid eeliseid.

Pythoni abil saate hõlpsalt rakendada mullide sortimise algoritmi. Kui te ei tunne Pythonit ja soovite oma teekonda alustada, on suurepärane valik alustada skriptiga „Tere maailm”.

Jaga Jaga Piiksuma E -post Kuidas alustada Pythoni kasutamist, kasutades skripti 'Tere maailm'

Python on tänapäeval üks populaarsemaid programmeerimiskeeli. Oma esimese Pythoni skriptiga alustamiseks järgige seda õpetust.

Loe edasi
Seotud teemad
  • Programmeerimine
  • Java
  • Python
  • Kodeerimise õpetused
Autori kohta Yuvraj Chandra(60 artiklit avaldatud)

Yuvraj on arvutiteaduse bakalaureuseõppe üliõpilane Indias Delhi ülikoolis. Ta on kirglik Full Stacki veebiarenduse vastu. Kui ta ei kirjuta, uurib ta erinevate tehnoloogiate sügavust.

kuidas teha Windows 7 käivitatavat DVD -d
Veel Yuvraj Chandrast

Telli meie uudiskiri

Liituge meie uudiskirjaga, et saada tehnilisi näpunäiteid, ülevaateid, tasuta e -raamatuid ja eksklusiivseid pakkumisi!

Tellimiseks klõpsake siin