C Program to Sort an Array Using Bubble Sort

C Program to Sort an Array Using Bubble Sort

Sorting is one of the most fundamental tasks in programming. It allows you to arrange data in a specific order, such as ascending or descending, which is essential for searching, organizing, and analyzing data efficiently. In C programming, one of the simplest sorting algorithms to learn is bubble sort. Understanding bubble sort will help you grasp the concepts of loops, comparisons, and swapping elements, which are core concepts in array manipulation.

Bubble sort works by repeatedly comparing adjacent elements in an array and swapping them if they are in the wrong order. This process continues until the entire array is sorted. Although bubble sort is not the most efficient algorithm for large datasets, it is an excellent way for beginners to understand the mechanics of sorting in C.

Sorting an Array Using Bubble Sort

In this example, we will create a C program that sorts an array of integers in ascending order using bubble sort. The program will explain each step clearly to help you understand how the algorithm works.

#include <stdio.h>

int main() {

    int n;

    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);

    int arr[50];

    printf("Enter %d elements: ", n);

    for(int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Bubble sort algorithm
    for(int i = 0; i < n - 1; i++) {

        for(int j = 0; j < n - i - 1; j++) {

            if(arr[j] > arr[j + 1]) {

                // Swap elements if they are in the wrong order
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;

            }

        }

    }

    printf("Sorted array in ascending order: ");

    for(int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    return 0;

}

In this program, we first take the size of the array and its elements from the user. The bubble sort algorithm uses two nested loops. The outer loop controls the number of passes over the array, while the inner loop compares adjacent elements. If an element is greater than the next element, we swap them using a temporary variable. This ensures that the largest unsorted element “bubbles up” to its correct position after each pass.

The Logic Behind Bubble Sort

Bubble sort repeatedly compares adjacent pairs of elements in the array. During each pass, if the left element is greater than the right, the elements are swapped. The largest element moves to the end of the array after the first pass. The process continues with each subsequent pass, ignoring the already sorted elements at the end. This simple method makes bubble sort easy to understand, but it can become slow for large arrays because it requires multiple passes through the data.

FAQs

Q1: Can bubble sort sort arrays in descending order?
Yes, by changing the comparison condition in the inner loop from arr[j] > arr[j + 1] to arr[j] < arr[j + 1], the algorithm will sort the array in descending order.

Q2: Is bubble sort efficient for large arrays?
No, bubble sort has a time complexity of O(n²), making it inefficient for large datasets. More advanced algorithms like quicksort or mergesort are preferred for larger arrays.

Q3: Can bubble sort handle arrays with duplicate elements?
Yes, bubble sort can sort arrays with duplicate elements without any issue. Duplicate values will simply remain in their relative positions after sorting.

Conclusion

Bubble sort is a simple and effective way to understand the mechanics of sorting in C. While it may not be suitable for very large datasets, it teaches fundamental programming concepts such as loops, comparisons, and element swapping. Practicing bubble sort helps beginners gain confidence in array manipulation and prepares them for more advanced sorting algorithms.

References & Additional Resources

  1. Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language. 2nd Edition, Prentice Hall, 1988.
  2. GeeksforGeeks: Bubble Sort Algorithm – Step-by-step bubble sort guide.
  3. TutorialsPoint: C Arrays – Detailed explanations of array operations.
  4. Programiz: C Programming Examples – Beginner-friendly C programming examples.
Scroll to Top