0
why this program is not running in proper way for bubble sort?
#include<iostream> using namespace std; int main() { int n; cin>>n; int a[n]; for (int i=0;i<n;i++) { cin>>a[i]; } for(int i=0;i<n-1;i++) { for(int j=0;j<n-1-i;j++) if(a[j]>a[j+1]) { int temp; temp=a[j]; a[j]=a[j+1]; a[j+1]=a[j]; } } for(int i=0;i<n;i++) { cout<<a[i]<<" "; } }
3 Respuestas
+ 2
Check your this part line no 17 here u defined temp variable and u not using anywhere check these statements
if(a[j]>a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=a[j];
}
+ 1
Cross check with this example
https://www.sololearn.com/learn/650/?ref=app
0
Assign a[j+1] = temp