Study Guides/Computer Science/Bubble Sort Program in C
Study Guide · Computer Science

Bubble Sort Algorithm and Program in C

In computer science, sorting means arranging data in a specific order (like ascending from smallest to largest). Bubble Sort is the simplest, most fundamental sorting algorithm taught to beginners. It gets its name because, just like air bubbles rise to the surface of water, the largest numbers 'bubble up' to the end of the array during the sorting process.

Question (Click to Flip)

What is bubble sort in C?

Answer

Bubble sort is a simple sorting algorithm that steps through an array, compares adjacent elements, and swaps them if they are in the wrong order.

Card 1 of 3 free previews

Key Facts

Algorithm Concept: Repeatedly comparing and swapping adjacent elements.

Naming: The largest elements 'bubble' to the end of the list first.

Worst Case Time Complexity: O(n²).

Best Case Time Complexity: O(n) (If the array is already sorted and an optimized flag is used).

Space Complexity: O(1) (It sorts in place, requiring no extra memory).

How Bubble Sort Works (The Logic)

The algorithm works by repeatedly stepping through the array and comparing adjacent pairs of elements.

  1. It looks at the first two elements. If the first is larger than the second, it swaps them.
  2. It moves to the next pair (2nd and 3rd elements) and does the same.
  3. By the time it reaches the end of the array during the first pass, the absolute largest number is guaranteed to be at the very end.
  4. It repeats this entire process for the remaining unsorted elements until no more swaps are needed.

C Code Example

#include <stdio.h>

void bubbleSort(int arr[], int n) {
    int i, j, temp;
    // Outer loop for the number of passes
    for (i = 0; i < n - 1; i++) {
        // Inner loop for comparing adjacent elements
        // The largest element bubbles up to the end in each pass
        for (j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap the elements
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    bubbleSort(arr, n);
    
    printf("Sorted array in ascending order: \n");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}

Time Complexity and Efficiency

While Bubble Sort is very easy to understand and code, it is highly inefficient for large datasets. Because it relies on nested loops (a loop inside a loop) to compare every element, its worst-case time complexity is O(n²). This means sorting 10,000 items could take 100 million operations!

Questions and Answers

What is bubble sort in C?+

Bubble sort is a simple sorting algorithm that steps through an array, compares adjacent elements, and swaps them if they are in the wrong order.

Why is it called bubble sort?+

It is called bubble sort because, with each pass through the array, the largest unsorted element 'bubbles up' to its correct position at the end of the array.

Is bubble sort fast?+

No, bubble sort is one of the slowest sorting algorithms with a time complexity of O(n²), making it unsuitable for large datasets.

More in Computer Science

Study Smarter with Shinyu.ai

Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast — free, no signup required.