Table of Contents
Bubble Sort Algorithms
Bubble sort is a simple sorting algorithm that compares pairs of elements and swaps them if they are in the wrong order. It repeats this process until the list is sorted.
Here is a more detailed explanation of how the bubble sort algorithm works:
- The algorithm begins by comparing the first two elements of the list. If the first element is larger than the second element, they are swapped.
- The algorithm then moves on to the next pair of elements (the second and third elements) and repeats the process. If the second element is larger than the third element, they are swapped.
- This process is repeated for all pairs of elements in the list until the end of the list is reached. At this point, the largest element in the list will have “bubbled” up to the end of the list.
- The algorithm then starts over again from the beginning of the list, repeating the process until no more swaps are necessary.
- When no more swaps are necessary, the list is considered to be sorted.
Bubble Sort pseudo Code :
procedure bubbleSort(A: list of sortable items) n = length(A) repeat swapped = false for i = 1 to n-1 inclusive do if A[i-1] > A[i] then swap(A[i-1], A[i]) swapped = true end if end for until not swapped end procedure
Example
bubble sort algorithm would work on a list of integers:
[5, 1, 4, 2, 8]
First pass:
[1, 4, 2, 5, 8] (5 and 1 swapped)
[1, 2, 4, 5, 8] (4 and 2 swapped)
[1, 2, 4, 5, 8] (no swap)
[1, 2, 4, 5, 8] (no swap)
Second pass:
[1, 2, 4, 5, 8] (no swap)
[1, 2, 4, 5, 8] (no swap)
[1, 2, 4, 5, 8] (no swap)
Sorted list: [1, 2, 4, 5, 8]
Code in multiple languages:
public void bubbleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j arr[j + 1]) { // swap arr[j+1] and arr[j] int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } }
def bubble_sort(arr): n = len(arr) for i in range(n - 1): for j in range(n - i - 1): if arr[j] > arr[j + 1]: # swap arr[j+1] and arr[j] arr[j], arr[j + 1] = arr[j + 1], arr[j]
public void BubbleSort(int[] arr) { int n = arr.Length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j arr[j + 1]) { // swap arr[j+1] and arr[j] int temp = arr[j]; arr[j]
#include using namespace std; void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j arr[j + 1]) { // swap arr[j+1] and arr[j] int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { int arr[] = {5, 1, 4, 2, 8}; int n = sizeof(arr) / sizeof(arr[0]); bubbleSort(arr, n); for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; }
function bubbleSort(arr) { for (let i = 0; i < arr.length - 1; i++) { for (let j = 0; j arr[j + 1]) { // swap arr[j+1] and arr[j] let temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } let arr = [5, 1, 4, 2, 8]; bubbleSort(arr); console.log(arr); // [1, 2, 4, 5, 8]
function bubbleSort($arr) { $n = count($arr); for ($i = 0; $i < $n - 1; $i++) { for ($j = 0; $j $arr[$j + 1]) { // swap $arr[$j+1] and $arr[$j] $temp = $arr[$j]; $arr[$j] = $arr[$j + 1]; $arr[$j + 1] = $temp; } } } } $arr = [5, 1, 4, 2, 8]; bubbleSort($arr); print_r($arr); // [1, 2, 4, 5, 8]
This Post Has 6 Comments