From 23ecf2b4730b7d30270a6ee1234915f621e5db5a 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 22:32:38 -0300 Subject: [PATCH] Update Bubble_Sort_2.c Added the option to choose the size of the array, choose the elements and the ordering. --- Sorting/Bubble Sort/C/Bubble_Sort_2.c | 90 ++++++++++++++++++--------- 1 file changed, 62 insertions(+), 28 deletions(-) diff --git a/Sorting/Bubble Sort/C/Bubble_Sort_2.c b/Sorting/Bubble Sort/C/Bubble_Sort_2.c index ab3f8e69..b4ca08f1 100644 --- a/Sorting/Bubble Sort/C/Bubble_Sort_2.c +++ b/Sorting/Bubble Sort/C/Bubble_Sort_2.c @@ -1,34 +1,68 @@ #include - -int main() -{ - int array[100], n, c, d, swap; - - printf("Enter number of elements\n"); - scanf("%d", &n); - - printf("Enter %d integers\n", n); - - for (c = 0; c < n; c++) - scanf("%d", &array[c]); - - for (c = 0 ; c < n - 1; c++) - { - for (d = 0 ; d < n - c - 1; d++) - { - if (array[d] > array[d+1]) /* For decreasing order use < */ - { - swap = array[d]; - array[d] = array[d+1]; - array[d+1] = swap; + +void swap (int array[], int j){ + int aux = 0; + aux = array[j]; + array[j] = array[j+1]; + array[j+1] = 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; } } } - - printf("Sorted list in ascending order:\n"); - - for (c = 0; c < n; c++) - printf("%d\n", array[c]); - + + else if(order == 2){ + for(int i=0; i < size-1; i++){ + int flag = 0; + + for(int j=0; j