Bubble Sort


DESCRIPTION

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity is quite high.

In Bubble Sort algorithm:

  • Traverse from left and compare adjacent elements and the higher one is placed at right side.
  • In this way, the largest element is moved to the rightmost end at first.
  • This process is then continued to find the second largest and place it and so on until the data is sorted.
  • Total no. of passes: n-1
  • Total no. of comparisons: n*(n-1)/2
Advantages Disadvantages
  • Simple to understand and implement.
  • No additional memory space required.
  • Stable sorting algorithm, preserving the relative order of elements with equal key values.
  • Time complexity of O(n2), making it inefficient for large datasets.
  • As a comparison-based algorithm, its efficiency can be limited by the need for a comparison operator to determine element order.

COMPLEXITY

Worst Case O(n2)
Best Case O(n)
Average Case O(n2)
Space Complexity O(1)

IMPLEMENTATIONS

void swap(int* xp, int* yp) {
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}

void bubbleSort(int arr[], int n) {
    int i, j;
    bool swapped;

    for (i = 0; i < n - 1; i++) {
        swapped = false;
        for (j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                swap(&arr[j], &arr[j + 1]);
                swapped = true;
            }
        }

        if (swapped == false)
            break;
    }
}
                        
void bubbleSort(int arr[], int n) {
    int i, j;
    bool swapped;
    for (i = 0; i < n - 1; i++) {
        swapped = false;
        for (j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                swap(arr[j], arr[j + 1]);
                swapped = true;
            }
        }

        if (swapped == false)
            break;
    }
}
                        
void bubbleSort(int arr[], int n) {
    int i, j, temp;
    boolean swapped;
    
    for (i = 0; i < n - 1; i++) {
        swapped = false;
        for (j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = true;
            }
        }

        if (swapped == false)
            break;
    }
}
                        
static void bubbleSort(int[] arr, int n) {
    int i, j, temp;
    bool swapped;
    for (i = 0; i < n - 1; i++) {
        swapped = false;
        for (j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = true;
            }
        }

        if (swapped == false)
            break;
    }
}
                        
def bubbleSort(arr):
    n = len(arr)

    for i in range(n):
        swapped = False

        for j in range(0, n-i-1):
                            
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                swapped = True
        if (swapped == False):
            break
                        
function bubbleSort(&$arr) {
    $n = sizeof($arr);

    for($i = 0; $i < $n; $i++) {
        $swapped = False;

        for ($j = 0; $j < $n - $i - 1; $j++) {
            
            if ($arr[$j] > $arr[$j+1]) {
                $t = $arr[$j];
                $arr[$j] = $arr[$j+1];
                $arr[$j+1] = $t;
                $swapped = True;
            }
        }

        if ($swapped == False)
            break;
    }
}