From bb5c12985b0643cdf14d77ac5cfd1cef0504f3b7 Mon Sep 17 00:00:00 2001 From: ayushi srivastava <92778552+Ayushisri29@users.noreply.github.com> Date: Sat, 8 Oct 2022 23:32:41 +0530 Subject: [PATCH] Create Bubble sort using recursion make a directory of bubble sort using recursion --- Sorting/Bubble sort using recursion | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Sorting/Bubble sort using recursion diff --git a/Sorting/Bubble sort using recursion b/Sorting/Bubble sort using recursion new file mode 100644 index 0000000..beec580 --- /dev/null +++ b/Sorting/Bubble sort using recursion @@ -0,0 +1,34 @@ +#include +using namespace std; + +// A function to implement bubble sort +void bubbleSort(int arr[], int n) +{ + // Base case + if (n == 1) + return; + + int count = 0; + // One pass of bubble sort. After + // this pass, the largest element + // is moved (or bubbled) to end. + for (int i=0; i arr[i+1]){ + swap(arr[i], arr[i+1]); + count++; + } + + // Check if any recursion happens or not + // If any recursion is not happen then return + if (count==0) + return; + + // Largest element is fixed, + // recur for remaining array + bubbleSort(arr, n-1); +} +void printArray(int arr[], int n) +{ + for (int i=0; i < n; i++) + printf("%d ", arr[i]); + }