1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <iostream>
#include <cstdlib>
using namespace std;
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);
int main()
{
typedef intArray[];
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 << "After sorting..." << endl;
displayNums(numbers, howMany);
}
void generate(intArray nums, int size, int low, int high)
{
int n, i = 0;
do
{
n = rand();
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%20 == 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;
}
|