How recursive bubble sort work
Recursive Bubble Sort
- Base Case: If array size is 1, return.
- Do One Pass of normal Bubble Sort. This pass fixes last element of current subarray.
- Recur for all elements except last of current subarray.
Example:
Index= 0 1 2 3 4 5 6 7
|
100 |
90 |
80 |
70 |
60 |
50 |
40 |
30 |
Array []=
Suppose we want to sort the above in the increasing order by the recursive bubble sort.
Make a get grater which will return the grater of the array.
Compare the first and last element of the array if the first is greater than last the swap with each other.
And again call the sort function and decrement the array size by 1;
1)
30 |
90 |
80 |
70 |
60 |
50 |
40 |
100 |
2)
30 |
40 |
80 |
70 |
60 |
50 |
90 |
100 |
The 30 is less the 90 the get grater will return the 90 as a maximum and compare it with the second last and swap.
3)
This will repeat until the array become sort
At last the sorted array
30 |
40 |
50 |
60 |
70 |
80 |
90 |
100 |
Comments
Post a Comment
if you have any confusion then ask hear.