Hello can someone please help me with my bubble sort code? It doesn't compile for some reason and its suppose to be in ascending order. I would like to know what my mistakes are and why it isn't working, even though the rest of the code works fine. I have been trying to fix the code for hours but no matter what i do the bible sort doesn't work. The code doesn't even exit it but stops doing anything and i have to exit out of it. Can someone please help me? Thank you very much for your help! (just the snip of the code thats not working below)
#include<iostream>
usingnamespace std;
int main(){
int n;
cin>>n;
int arr[n];
for (int i=0; i<n; i++){
cin>>arr[i]; //read all the elements of the array
}
bool swapped = true;
while(swapped){
swapped=false;
for (int i=0; i<n; i++){
if (arr[i]>arr[i+1]){
swap(arr[i], arr[i+1]); //if an unswapped element is found, swap it
swapped=true; //change swapped to true so it can continue searching for an unswapped element.
}
}
}
for (int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}
Not "for some reason". If it doesn't compile, your compiler will have given you an error message which tells you specifically the reason why it doesn't compile.
I'm curious; why did you decide it would be better not to tell us what that error message is?
Thank you! But that didn't work.
I didn't include the error message because there isn't an error message. The first part of my code runs fine, which asks the user how many arrays the user wants and then takes that many elements from the user e and outputs the elements back in the same order. But after that nothing, the program doesn't end or do anything, it just kind of stops without doing anything. So I guess should have said that the code complies but it doesn't work correctly.