This article
will show how to sort array elements with C and C++ program. The most popular Bubble
Sorting algorithm is used in this program. The following program will first
store inputs into array with the scanf() function and then Bubble sort
algorithm is applied to sort the array elements. The complete programming code
is given below.
Array Sorting Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],N;
clrscr();
printf("How many array elements:");
scanf("%d",&N);
printf("Enter elements:\n");
// Storing elements into an array
for(int i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
// Showing unsorted array elements
printf("Array is :");
for(i=0;i<N;i++)
{
printf("%d ",a[i]);
}
//sorting array elements with Bubble sort algorithm
for(i=0;i<N;i++)
{
int ptr=0;
while(ptr<N-1-i)
{
if(a[ptr]>a[ptr+1])
{
int temp=a[ptr];
a[ptr]=a[ptr+1];
a[ptr+1]=temp;
}
ptr=ptr+1;
}
}
// Showing sorted array elements
printf("\nSorted array :");
for(i=0;i<N;i++)
{
printf("%d ",a[i]);
}
getch ();
}
Output
