Algorithm: Bubble Sort
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/*bubble sort on array by codingstreet.com */ #include<stdio.h> void swap(int *p,int *q){ int t; t=*p; *p=*q; *q=t; } void bubblesort(int A[],int size){ int i,j; for(i=0;i<size;i++){ for(j=size-1;j>=i+1;j--){ if(A[j]<A[j-1]) swap(&A[j],&A[j-1]); } } } int main(){ int A[]={9,8,7,6,5,4},i; bubblesort(A,6); for(i=0;i<6;i++) printf(" %d ",A[i]); return 0; } |