Friday, 11 July 2025

Sort the array into ascending order.

Sort the array into ascending order.

Learn how to implement Bubble Sort algorithm in C

bubble_sort.c
#include <stdio.h>

int main() {
    int arr[100], n, i, j, temp;

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

    // Input elements
    printf("Enter %d elements:\n", n);
    for(i = 0; i < n; i++) {
        printf("arr[%d]: ", i);
        scanf("%d", &arr[i]);
    }

    // Sorting using Bubble Sort (Ascending Order)
    for(i = 0; i < n - 1; i++) {
        for(j = 0; j < n - i - 1; j++) {
            if(arr[j] > arr[j + 1]) {
                // Swap elements
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    // Display sorted array
    printf("\nArray in ascending order:\n");
    for(i = 0; i < n; i++) {
        printf("arr[%d] = %d\n", i, arr[i]);
    }

    return 0;
}

๐Ÿงพ Step-by-Step Explanation

๐Ÿ”น Step 1: Include Header File

#include <stdio.h>

This standard library provides:

  • printf() for output display
  • scanf() for user input
  • Essential I/O functions for console operations

๐Ÿ”น Step 2: Declare Variables and Array

int arr[100], n, i, j, temp;
  • arr[100]: Array with capacity for 100 integers
  • n: Actual number of elements to sort
  • i, j: Loop counters for sorting passes
  • temp: Temporary variable for swapping elements

๐Ÿ”น Step 3: Input Size and Elements

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

for(i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
}
  • First gets the array size (n ≤ 100)
  • Then collects each element from user
  • Stores elements in sequential array positions
  • Uses formatted input with index display

๐Ÿ”น Step 4: Sort the Array (Bubble Sort)

for(i = 0; i < n - 1; i++) {
    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;
        }
    }
}

Bubble Sort implementation:

  • Outer loop: Controls number of passes (n-1 passes needed)
  • Inner loop: Compares adjacent elements in each pass
  • Comparison: Checks if elements are in wrong order
  • Swapping: Uses temp variable to exchange elements
  • With each pass, largest unsorted element "bubbles up" to correct position

๐Ÿ”น Step 5: Display Sorted Array

for(i = 0; i < n; i++) {
    printf("arr[%d] = %d\n", i, arr[i]);
}
  • Prints all elements in sorted order
  • Shows both index and value for each element
  • Verifies the successful sorting
  • Uses newline character for clean output

๐Ÿงช Sample Output

Enter the number of elements: 5
Enter 5 elements:
arr[0]: 30
arr[1]: 10
arr[2]: 50
arr[3]: 20
arr[4]: 40

Array in ascending order:
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50

๐Ÿ“Œ Key Concepts Covered

Bubble Sort Algorithm
A simple comparison-based sorting method with O(n²) time complexity
Array Manipulation
Storing and processing multiple values in a linear data structure
Nested Loops
Using one loop inside another to implement sorting passes
Element Swapping
Temporary variable technique for exchanging two values
Ascending Order
Arranging elements from smallest to largest value

No comments:

Post a Comment

Total Pageviews

Search This Blog

Write a program which performs the following operations using a simple queue. : insert() -> delete() -> display()

Write a program which performs the following operations using a simple queue. : insert() -> delete() -> display() ...