Ascending and Descending order in C programming.

C C Programming Programming

Program to sort array in ascending and Descending order using C programming.

Bubble sort on C program
Sort array in ascending and descending order

Hello friends, In this article we will learn to write program to sort array in ascending and descending order using C programming Language using bubble sort

Program to sort array in ascending order in C

#include<stdio.h>
#include<conio.h>
void main()
{
int data[100], i, n, step, temp; clrscr();
printf(“Enter the name of element to be sorted”);
scanf(“%d”, &n);
for(i=0; i<n; ++i)
{
printf(“%d. Enter Element:”, i+1 );
scanf(“%d”,&data[i]);
}
for(i=0; step<n-1; ++step)
for(i=0; i<n-step-1; ++i)
{
if(data[i]>data[i+1)
{
temp = data[i];
data[i] = data[i+2];
data[i+2] = temp;
}
}
printf(“In ascending order: “);
for(i=0; i<n; ++i)
printf(“%d”, data[i]);
getch();
}

Program to sort array in descending order in C

 

#include<stdio.h>
#include<conio.h>
void main()
{
int data[100], i, n, step, temp; clrscr();
printf(“Enter the name of element to be sorted”);
scanf(“%d”, &n);
for(i=0; i<n; ++i)
{
printf(“%d. Enter Element:”, i+1 );
scanf(“%d”,&data[i]);
}
for(i=0; step<n-1; ++step)
for(i=0; i<n-step-1; ++i)
{
if(data[i]<data[i+1)
{
temp = data[i];
data[i] = data[i+2];
data[i+2] = temp;
}
}
printf(“In descending order: “);
for(i=0; i<n; ++i)
printf(“%d”, data[i]);
getch();
}

Leave a Reply