Sort array in ascending order
#include<stdio.h>
#include<conio.h>
void main(){
int arr[100],i,j,element,no,temp;
clrscr();
printf("\nEnter the no of Elements: ");
scanf("%d", &no);
for(i=0;i<no;i++){
printf("\n Enter Element %d: ", i+1);
scanf("%d",&arr[i]);
}
for(i=0;i<no;i++){
for(j=i;j<no;j++){
if(arr[i] > arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\nSorted array:");
for(i=0;i<no;i++){
printf("\t%d",arr[i]);
}
getch();
}
Output:
Enter the no of Elements: 5
Enter Element 1: 2
Enter Element 2: 5
Enter Element 3: 2
Enter Element 4: 5
Enter Element 5: 3
Sorted array: 2 2 3 5 5
Follow Us On Facebook !!!
No comments:
Post a Comment