what is wrong with the code?
Sorting in linear time
#include<bits/stdc++.h>
using namespace std;
void swap(int *a,int *b)
{
int temp = *a;
*a=*b;
*b = temp;
}
int main() {
int n;
cin>>n;
int a[n];
for(int i=0 ; i<n ; i++)
{
cin>>a[i];
}
int s=0,e=n-1,mid=0,i;
while(mid<=e)
{
if(a[i]==1)
{
mid++;
}
else if(a[i]==2)
{
swap(&a[mid],&a[e]);
eā;
}
else{
swap(&a[mid],&a[s]);
mid++;
s++;
}
}
for(int i=0 ; i<n ; i++)
{
cout<<a[i]<<endl;
}
return 0;
}
what is i in the while loop checking i has the garbage value
1 Like