# bubble sort not sorting numbers in order.

This is a discussion on bubble sort not sorting numbers in order. within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering if you can help me figure out why my bubble sort is not sorting the random ...

1. ## bubble sort not sorting numbers in order.

Hi,

I was wondering if you can help me figure out why my bubble sort is not sorting the random numbers in order. My code is the following:

Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

typedef int intArray[];
void generate(intArray nums, int size, int low, int high);
void bubSort(intArray nums, int size);
void displayNums(intArray nums, int size);
void display(int n);
bool notInOrder(int a, int b);
void swap(int& a, int& b);

int main()
{
const int MAX_SIZE = 5000; // arbitrary
int numbers[MAX_SIZE];
int howMany;

cout << "How many numbers to sort? ";
cin >> howMany;

if(howMany > MAX_SIZE)
{
howMany = MAX_SIZE;
}

generate(numbers, howMany, 0, 999);
cout << "Numbers generated..." << endl;
displayNums(numbers, howMany);
cout << endl;
cout << "After sorting..." << endl;
displayNums(numbers, howMany);
cout << endl;
}

void generate(intArray nums, int size, int low, int high)
{
int n, i = 0;
do
{
n = (rand() + time(0)) % 100;
if(n >= low && n <= high)
{
nums[i] = n;
i++;
}
}while (i <= size);
}

void bubSort(intArray nums, int size)
{
for(int k = size-2; k >= 0; k--)
{
for(int i = 0; i <= k; i++)
{
if(notInOrder(nums[i],nums[i+1]))
{
swap(nums[i],nums[i+1]);
}
}
}
}

void displayNums(intArray nums, int size)
{
for(int s = 1; s <= size; s++)
{
cout.width(4);
cout << nums[s];
if(s != 0 && s%15 == 0)
{
cout << endl;
}
}
}

bool notInOrder(int a, int b)
{
return (a > b);
}

void swap(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}```
And the results I get are:
Code:
```Numbers generated...
90  45  47  83  59  42  51  44  18  81
After sorting...
90  45  47  83  59  42  51  44  18  81```
I have no idea why they are not being sorted correctly. I am sure the problem is there staring me in the face, but I've been trying to figure out whats wrong for a couple of hours, and have resorted to coming on here, and getting help.

I thank you in advance if you can help me solve my problem as to why the numbers are not being sorted.

2. So ... you could call your sort function, maybe, if you want things to be sorted.

3. Originally Posted by tabstop
So ... you could call your sort function, maybe, if you want things to be sorted.
Sorry, I seem really stupid now not noticing the most important part of the program not being there!! Thanks very much.

4. There is one other problem though, it seems as though my program sorts the numbers out, but some just disappear such as what happens in this one:

Code:
```How many numbers to sort? 10
Numbers generated...
4  59  61  97  73  56  65  58  32  95
After sorting...
32  56  58  59  61  65  73  84  97  95```
Why does it seem to delete a number and then replace it with a random number?

5. Arrays go from 0 to n-1, despite what your displayNums may believe.

6. Originally Posted by tabstop
Arrays go from 0 to n-1, despite what your displayNums may believe.
Ok, well I've sorted out one problem in that it now doesn't replace numbers and the output is the following:

Code:
```How many numbers to sort? 10
Numbers generated...
73  93  48  50  86  62  45  54  47  21  84
After sorting...
21  45  47  48  50  54  62  73  86  93  84```
Now it seems that it does not sort out the last number so in the case above, 84 should be before 86 and not at the end.

Any idea why this is happening?

7. Did you notice how 11 numbers were printed? Remember, arrays go from 0 to n-1.

8. Originally Posted by tabstop
Did you notice how 11 numbers were printed? Remember, arrays go from 0 to n-1.
Sorry but, I really am struggling to know why it is doing this. Is the problem within the bubSort code?

9. Originally Posted by rushhour
Sorry but, I really am struggling to know why it is doing this. Is the problem within the bubSort code?
No.
Code:
`for(int s = 1; s <= size; s++)`
Arrays go from 0 to n-1, not 1 to n.

10. Ok well I've changed it, but it seems to be missing a number again, which was the second problem that I was having. My output now displays:
Code:
```How many numbers to sort? 10
Numbers generated...
79  34  36  72  48  31  40  33   7  70
After sorting...
31  33  34  36  40  48  59  70  72  79```
and the 7 is missing from the sorting. So how can this problem be corrected?

11. Originally Posted by rushhour
Ok well I've changed it, but it seems to be missing a number again, which was the second problem that I was having. My output now displays:
Code:
```How many numbers to sort? 10
Numbers generated...
79  34  36  72  48  31  40  33   7  70
After sorting...
31  33  34  36  40  48  59  70  72  79```
and the 7 is missing from the sorting. So how can this problem be corrected?
I'm guessing the problem would be corrected by printing the ten numbers in the array, and not the last nine numbers of the array plus a random number after. Remember, everything I highlighted in red is wrong, and should not be in your program. When I change your print statement to print the ten numbers in the array, and not the last nine numbers of the array plus a random number after, I get correct results.

12. So must the for loop be changed like this?
Code:
`for(int s = 0;  s < size; s++)`
Really sorry, but i really am struggling to find the solution to the problem at hand.

13. Originally Posted by rushhour
So must the for loop be changed like this?
Code:
`for(int s = 0;  s < size; s++)`
Really sorry, but i really am struggling to find the solution to the problem at hand.
Since that is a loop that goes from 0 to n-1, that is the correct loop.

14. Originally Posted by tabstop
Since that is a loop that goes from 0 to n-1, that is the correct loop.
Ok, but can you tell me why it still replaces one of the numbers? e.g. the output is:
Code:
```How many numbers to sort? 10
Numbers generated...
24  44  99   1  37  13  96   5  98  72
After sorting...
1   5  13  24  35  37  44  72  96  98```
There seems to be no number 35. Can you explain why this is the case?

15. Originally Posted by rushhour
There seems to be no number 35. Can you explain why this is the case?
hmm...
Code:
```After sorting...
1   5  13  24  35  37  44  72  96  98```
EDIT:
Wait, a minute, you mean to say that there is no 35 before sorting? It was rather confusing to ask "why it still replaces one of the numbers" and then mention 35. You should mention 99 instead.

Anyway, what is your current code?

Page 1 of 2 12 Last
Popular pages Recent additions