You can easily understand the working from image below given
Image Credits: codingeek.com |
Program:
//a program to sort given 10 numbers in ascending order by SlashMyCode.com
#include<stdio.h>
#include<conio.h>
#include<conio.h>
void main()
{
int i,j,temp,a[10];
clrscr();
printf("Enter 10 integer numbers: \n");
for(i=0;i<10;i++) //for loop to accept 10 numbers
{
scanf("%d",&a[i]);
}
{
int i,j,temp,a[10];
clrscr();
printf("Enter 10 integer numbers: \n");
for(i=0;i<10;i++) //for loop to accept 10 numbers
{
scanf("%d",&a[i]);
}
//sorting the numbers
for (i=0;i<10;i++)
{
for(j=i+1;j<10;j++) /*j=i+1 since we want to compare first number with next number.*/
{
if(a[i]>a[j]) /*if loop to swap the numbers if first number is greater than second, we use 3rd variable (temp) for swapping */
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
for (i=0;i<10;i++)
{
for(j=i+1;j<10;j++) /*j=i+1 since we want to compare first number with next number.*/
{
if(a[i]>a[j]) /*if loop to swap the numbers if first number is greater than second, we use 3rd variable (temp) for swapping */
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf("\n\nThe 10 numbers sorted in ascending order are: \n");
for(i=0;i<10;i++) // To print sorted numbers
{
printf("%d ",a[i]);
}
//a program to sort given 10 numbers in ascending order by SlashMyCode.com
getch();
}
for(i=0;i<10;i++) // To print sorted numbers
{
printf("%d ",a[i]);
}
//a program to sort given 10 numbers in ascending order by SlashMyCode.com
getch();
}
Output:
Flowchart:
No comments:
Post a Comment
Feel free to ask us any question regarding this post