Wednesday, April 30, 2025

C Program - Bubble Sort Algorithm using Function in C

This is a C program that sorts an array of integers in ascending order using the Bubble Sort algorithm. 

#include <stdio.h>

int main()
{

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

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

    printf("Sorted Array: \n");
    for (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.031 s
Press any key to continue.

Here's a detailed explanation of each line of the code: 

#include <stdio.h>

This line includes the standard input-output header file in the program. 

int main()
{

This is the main function of the program. 

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

This declares an array of integers with 10 elements and initializes it with some values. 

    int num = 10;

This sets the value of num to 10, which is the number of elements in the array. 

    int x, y;

These are integer variables used as loop counters. 

    int temp;

This is a temporary integer variable used for swapping the values of array elements during the sorting process. 

    for (x = 0; x < num - 1; x++)

This is the outer loop that iterates through the array elements from the first element to the second last element. 

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

This is the inner loop that iterates through the array elements from the first element to the second last element of the unsorted portion of the array. It compares each adjacent pair of elements and swaps them if they are not in the correct order (i.e., the first element is greater than the second element). 

    printf("Sorted Array: \n");
    for (x = 0; x < num; x++)
    {
        printf("%d ", arr[x]);
    }

This prints the sorted array elements in ascending order. 

    return 0;
}

This statement ends the program and returns 0 to the operating system to indicate successful execution of the program.

Bubble Sort using Functions: 

#include <stdio.h> //include standard input-output library

void bubble_sort(int arr[], int num) //declare a function named bubble_sort with parameters: array of integers and size of the array
{
    int x, y; //declare two integer variables to be used in loops
    int temp; //declare a temporary variable to be used for swapping elements in the array

    for (x = 0; x < num - 1; x++) //outer loop for iterating over all elements in the array except the last one
    {
        for (y = 0; y < num - x - 1; y++) //inner loop for comparing adjacent elements in the array
        {
            if (arr[y] > arr[y + 1]) //check if the current element is greater than the next element
            {
                temp = arr[y]; //swap elements if the condition is true using a temporary variable
                arr[y] = arr[y + 1];
                arr[y + 1] = temp;
            }
        }
    }

    printf("Sorted Array: \n"); //print a message indicating that the array is sorted
    for (x = 0; x < num; x++) //iterate over all elements in the sorted array
    {
        printf("%d ", arr[x]); //print each element of the sorted array
    }
}

int main() //start of the main function
{

    int arr[] = {2, 4, 3, 1, 6, 5, 8, 10, 7, 9}; //initialize an array of integers
    int num = sizeof(arr)/sizeof(arr[0]); //get the number of elements in the array

    bubble_sort(arr, num); //call the bubble_sort function with the array and its size as arguments

    return 0; //return 0 to indicate successful program execution
}

Result: 

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

INDEX

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 .  ...