I am trying to figure out how to use recursion in programs. I understand how recursion works in classical examples like "factorial", but am not sure how to apply it on my own...
I am starting out with converting an iterative bubble sort code into a recursive code... I have searched over the net for the same.... but am not able to find a convincing solution/explanation..
Example iterative code for bubble sort is:
arr[n]-> array with elements (1..n) which is to be sorted
for(i:1 to n) for(j:1 to n-1) if(arr[j+1]>arr[j]) swap(arr[j+1],arr[j]);
Would feel helpful if someone could give a hint about how to go about...
In the recursive approach of Bubble sort, the base case is array length = 1. Otherwise traverse the array using single for loop and swap elements accordingly. Take input array Arr[] and length as number of elements in it.
Recursive Bubble Sort has no performance/implementation advantages, but can be a good question to check one's understanding of Bubble Sort and recursion. If we take a closer look at Bubble Sort algorithm, we can notice that in first pass, we move largest element to end (Assuming sorting in increasing order).
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed.
Recursion is used in Quick sort and merge sort.
Recursion is a design technique based on inductive proofs. Considers one or more base (simple) case(s) for your problem, and one or more ways to move the problem closer to being a base-case problem. Then, at each step of the algorithm, you either recognize completion (and deal appropriately with a base case) make the problem slightly closer to being a base case.
Bubble sort is just an application of the observation that a sorted array has all adjacent pairs of elements in order. Defined recursively, it works like:
You can write that very idea in the programming language of your choice, and you get bubble sort. Oh, but you have to define what it means to "bubble the largest element". Well, that's another opportunity for recursive design. I think you get the idea, though.
Faster sorts are mostly based on observations about how you get closer to the goal in less work. Quick sort, for instance, relies on the notion that, if an array is sorted, then there is some element P, and all elements less than P are to P's left, and all elements more than P are to P's right. If you can establish that property on an array, and also pick P, then you can cut the problem roughly in half at each step instead of simply by one.
public void sort(int[] arr, int first, int last){
if(first < last && last > 0){
if(arr[first] > arr[first+1]){
int temp = arr[first];
arr[first] = arr[first+1];
arr[first+1] = temp;
}
sort(arr, first+1, last);
sort(arr, first, last-1);
}
else
return;
}
Late for 2 years, but maybe it will useful to someone
Here is another easy way to sort your array recursively
using Bubble-Sort
.
static void recursive_bubble_sort(int[] Arr, int l)
{// 'Arr' is the array where 'l' is the length of the array
if (l == 0) {
return;
}
for (int j = 0; j < l - 1; j++) {
if (Arr[j] > Arr[j + 1]) {
swap(Arr[j], Arr[j + 1]);
}
}
recursive_bubble_sort(Arr, l - 1);
}
For recursive solution, the length must be updated so function always works with the remaining items from array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With