Write a program to sort elements of an array in ascending order using bubble sort.
#include<stdio.h> void print(int [],int); void bubble_sort(int a[],int n); int main() { int a[] = {50,20,70,10,90,100,30,60,40,80},size; size = sizeof(a)/sizeof(a[0]); print(a,size); bubble_sort(a,size); print(a,size); return 0; } void print(int a[],int n) { int i; for(i = 0;i < n;i++) printf("%d ",a[i]); printf("\n"); } void bubble_sort(int a[],int n) { int i,j,k,t; for(i = 0;i < n-1;i++) { t = 0; for(j = 0; j < n-1 - i; j++) { if(a[j] > a[j+1]) { t = 1; k = a[j]; a[j] = a[j+1]; a[j+1] = k; } } if(t == 0) { break; } } }
Output
50 20 70 10 90 100 30 60 40 80 10 20 30 40 50 60 70 80 90 100