 # Help Required - Recursive Implementation of BubbleSort

``````

#include<bits/stdc++.h>

using namespace std;

void pass(vector<int>&A,int J,int I)
{
if(J==I)
{
return;
}
if(A[J]>A[J+1])
{
int temp = A[J];
A[J]= A[J+1];
A[J+1] = temp;
}
pass(A,J+1,I);
}

void bubbleSort(vector<int>&A,int n,int I)
{
if(I==n)
{
return;
}
pass(A,I=0,n-I);
bubbleSort(A,n,I+1);
}
int main()
{
int n,x;
cin>>n;
vector<int>A;
for(int i=0;i<n;i++)
{
cin>>x;
A.push_back(x);
}
bubbleSort(A,n-1,0);
for(int i=0;i<n;i++)
{
cout<<A[i]<<" ";
}
}`````````

``````C++
```
```
1 Like

Ya, thanks for the formatting suggestion. Now since the code is properly readable, can you please look into what errors are there in the code?

so you actually wrote main() in void function ??? 1 Like

That was just a typing error while typing in the question in the new topic section.
Can you look into the main problem?

no bro im not coding right now . i think you could figured it out and learn on your own it really teaches you even if it takes time.

I think you need to start from here. Your logic of bubble sort is wrong. Try the above tutorial(convert it into recursive), and if still in doubt, I’ll see what I can do The thing is for recurssive case maybe the optimized case is not possible. So for normal bubble sort implementation recurssively, this is what i tried. And i studied bubble sort properly before asking the question. Iterative case was easy, but please look into the recurssive case here.

Okay I got the bug,

In the pass function second parameter you are always resetting I to 0 so your programme goes to infinite loop I=0,1,0,1,0,1…

so to fix your pass should be pass(A,0,n-I)

Okay you should pass n-I only my bad!!

1 Like

in the pass function, instead of writing i=0, write 0 because it is making i equals to 0,Rest of the things are
correct.

1 Like

Thanks for the help. This helped me clarify concept of Recurssion about how some values reset. Although I my code was perfectly fine except that i=0 case. Thanks again.