0
4.1kviews
Write a program to sort given numbers in ascending 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 num.
Now enter element one by one.
For (i = 0; i < number; i++)
Input a[i]
For (i = 0; i < number; i++)
For (j = 0; j < (number - i - 1); j++)
If a[j] is greater than a[j + 1]
Temp is equal to a[j].
a[j] is equal to a[j + 1];
a[j + 1] is equal to Temp;
Print Sorted array in ascending 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 = 0; j < (num - i - 1); j++){
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}//inner for loop ends
}//outer for loop ends
printf("Sorted array in ascending order is: \n");
for (i = 0; i < num; i++){
printf("%d\n", a[i]);
}
}//main ends
Output:
Enter the value of number: 10
Enter the elements one by one: 1 5 6 4 3 2 9 8 7 10
Sorted array in ascending order is: 1 2 3 4 5 6 7 8 9 10