C Program to Sort Array in Ascending Order

How to write a C Program to Sort Array in Ascending Order using For Loop, and Functions with example.

C Program to Sort Array in Ascending Order

This program for sorting array in c allows the user to enter the Size and the row elements of One Dimensional.

Next, we are using Nested For Loop to sort the array elements in ascending order in C and print all the elements in it.

/* C Program to Sort Array in Ascending Order */
#include <stdio.h>

int main()
{
	int Array[50], i, j, temp, Size;
	
	printf("\nPlease Enter the Number of elements in an array  :  ");
	scanf("%d", &Size);
	
	printf("\nPlease Enter %d elements of an Array \n", Size);
	for (i = 0; i < Size; i++)
	{
		scanf("%d", &Array[i]);
    }     
	for (i = 0; i < Size; i++)
	{
		for (j = i + 1; j < Size; j++)
		{
			if(Array[i] > Array[j])
			{
				temp = Array[i];
				Array[i] = Array[j];
				Array[j] = temp;
			}
			
		}
	}
	printf("\n **** Array of Elemenst in Ascending Order are : ****\n");
	for (i = 0; i < Size; i++)
	{
		printf("%d\t", Array[i]);
	}
	
	return 0;
}
C Program to Sort Array in Ascending Order 1

In this C array sort program, For Loop will make sure that the number is between 0 and maximum size value. In this example, it will be from 0 to 3

Let me show you the Nested For Loop in the sorting of an array in c program in Iteration wise

First For Loop – First Iteration: for(i = 0; 0 < 4; 0++)
The condition is True so, it will enter into second for loop

Second For Loop – First Iteration: for(j = 0 + 1; 1 < 4; 1++)
Condition inside the For Loop is True so, compiler will enter into the If Statement
if(19 > 25)
It means, Condition is False so, it will exit from If block, and j value will be incremented by 1.

Second For Loop – Second Iteration: for(j = 2; 2 < 4; 2++)

Condition inside the For Loop is True. So, the C Programming compiler will enter into the If Statement

if(19 > 4) – It means, Condition is True
temp = 19
Array[i] = 4
Array[j] = 19

Now the Array will be 4 25 19 21. Next, j will be increment by 1.

Third Iteration: for(j = 3; 3 < 4; 3++)
Condition inside the For Loop is True so, the compiler will enter into the If Statement: if(Array[0] > Array[3])

if(4 > 21) – It means Condition is False. So, it will exit from the If block, and j value will increment by 1.

Fourth Iteration: for(j = 4; 4 < 4; 4++)
Condition inside it is False. So, the compiler will exit from the For Loop. Next, i value incremented by 1.

First For Loop – Second Iteration: for(i = 1; 1 < 4; 1++)
The condition is True so, it will enter into second for loop

Second For Loop – First Iteration: for(j = 1 + 1; 2 < 4; 2++)
Condition inside it is True so, compiler will enter into the If Statement:
if(25 > 19) – It means, Condition is True
temp = 25
[i] = 19
[j] = 25
Now the array will be  4 19 25 21. Next, j will be increment by 1.

Second For Loop – Second Iteration: for(j = 3; 3 < 4; 3++)
Condition inside it is True so, the compiler will enter into the If Statement: if(Array[1] > Array[3])

if(19 > 21) – It means, Condition is False so, it will exit from the If block, and the Second For Loop.

Do the same for the remaining Iterations of the program for sorting array in C

C Program to Sort Array in Ascending Order using Functions

This program for sort array in C is the same as the first example. However, we separated the logic to sort elements in ascending order using Functions.

#include <stdio.h>

int *Sort_ArrayAsc(int arr[], int Size);

int main()
{
	int Array[50], i, j, Size;
	int *arr;
	
	printf("\nPlease Enter the Number of elements  :  ");
	scanf("%d", &Size);
	
	printf("\nPlease Enter %d elements \n", Size);
	for (i = 0; i < Size; i++)
	{
		scanf("%d", &Array[i]);
    }  
	
	arr = Sort_ArrayAsc(Array, Size);   

	printf("\n **** Array of Elements in Ascending Order are : ****\n");
	for (i = 0; i < Size; i++)
	{
		printf("%d \n", i, arr[i]);
	}	
	return 0;
}

int *Sort_ArrayAsc(int arr[], int Size)
{
	int i, j, temp;
	
	for (i = 0; i < Size; i++)
	{
		for (j = i + 1; j < Size; j++)
		{
			if(arr[i] > arr[j])
			{
				temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}			
		}
	}
	return arr;	
}
Please Enter the Number of elements  :  5

Please Enter 5 elements
25 89 4 12 15

 **** Array of Elements in Ascending Order are : ****
4 
12 
15 
25 
89 

Comments are closed.