Skip to main content

Posts

Showing posts from June, 2021

Bubble sort descending order

Bubble sort descending order   #include"stdio.h" int main() { int a[100],n,l,k,swap; printf("Enter the of Elements:"); scanf("%d",&n); for(k=0;k<n;k++) { printf("Enter a[%d] :",k); scanf("%d",&a[k]); } printf("Elements are :\n "); for(k=0;k<n;k++) { printf("%d\t",a[k]); } // sorting // for(k=0;k<n;k++) { for(l=0;l<n;l++) { if(a[l]<a[l+1]) { swap = a[l+1]; a[l+1]= a[l]; a[l]=swap; } } } printf("\nDescending order are:\n"); for(k=0;k<n;k++) { printf("%d \t",a[k]); } } Output are: Enter the of Elements:5 Enter a[0] :12 Enter a[1] :987 Enter a[2] :456 Enter a[3] :320 Enter a[4] :72 Elements are :  12     987     456     320     72 Descending order are: 987     456     320     72      12

Bubble sort ascending order

 Bubble sort ascending order #include"stdio.h" int main() { int a[100],n,l,k,swap; printf("Enter the of Elements:"); scanf("%d",&n); for(k=0;k<n;k++) { printf("Enter a[%d] :",k); scanf("%d",&a[k]); } printf("Elements are :\n "); for(k=0;k<n;k++) { printf("%d\t",a[k]); } // sorting // for(k=0;k<n;k++) { for(l=0;l<n;l++) { if(a[l]>a[l+1]) { swap = a[l+1]; a[l+1]= a[l]; a[l]=swap; } } } printf("\nAscending order are:\n"); for(k=1;k<n+1;k++) { printf("%d \t",a[k]); } } Output are: Enter the of Elements:5 Enter a[0] :12 Enter a[1] :48 Enter a[2] :75 Enter a[3] :16 Enter a[4] :30 Elements are :  12     48      75      16      30 Ascending order are: 12      16      30      48      75