How to write a C Program To Sort An Array In Ascending And Descending Order in C Programming Language ?
Solution For C Program :
/*C Program To Sort An Array In Ascending And Descending Order.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,k,temp=0;
clrscr();
printf("How many numbers you want to store:=");
scanf("%d",&k);
printf("Enter numbers for in an array:=");
for(i=0;i<k;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<k;i++)
{
for(j=1;j<k;j++)
{
if(a[j]>a[j-1]) //NOTE: we can simply replace '<' by '>' for ascending order.
{
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
}
}
}
printf("The sorted array in descending order is:=\n");
for(i=0;i<k;i++)
{
printf("\t%d",a[i]);
}
printf("\n\nThe sorted array in ascending order is:=\n");
for(i=k-1;i>=0;i--)
{
printf("\t%d",a[i]);
}
getch();
}
You may also learn these C Program/Code :
C Program To Swap Two Numbers Without Using Third Variable
Learn More :
Array
- How to pass one dimensional array to function in c.
- Write a c program which passes two dimension array to function.
- C Program Array Index Example
- C Program Array Example: Average
- C Program Array Example: Reverse
- C Program Array Example
- C Program to Array
- C Program Array NxM Elements Geometric/Arithmetic
- C Program To Find The Maximum And Minimum Value In An Array
- C Program To Find Union & Intersection Of Two Array
- C Program To Search A Number Inside The Array
- C Program To Sort An Array Of Names In Alphabetical And Reverse Order
- C Program To Copy One Character Array Into Another
- Returns: array of decoded values. [0] - count of values
- C Program to Check if a number is in an array[1000]
- C Program to Find max and min in array using pointer concept
- C Program Sort Array By Segment
- C Program to sort an array using bubble sort
- C Program to find smallest in an array using pointer
- C Program to merge and sort two arrays
- Un-sortiertes Array and Sortiertes Array
- C Program to Array Deserialization
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- Input reads in the array positions and range for random generation.
Sort
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program Sort Example
- C Program Shell Sort Using C Programming Language
- C Program Selection Sort Using C Programming Language
- C Program to Bubble Sort Using C Programming Language
- C Program Insertion Sort Using C Programming Language
- Heap Sort Using C Programming Language
- C Program To Sort An Array Of Names In Alphabetical And Reverse Order
- C Program Sort Array By Segment
- C Program to sort an array using bubble sort
- C Program to merge and sort two arrays
- Un-sortiertes Array and Sortiertes Array
- matrix sort in C Program
- C program that receives 10 float numbers from the console and sort them in non-ascending order, and prints the result
- C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order
- C Program to accept n numbers from user,store these numbers into an array & sort the number of an array
- C Program to Implement Quick Sort
- QuickVSInsertion Sorting C Program
Decending order