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]<<" "; } }

8th Jun 2021, 1:50 AM
Abhishek Kumar
Abhishek Kumar - avatar
4 Respostas
+ 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]; }
8th Jun 2021, 2:01 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Cross check with this example https://www.sololearn.com/learn/650/?ref=app
8th Jun 2021, 2:01 AM
Ipang
0
Assign a[j+1] = temp
8th Jun 2021, 4:21 AM
Aravind Shetty
Aravind Shetty - avatar