Bubble Sort Help

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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  //Sort the elements, Ascending order, Lowest ---> highest
    int temp;
    for(int num = 0; num < (arr[num]-1); num++)
    {
      for(int j = 0; j < (arr[num]-num-1); j++)
      {
        if(arr[j] > arr[j+1])
        {
          //swap them
          temp = arr[j];
          arr[j] = arr[j+1];
          arr[j+1] = temp;
        }
      }
    }

    //Display the elements of the array (one element per line)
   for (int num =0; num < 20 ; num++)
   {
     cout << arr[num] << " ";
   }
  return 0;
Last edited on
You need a while loop, which checks if there are any unswapped elements left.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace 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]<<" ";
    }
}
Last edited on
It doesn't compile for some reason

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.
What doesn't work, your code or mine?
Mine, I did what you suggested and it still didn't work.
Well it works fine for me. What compiler are you using?
I'm using g++.
Should work. Maybe the console just closes instantly. Try putting this code at the end of main.

1
2
cin.get();
    return 0;

Also be sure you put correct input.

First a number n, specifying how many integers will be used.
Then, n integers.
So sorry for the late reply, but it worked! Thank You so much>
Topic archived. No new replies allowed.