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.