0

I am in a discrete mathematics class and one of the hw problems is to implement a bubble sort. Here's my futile attempt because it does not output the solution. Please advice. Thank you.

#include <iostream>
#include <cstdlib>
using namespace std;
void BubbleSort();
int array1[100] = {0};
int k;
int main()
{
    cout << "Enter your numbers and when you are done, enter 0000:\n";
    int x = 0;
    int i;
    while (i != 0000)
    {
        cin >> i;
        array1[x] = i;
        x++;
        k = x;
    }
    BubbleSort();
    system("pause");
    return 0;

}

void BubbleSort(){
    int temp;
    for( int i = 0; i < k; i++ ){
        if ( array1[i] > array1[i+1]){
            temp = array1[i+1];
            array1[i+1] = array1[i];
            array1[i] = temp;
        }
    }
    int x = 0;
    while (x <= k)
    {
        cout << array1[x] << "\n";
        x++;
    }
}

Please only use basic programming techniques because this is my first programming class. Thank you. Edit: fixed the relational operator. But now I get incorrect results.

2
  • 2
    i is uninitialized, so it may never even enter your loop for input. Without knowing what the error actually is though, it's tough to provide any help. Commented Feb 4, 2014 at 4:56
  • 1
    You can't accomplish a bubble sort in linear time. You need a nested loop there. And please initialize 'i' to a non-zero value explicitly. Commented Feb 4, 2014 at 5:01

2 Answers 2

2
while (x >! k)

This doesn't do what you think it does. If you want something that says "while x is not greater than k", you want <=. Since array1[k] isn't one of the elements you sorted, though, you probably want <.

while (x < k)

Note that for exists for loops like these:

for (int x = 0; x < k; x++) {
    cout << array1[x] << "\n";
}

As for the new bug, you're only doing one round of bubbling in your bubble sort. You need another for loop. Also, i is never initialized in main, and i != 0000 isn't going to check whether the user literally entered 4 zeros. It'll only check whether the user's input was equal to the number 0.

Sign up to request clarification or add additional context in comments.

2 Comments

COuld you please give me some other checking value that works? Thank you.
@user3088723: You'll need to read the user's input as a string, compare it to the string "0000", and then convert it to an integer if it wasn't "0000".
1

The primary problem is here:

while (x >! k)

On the first iteration, the condition checks whether (0 > !k), and k is not 0, so !k is 0, so the condition is false and the loop never executes. Try using:

for (int x = 0; x < k; x++)
    cout << array1[x] << "\n";

You also have a problem in the sort phase of your bubble sort; you only iterate through the data once, which is not enough to sort it, in general.

Finally, some design issues.

  1. You should have one function to sort the data and a separate function to print it. Don't combine the two functions as you have done here.
  2. Avoid global variables. Pass the array and its operational length to the sort function, and to the print function if you have one.

1 Comment

Thank you. I didn't think I'd have to sort multiple times. but now I understand!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.