Explanation
The program takes the size and elements of an array from the user, sorts the array in ascending order, and prints both the original and sorted arrays.
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 withd
elements to hold the array values.int min_index
: Used to store the index of the minimum element during sorting.int temp
: Used for swapping elements during sorting.
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 ind
.int e[d];
: Declares an array withd
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.
Original Array Printing
The program prints the original array:
printf("Original array: ");
: Prints a label for the original array.for (int i = 0; i < d; i++)
: Loop to print each element of the original array.printf("%d ", e[i]);
: Prints each element followed by a space.printf("\n");
: Prints a newline after printing the array.
Array Sorting
The program sorts the array in ascending order using selection sort:
for (int m = 0; m < d - 1; m++)
: Outer loop to iterate over each element of the array except the last one.int min_index = m;
: Initializes the minimum index to the current positionm
.for (int j = m + 1; j < d; j++)
: Inner loop to find the minimum element in the unsorted part of the array.if (e[j] < e[min_index])
: Checks if the current element is smaller than the current minimum.min_index = j;
: Updates the minimum index if a smaller element is found.int temp = e[m];
: Swaps the minimum element with the first element of the unsorted part.e[m] = e[min_index];
: Assigns the minimum element to the current positionm
.e[min_index] = temp;
: Assigns the original element at positionm
to the position of the minimum element.
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 take the array size and its elments from the user and sort them. 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]);
}
// Print the original array
printf("Original array: ");
for (int i = 0; i < d; i++)
{
printf("%d ", e[i]);
}
printf("\n");
// Sort the array in ascending order using a for loop
for (int m = 0; m < d - 1; m++)
{
// Find the minimum element in the unsorted part of the array
int min_index = m;
for (int j = m + 1; j < d; j++)
{
if (e[j] < e[min_index])
{
min_index = j;
}
}
// Swap the minimum element with the first element of the unsorted part
int temp = e[m];
e[m] = e[min_index];
e[min_index] = 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 take the array size and its elments from the user and sort them. Example:3, 2, 9, 1 = 1, 2, 3, 9
Enter the size of the array: 5
Enter the elements of the array:
6 3 8 7 1
Original array: 6 3 8 7 1
Sorted array: 1 3 6 7 8