Program 38 - CSU1128 - CSE 2026 - Shoolini University

Program to apply bubble sort on the array entered by the user. Example:3, 2, 9, 1 = 1, 2, 3, 9

Explanation

The program applies bubble sort on an array entered by the user, sorting it in ascending order.

Header

The code begins with including the standard input-output header file using #include <stdio.h>.

Variable Declaration

The main() function is defined, and several variables are declared:

  • int d: Stores the size of the array.
  • int e[d]: Declares an array of integers with d elements to hold the array values.

Program Description

The program prints a description message for the user to understand its functionality.

User Input

The user is prompted to enter the size and elements of the array:

  • printf("Enter the size of the array: ");: Prompts the user to enter the size of the array.
  • scanf("%d", &d);: Reads the size of the array and stores it in d.
  • int e[d];: Declares an array with d elements.
  • printf("Enter the elements of the array:\n");: Prompts the user to enter the elements of the array.
  • for (int i = 0; i < d; i++): Loop to read each element of the array.
  • scanf("%d", &e[i]);: Reads each element and stores it in the array.

Bubble Sort Implementation

The program sorts the array in ascending order using bubble sort:

  • for (int m = 0; m < d - 1; m++): Outer loop to iterate over each element of the array except the last one.
  • for (int j = 0; j < d - m - 1; j++): Inner loop to compare and swap adjacent elements.
  • if (e[j] > e[j + 1]): Checks if the current element is greater than the next element.
  • int temp = e[j];: Swaps the elements if they are in the wrong order.
  • e[j] = e[j + 1];: Assigns the next element to the current position.
  • e[j + 1] = temp;: Assigns the current element to the next position.

Sorted Array Printing

The program prints the sorted array:

  • printf("Sorted array: ");: Prints a label for the sorted array.
  • for (int i = 0; i < d; i++): Loop to print each element of the sorted array.
  • printf("%d ", e[i]);: Prints each element followed by a space.
  • printf("\n");: Prints a newline after printing the array.

Program End

The program ends with return 0; to indicate successful execution.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
int main()
{
    printf("\n\n Program to apply bubble sort on the array entered by the user. Example:3, 2, 9, 1 = 1, 2, 3, 9\n\n");

    // Read the size of the array from the user
    printf("Enter the size of the array: ");
    int d;
    scanf("%d", &d);

    // Declare an array of integers with d elements
    int e[d];

    // Read the elements of the array from the user
    printf("Enter the elements of the array:\n");
    for (int i = 0; i < d; i++)
    {
        scanf("%d", &e[i]);
    }

    // Implement bubble sort
    for (int m = 0; m < d - 1; m++)
    {
        for (int j = 0; j < d - m - 1; j++)
        {
            if (e[j] > e[j + 1])
            {
                // Swap the elements if they are in the wrong order
                int temp = e[j];
                e[j] = e[j + 1];
                e[j + 1] = temp;
            }
        }
    }

    // Print the sorted array
    printf("Sorted array: ");
    for (int i = 0; i < d; i++)
    {
        printf("%d ", e[i]);
    }
    printf("\n");

    return 0;
}
                    
                

Output

Program to apply bubble sort on the array entered by the user. Example:3, 2, 9, 1 = 1, 2, 3, 9

Enter the size of the array: 5
Enter the elements of the array: 1 7 4 9 3
Sorted array: 1 3 4 7 9