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

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

/* 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

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;
		}
		
	}
}

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(Array[0] > Array[1]) = 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.

Second For Loop – 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.

Second For Loop – Fourth Iteration: for(j = 4; 4 < 4; 4++)
Condition inside the For Loop 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 the For Loop is True so, compiler will enter into the If Statement: if(Array[1] > Array[2])
if(25 > 19) – It means, Condition is True
temp = 25
Array[i] = 19
Array[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 the For Loop 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 array elements in ascending order using Functions.

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

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

int main()
{
	int Array[50], i, j, Size;
	int *arr;
	
	printf("\nPlease Enter th 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]);
    }  
	
	arr = Sort_ArrayAsc(Array, Size);   

	printf("\n **** Array of Elemenst in Ascending Order are : ****\n");
	for (i = 0; i < Size; i++)
	{
		printf(" Element at Array[%d] = %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;	
}

C Sort Array in Ascending Order output

Please Enter th Number of elements in an array  :  5

Please Enter 5 elements of an Array 
25 89 4 12 15

 **** Array of Elemenst in Ascending Order are : ****
 Element at Array[0] = 4 
 Element at Array[1] = 12 
 Element at Array[2] = 15 
 Element at Array[3] = 25 
 Element at Array[4] = 89 

Comments are closed.