From 9b511bc8bcafe73a6c2c411fd2839646b08ee1ce Mon Sep 17 00:00:00 2001 From: Abhinav Aryan <77077109+07abhi9av@users.noreply.github.com> Date: Fri, 30 Sep 2022 00:33:21 +0530 Subject: [PATCH 1/2] Optimised version of bubble sort algorith --- sorting/bubble_sort.c | 99 ++++++------------------------------------- 1 file changed, 13 insertions(+), 86 deletions(-) diff --git a/sorting/bubble_sort.c b/sorting/bubble_sort.c index 8bd4edf296..08b9ef0cc6 100644 --- a/sorting/bubble_sort.c +++ b/sorting/bubble_sort.c @@ -1,95 +1,22 @@ -/** - * @file - * @brief [Bubble sort](https://en.wikipedia.org/wiki/Bubble_sort) algorithm - * implementation - */ -#include -#include #include -#include -#include -/** - * Display elements of array - * @param arr array to be display - * @param n length of array - */ -void display(const int *arr, int n) -{ - for (int i = 0; i < n; i++) - { - printf("%d ", arr[i]); - } - printf("\n"); -} +int main(){ +#define SIZE 5 // defining a macro +int arr[SIZE] = {3,6,2,7,1}; // taking a demo array -/** - * Swap two values by using pointer - * @param first first pointer of first number - * @param second second pointer of second number - */ -void swap(int *first, int *second) -{ - int temp = *first; - *first = *second; - *second = temp; -} - -/** - * Bubble sort algorithm implementation - * @param arr array to be sorted - * @param size size of array - */ -void bubbleSort(int *arr, int size) -{ - for (int i = 0; i < size - 1; i++) - { /* for each array index */ - bool swapped = false; /* flag to check if any changes had to be made */ - /* perform iterations until no more changes were made or outer loop - executed for all array indices */ - for (int j = 0; j < size - 1 - i; j++) - { /* for each element in the array */ - if (arr[j] > arr[j + 1]) - { /* if the order of successive elements needs update */ - swap(&arr[j], &arr[j + 1]); - swapped = true; /* set flag */ - } - } - if (!swapped) - { - /* since no more updates we made, the array is already sorted - this is an optimization for early termination */ - break; +//Bubble Sort Algorithm +for(int i=0;i arr[j+1]){ + int temp = arr[j]; //swapping using a temporary variable + arr[j] = arr[j+1]; + arr[j+1] = temp; } } } -/** - * Test function - */ -void test() -{ - const int size = 10; - int *arr = (int *)calloc(size, sizeof(int)); - - /* generate size random numbers from 0 to 100 */ - for (int i = 0; i < size; i++) - { - arr[i] = rand() % 100; - } - bubbleSort(arr, size); - for (int i = 0; i < size - 1; ++i) - { - assert(arr[i] <= arr[i + 1]); - } - free(arr); +for(int i=0;i Date: Fri, 30 Sep 2022 01:12:00 +0530 Subject: [PATCH 2/2] Update bubble_sort.c --- sorting/bubble_sort.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/sorting/bubble_sort.c b/sorting/bubble_sort.c index 08b9ef0cc6..32b7493972 100644 --- a/sorting/bubble_sort.c +++ b/sorting/bubble_sort.c @@ -1,22 +1,29 @@ #include -int main(){ -#define SIZE 5 // defining a macro +int main() +{ +#define SIZE 5 // defining a macro of definite size int arr[SIZE] = {3,6,2,7,1}; // taking a demo array //Bubble Sort Algorithm -for(int i=0;i arr[j+1]){ - int temp = arr[j]; //swapping using a temporary variable - arr[j] = arr[j+1]; - arr[j+1] = temp; +for(int i = 0 ; i < SIZE - 1 ; i++) //loop till SIZE - 1 index +{ + for(int j = 0 ; j < SIZE -i - 1 ; j++) //loop till SIZE of array - i -1 index + { + if(arr[j] > arr[j + 1]) //comparing j and j+1 element + { + int temp = arr[j]; //swapping using a temporary(temp) variable + arr[j] = arr[j + 1]; + arr[j + 1] = temp; } } } -for(int i=0;i