You are currently viewing Ascending and Descending order in C programming.

Ascending and Descending order in C programming.

Spread the love

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

Spread the love

Santosh Adhikari

Hello, it's me Santosh Adhikari, from Kathmandu, Nepal. I'm a student of Science and Technology. I love new challenges and want to explore more on Software Quality Assurance. I want to be a professional SQA. I'm also interested in Blogging, SEO activities.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments