0
2.6kviews
Write a program to sort list elements in descending order.
1 Answer
written 8.3 years ago by |
Algorithm:
Start
Initialize array of size n and variable i, j, number and temp.
Now enter the value of numbers.
Input number.
Now enter element one by one.
For (i = 0; i < number; i++)
Input a[i]
For (i = 0; i < number; i++)
For (j = i+1; j < number; ++j)
If a[i] less than a[j].
Temp is equal to a[i].
a[i] is equal to a[j].
a[j] is equal to temp.
Print Sorted array in descending order is.
for (i = 0; i < number; i++)
input a[i]
Stop.
Program:
#include <stdio.h>
int main() {
int a[100];
int i, j, num, temp;
printf("Enter the value of number: \n");
scanf("%d", &num);
printf("Enter the elements one by one: \n");
for (i = 0; i < num; ++i) {
scanf("%d", &a[i]);
}
for (i = 0; i < num; i++) {
for (j = i+1; j < num; ++j){
if (a[i] < a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Sorted array in descending order is: \n");
for (i = 0; i < num; ++i){
printf("%d\n", a[i]);
}
}
Output:
Enter the value of number: 10
Enter the elements one by one: 1 2 3 4 5 6 7 8 9 10
Sorted array in descending order is: 10 9 8 7 6 5 4 3 2 1