From abffa7443224b3ebe8fabe38e2866e6a7ebef9df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maria=20Lu=C3=ADsa=20Mendes?= <72216924+lumendesp@users.noreply.github.com> Date: Tue, 13 Oct 2020 23:19:17 -0300 Subject: [PATCH] Update bubble_sort.c Added the option to choose the size of the array, choose the elements and the ordering. --- Sorting Algorithms/C-CPP/bubble_sort.c | 92 +++++++++++++++++--------- 1 file changed, 62 insertions(+), 30 deletions(-) diff --git a/Sorting Algorithms/C-CPP/bubble_sort.c b/Sorting Algorithms/C-CPP/bubble_sort.c index 1230d62..af0f6d1 100644 --- a/Sorting Algorithms/C-CPP/bubble_sort.c +++ b/Sorting Algorithms/C-CPP/bubble_sort.c @@ -1,35 +1,67 @@ -#include - -void bbSort(int arr[], int N) //Function for Bubble Sorting - { - int i, j, aux; - for (i = 1; i < N; i++) - for (j = 0; j < N - 1; j++) { - if (arr[j] > arr[j + 1]) { - aux = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = aux; - } - } +#include + +void swap (int array[], int j){ + int aux = 0; + aux = array[j]; + array[j] = array[j+1]; + array[j+1] = aux; } -int main() -{ - int N, aux; - printf("\nEnter the number of elements= "); - scanf(" %d", &N); - int arr[N]; - printf("\nEnter the elements= "); - for(int i = 0; i < N; i++) - { - scanf(" %d", &aux); - arr[i] = aux; +void bubbleSort(int array[], int size, int order){ + if(order == 1){ + for(int i=0; i < size-1; i++){ + int flag = 0; + + for(int j=0; j array[j+1]){ + swap(array, j); + flag = 1; + } + } + if(flag == 0){ + break; + } } - bbSort(arr, N); - printf("\nThe sorted elements= \n"); - for(int i = 0; i < N; i++) - { - printf("\n%d", arr[i]); + } + + else if(order == 2){ + for(int i=0; i < size-1; i++){ + int flag = 0; + + for(int j=0; j