Wednesday, April 30, 2025

C Program - Bubble Sort In C using Nested While Loops

This code sorts an array of integers using the Bubble Sort algorithm and prints the sorted array to the console.

The array to be sorted is initialized at the beginning of the program, and the main() function contains a nested while loop that implements the Bubble Sort algorithm.

The algorithm works by repeatedly swapping adjacent elements if they are in the wrong order, and it continues doing this until the array is fully sorted.

Once the array is sorted, the program uses a for loop to iterate through the sorted array and print each element to the console. The program then returns 0 to indicate successful program execution. 

#include <stdio.h>

int main()
{

    int arr[] = {2, 4, 3, 1, 6, 5, 8, 10, 7, 9};
    int num = 10;
    int x = 0;
    int temp;

    while (x < num - 1)
    {
        int y = 0;
        while (y < num - 1 - x)
        {
            if (arr[y] > arr[y + 1])
            {
                temp = arr[y]; //helper variable
                arr[y] = arr[y + 1];
                arr[y + 1] = temp;
            }
            y++;
        }
        x++;
    }

    printf("Sorted Array: \n");

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

    return 0;
}

Result: 

Sorted Array:
1 2 3 4 5 6 7 8 9 10
Process returned 0 (0x0)   execution time : 0.035 s
Press any key to continue.

Here's a line-by-line explanation of the program: 

#include <stdio.h>

int main()
{

The program starts with the standard main() function and includes the standard library header stdio.h

    int arr[] = {2, 4, 3, 1, 6, 5, 8, 10, 7, 9};
    int num = 10;
    int x = 0;
    int temp;

The program initializes an integer array arr with 10 integers and initializes integer variables num, x, and temp

    while (x < num - 1)
    {
        int y = 0;
        while (y < num - 1 - x)
        {
            if (arr[y] > arr[y + 1])
            {
                temp = arr[y];
                arr[y] = arr[y + 1];
                arr[y + 1] = temp;
            }
            y++;
        }
        x++;
    }

The program uses a nested while loop to implement the Bubble Sort algorithm. The outer loop executes num - 1 times and the inner loop executes num - 1 - x times. The variable y is used to iterate through the array. Inside the inner loop, the algorithm compares the current element with the next element of the array. If the current element is greater than the next element, they are swapped using a helper variable temp

    printf("Sorted Array: \n");

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

    return 0;
}

Finally, the program prints the sorted array using a for loop and returns 0 to indicate successful program execution.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...