Sort an Array Elements Ascending Order in C

Advertisements

Prev Tutorial Next Tutorial

Sort an Array Elements in Ascending Order Program in C

Sort array elements means arrange elements of array in Ascending Order and Descending Order. we need to sort the given array in ascending order such that elements will be arranged from smallest to largest. This can be achieved through two loops. The outer loop will select an element, and inner loop allows us to compare selected element with rest of the elements.

Algorithm Sort an Array Elements in Ascending Order

  • Declare and initialize an array.
  • Loop through the array and select an element.
  • The inner loop will be used to compare the selected element from the outer loop with the rest of the elements of the array.
  • If any element is less than the selected element then swap the values.
  • Continue this process till entire array is sorted in ascending order.

Different Ways to Sort Array Elements

There are literally hundreds of different ways to sort arrays. Below are the top ways to sort an array element in ascending or descending order.

Sort an Array Elements in Ascending Orde Program in C

#include<stdio.h>
#include<conio.h>

void main()
{
int i,a[10],temp,j;
clrscr();
printf("Enter any 10 num in array = \n");
for(i=0;i<=10;i++)
{
scanf("%d",&a[i]);
}
printf("\n\nData before sorting  = ");
for(j=0;j<10;j++)
{
printf("  %d",a[j]);
}
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n\n\nData after sorting   = ");
for(j=0;j<10;j++)
{
printf("  %d", a[j]);
}
getch();
}

Output

sort array elements
Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements