Bubble Sort

#include <stdio.h> #include <stdlib.h> void bubble(int *arr,int size); void swap(int *int1, int *int2); void print(int *ptr,int size); void bubble(int *arr,int size){ int i,j; for (i=0;i<size;i++){ for (j=0;j<size-1-i;j++){ if (*(arr+j) > *(arr+j+1) ) swap((arr+j), (arr+j+1)); printf("\n i =%d ; j = %d ", i, j); print(arr,5); } } } void swap(int *int1, int *int2){ int temp; temp=*int1; *int1=*int2; *int2=temp; } void print(int *ptr,int size){ int i; printf("\n"); for (i=0;i<size;i++){ printf(" %d ",*(ptr+i) ); } } int main(){ int arr[5] = {20,100,30,50,40}; unsigned short int iter; bubble(arr,sizeof(arr)/sizeof(int)); // for (iter=0; iter<5; iter++) // printf(" %d ", arr[iter]); }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.