Explanation
The program sorts an array entered by the user using the bubble sort algorithm.
Header
The code begins with including the standard input-output header file using #include <stdio.h>
.
Function Declaration
The program declares a function bubbleSort
to sort an array:
void bubbleSort(int array[], int size);
: Declares the function with an integer array and its size as parameters.
Function Definition
The program defines the bubbleSort
function:
for (int i = 0; i < size - 1; i++)
: Outer loop to iterate through each element of the array.for (int j = 0; j < size - i - 1; j++)
: Inner loop to compare and swap adjacent elements if they are in the wrong order.if (array[j] > array[j + 1])
: Checks if the current element is greater than the next element.int temp = array[j];
: Stores the current element in a temporary variabletemp
.array[j] = array[j + 1];
: Swaps the current element with the next element.array[j + 1] = temp;
: Sets the next element to the value oftemp
.
Variable Declaration
The main()
function is defined, and several variables are declared:
int size;
: Variable to hold the size of the array.int array[size];
: Array to hold the elements entered by the user.
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 of the array and its elements:
printf("Enter the size of the array: ");
: Prompts the user to enter the size of the array.scanf("%d", &size);
: Reads the size entered by the user and stores it insize
.printf("Enter the elements of the array: ");
: Prompts the user to enter the elements of the array.for (int i = 0; i < size; i++) { scanf("%d", &array[i]); }
: Reads the elements entered by the user and stores them in the arrayarray
.
Sort Array
The program calls the bubbleSort
function to sort the array:
bubbleSort(array, size);
: Calls thebubbleSort
function witharray
andsize
as arguments.
Output Result
The program prints the sorted array:
printf("Sorted array: ");
: Prints a label for the sorted array.for (int i = 0; i < size; i++) { printf("%d ", array[i]); }
: Loops through the sorted array and prints each element.printf("\n");
: Prints a newline after the sorted 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>
void bubbleSort(int array[], int size)
{
// Perform bubble sort
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (array[j] > array[j + 1])
{
// Swap array[j] and array[j + 1]
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
int main()
{
printf("\n\n Program to bubble sort an array entered by the user. \n\n");
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int array[size]; // Declare the array with user-defined size
printf("Enter the elements of the array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
bubbleSort(array, size); // Sort the array
printf("Sorted array: ");
for (int i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
Output
Program to bubble sort an array entered by the user.
Enter the size of the array: 5
Enter the elements of the array: 4 5 3 1 9
Sorted array: 1 3 4 5 9