Bubble Sort Algorithm
Maxwell Githinji
Posted on May 20, 2024
Sorting algorithms are important as they help the programmer to arrange or re-arrange elements depending on their desired format. There are a number of sorting algorithms, among them are:
- Bubble sort.
- Selection sort.
- Insertion sort.
- Merge sort.
- Quick sort.
- Heap sort
In this article, we are going to discuss the bubble sort algorithm. First, let's define and understand how a bubble sort algorithm works. Bubble sort algorithm just like other sorting algorithms, it's mainly concerned with organizing or re-organizing elements. Bubble sort does this by using the ideology of comparison. This would mean that its a step by step approach- for instance, to organize an array with elements "a", "b", and "c", the algorithm would compare element "a" to element "b" and depending on the programmer's desired or defined format, a swap might happen.
Below are examples of code scripts for python, javascript, and C showing how to produce the algorithm.
Bubble sort-algorithm using Python.
def bubble_sort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Track if any elements were swapped in this iteration
swapped = False
# Last i elements are already in place, so inner loop can avoid checking them
for j in range(0, n-i-1):
# Traverse the array from 0 to n-i-1
# Swap if the element found is greater than the next element
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
# If no elements were swapped in the inner loop, then the array is already sorted
if not swapped:
break
Bubble sort-algorithm using JavaScript.
function bubbleSort(arr) {
let n = arr.length;
let swapped;
// Outer loop to iterate over the entire array
for (let i = 0; i < n; i++) {
swapped = false;
// Inner loop to compare adjacent elements
for (let j = 0; j < n - i - 1; j++) {
// Swap if the element found is greater than the next element
if (arr[j] > arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped, break out of the loop
if (!swapped) {
break;
}
}
}
Bubble sort-algorithm using C.
#include <stdio.h>
// Function to implement bubble sort
void bubbleSort(int arr[], int n) {
int i, j, temp;
int swapped;
// Outer loop to iterate over the entire array
for (i = 0; i < n-1; i++) {
swapped = 0;
// Inner loop to compare adjacent elements
for (j = 0; j < n-i-1; j++) {
// Swap if the element found is greater than the next element
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = 1;
}
}
// If no elements were swapped, break out of the loop
if (swapped == 0)
break;
}
}
// Function to print an array
void printArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
// Main function to test the bubble sort algorithm
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Unsorted array: \n");
printArray(arr, n);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
If you enjoyed the read, you can follow me on:
Github: https://github.com/MuthonduG
Linkedin: https://www.linkedin.com/in/maxwell-githinji-954422258/
Posted on May 20, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.