Εισαγωγή στον αλγόριθμο ταξινόμησης φυσαλίδων

Εισαγωγή στον αλγόριθμο ταξινόμησης φυσαλίδων

Η ταξινόμηση είναι μία από τις πιο βασικές λειτουργίες που μπορείτε να εφαρμόσετε σε δεδομένα. Μπορείτε να ταξινομήσετε στοιχεία σε διαφορετικές γλώσσες προγραμματισμού χρησιμοποιώντας διάφορους αλγόριθμους ταξινόμησης όπως 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}.





Παράδειγμα:

Εδώ τα παρακείμενα στοιχεία συγκρίνονται και αν δεν είναι σε αύξουσα σειρά, αλλάζουν.



Pseευδοκώδικας του αλγορίθμου ταξινόμησης φυσαλίδων

Σε ψευδοκώδικα, ο αλγόριθμος 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

Ο παραπάνω αλγόριθμος επεξεργάζεται όλες τις συγκρίσεις ακόμη και αν ο πίνακας είναι ήδη ταξινομημένος. Μπορεί να βελτιστοποιηθεί περαιτέρω σταματώντας τον αλγόριθμο εάν ο εσωτερικός βρόχος δεν προκάλεσε ανταλλαγή. Αυτό θα μειώσει τον χρόνο εκτέλεσης του αλγορίθμου.





Έτσι, ο ψευδοκώδικας του βελτιστοποιημένου αλγορίθμου 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

Πολυπλοκότητα χρόνου και βοηθητικός χώρος του αλγορίθμου ταξινόμησης φυσαλίδων

Η χειρότερη περίπλοκη χρονική πολυπλοκότητα του αλγορίθμου ταξινόμησης φυσαλίδων είναι O (n^2). Εμφανίζεται όταν ο πίνακας είναι σε φθίνουσα σειρά και θέλετε να τον ταξινομήσετε με αύξουσα σειρά ή αντίστροφα.





πώς να παρακολουθήσετε το netflix διαδικτυακά με φίλους

Η καλύτερη χρονική πολυπλοκότητα του αλγορίθμου ταξινόμησης φυσαλίδων είναι O (n). Εμφανίζεται όταν ο πίνακας έχει ήδη ταξινομηθεί.

μπορώ να κατεβάσω βίντεο στο youtube στο iphone μου

Σχετίζεται με: Τι είναι το Big-O Notation;

Η μέση χρονική πολυπλοκότητα του αλγορίθμου ταξινόμησης φυσαλίδων είναι O (n^2). Εμφανίζεται όταν τα στοιχεία του πίνακα είναι σε ανακατεμένη σειρά.

Ο βοηθητικός χώρος που απαιτείται για τον αλγόριθμο Bubble Sort είναι O (1).

Υλοποίηση C ++ του αλγορίθμου ταξινόμησης φυσαλίδων

Παρακάτω είναι η εφαρμογή 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 του αλγορίθμου ταξινόμησης φούσκας

Παρακάτω είναι η εφαρμογή 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 Εφαρμογή του αλγορίθμου ταξινόμησης φυσαλίδων

Παρακάτω είναι η εφαρμογή 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 του αλγορίθμου ταξινόμησης φυσαλίδων

Παρακάτω είναι η υλοποίηση 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 είναι ο απλούστερος αλγόριθμος ταξινόμησης και χρησιμοποιείται κυρίως για την κατανόηση των θεμελίων της ταξινόμησης. Το Bubble Sort μπορεί επίσης να εφαρμοστεί αναδρομικά, αλλά δεν παρέχει επιπλέον πλεονεκτήματα.

Χρησιμοποιώντας την Python, μπορείτε να εφαρμόσετε τον αλγόριθμο Bubble Sort με ευκολία. Εάν δεν είστε εξοικειωμένοι με την Python και θέλετε να ξεκινήσετε το ταξίδι σας, η εκκίνηση με ένα σενάριο 'Hello World' είναι μια εξαιρετική επιλογή.

Μερίδιο Μερίδιο Τιτίβισμα ΗΛΕΚΤΡΟΝΙΚΗ ΔΙΕΥΘΥΝΣΗ Πώς να ξεκινήσετε με την Python χρησιμοποιώντας ένα σενάριο «Hello World»

Η Python είναι μία από τις πιο δημοφιλείς γλώσσες προγραμματισμού που χρησιμοποιείται σήμερα. Ακολουθήστε αυτό το σεμινάριο για να ξεκινήσετε με το πρώτο σας σενάριο Python.

Διαβάστε Επόμενο
Σχετικά θέματα
  • Προγραμματισμός
  • Ιάβα
  • Πύθων
  • Φροντιστήρια κωδικοποίησης
Σχετικά με τον Συγγραφέα Γιουβράι Τσάντρα(Δημοσιεύθηκαν 60 άρθρα)

Ο Yuvraj είναι προπτυχιακός φοιτητής Πληροφορικής στο Πανεπιστήμιο του Δελχί, Ινδία. Είναι παθιασμένος με το Full Stack Web Development. Όταν δεν γράφει, εξερευνά το βάθος διαφορετικών τεχνολογιών.

δείτε ταινίες online χωρίς εγγραφή
Περισσότερα από τον Yuvraj Chandra

Εγγραφείτε στο newsletter μας

Εγγραφείτε στο ενημερωτικό μας δελτίο για τεχνικές συμβουλές, κριτικές, δωρεάν ebooks και αποκλειστικές προσφορές!

Κάντε κλικ εδώ για εγγραφή