C Program for Bubble Sort
C Program :
/* Aim: C Program to sort integers of an array in ascending order using bubble sort method */ #include<stdio.h> void main() { int arr[50],i,size,temp,j; printf("\n How many numbers to accept:- "); scanf("%d",&size); printf("\n Enter the array elements:- "); for(i=0;i<size;i++) scanf("%d",&arr[i]); for(i=1;i<size;i++) { for(j=0;j<size-i;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } printf("\n The array elements in ascending order:- "); for(i=0;i<size;i++) printf(" %d",arr[i]); printf("\n \n"); }
/* Output of above code:-
[root@localhost ~]# cc Bubble_Sort_ASC.c
[root@localhost ~]# ./a.out
How many numbers to accept:- 5
Enter the array elements:- 15 14 13 12 10
The array elements in ascending order:- 10 12 13 14 15
*/
Related Programs
Comments