diff --git a/Competitive Coding/Sorting/Bubble Sort/bubble_sort.c b/Competitive Coding/Sorting/Bubble Sort/bubble_sort.c index 8f47b721f..af0f6d1e1 100644 --- a/Competitive Coding/Sorting/Bubble Sort/bubble_sort.c +++ b/Competitive Coding/Sorting/Bubble Sort/bubble_sort.c @@ -1,46 +1,67 @@ +#include -// BUBBLE SORT +void swap (int array[], int j){ + int aux = 0; + aux = array[j]; + array[j] = array[j+1]; + array[j+1] = aux; +} -#include +void bubbleSort(int array[], int size, int order){ + if(order == 1){ + for(int i=0; i < size-1; i++){ + int flag = 0; -void swap(int*,int*); - -void bubblesort(int arr[], int size) -{ - int i, j; - for (i = 0; i < size - 1; i++) // Function where the actual algorithm is implemented - { - for (j = 0; j < size - i - 1; j++) - { - if (arr[j] > arr[j+1]) - swap(&arr[j], &arr[j+1]); - + for(int j=0; j array[j+1]){ + swap(array, j); + flag = 1; } + } + if(flag == 0){ + break; + } } + } + + else if(order == 2){ + for(int i=0; i < size-1; i++){ + int flag = 0; + + for(int j=0; j