diff --git a/bubble_sort b/bubble_sort index 0f57f61..32ef6ad 100644 --- a/bubble_sort +++ b/bubble_sort @@ -1,36 +1,41 @@ +#include +using namespace std; + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + + +void bubbleSort(int arr[], int n) +{ + int i, j; + for (i = 0; i < n-1; i++) + + + for (j = 0; j < n-i-1; j++) + if (arr[j] > arr[j+1]) + swap(&arr[j], &arr[j+1]); +} + -#include -#define MAX 100 -int main(void) -{ - int arr[MAX],i,j,temp,n,xchanges; - printf("Enter the number of elements : "); - scanf("%d",&n); - for(i=0; i arr[j+1]) - { - temp = arr[j]; - arr[j] = arr[j+1]; - arr[j+1] = temp; - xchanges++; - } - } - if(xchanges==0) /*If list is sorted*/ - break; - } - printf("Sorted list is :\n"); - for(i=0; i