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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include <iostream>
#include <time.h>
#include <string>
#include <iomanip>
using namespace std;
template <class Item>
void bubble_sort(Item a[], size_t size);
template <class Item>
void swap(Item& a, Item& b);
template <class Item>
void display_array(Item randNum, size_t size);
int main()
{
int *randNum;
int a,b;
int size;
swap(Item& a, Item& b); //NOT SURE IF WORKS YET, UNTESTED
srand((unsigned)time(NULL));
cout << "How many integers?" << endl;
cin >> size;
size = size +1;
randNum = new int [size];
for (int i = 1; i < size; i++)
{
randNum[i] = 1+ rand() % 10;
}
display_array(randNum, size);
cout << "\n \n sorted..." << endl;
bubble_sort( a[], size); //ERROR
//display_array(randNum, size);
system("pause");
return 0;
}
//=============================
template <class Item>
void bubble_sort(Item a[], size_t size)
{
size_t idx, pass;
for (pass=1; pass<=size; ++pass)
{
for (idx=0; idx<=size-2; ++idx)
{
if (a[idx] > a[idx+1])
swap(a[idx], a[idx+1]);
}
}
}
template <class Item>
void swap(Item& a, Item& b)
{
Item temp;
temp = a;
a = b;
b = temp;
}
template <class Item>
void display_array(Item randNum, size_t size)
{
for ( int j = 1; j < size; j++ )
{
cout << setw(16) << randNum[ j ];
}
}
|