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
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#define ASIZE 25
#define SCALE 100
using namespace std;
void fillarray(int a[], int n);
void sortarray(int a[], int n);
void printarray(int a[], int n);
void initialize(int a[]);
int getrandom(int use[]);
int main()
{
int size=ASIZE; /* SIZE FOR ARRAY */
int array[size]; /* ARRAY OF DATA */
fillarray(array, size);
printarray(array, size);
sortarray(array, size);
printarray(array, size);
return 0;
}
/* SORT ARRAY OF SIZE N */
void sortarray(int a[], int n)
{
int r, s, t; /* LOOP ITERATERS */
int tmp; /* FOR SWAPPING */
for (r=n/2; r>0; r/=2) {
for (s=r; s<n; s++) {
tmp = a[s];
for (t=s; t>=r && tmp<a[t-r]; t-=r)
{
a[t] = a[t-r];
}
a[t] = tmp;
}
}
}
/* PRINT ARRAY OF SIZE N */
void printarray(int a[], int n)
{
for (int x=0; x<n; x++)
{
cout << a[x];
if (x<n-1)
cout << ",";
}
cout << "\n";
}
/* FILL ARRAY OF SIZE N WITH RANDOM NUMBER */
void fillarray(int a[], int n)
{
int unique[SCALE]; /* array to make all unique */
initialize(unique);
srand((unsigned long int) time(NULL));
for (int x=0; x<n; x++)
a[x]=getrandom(unique);
}
int getrandom(int use[])
{
int n;
do
n=rand()%SCALE;
while (use[n]==1);
use[n]=1;
return n;
}
/* FILL ARRAY WITH 0 TO GARANTEE UNIQUENESS */
void initialize(int a[])
{
for (int x=0; x<SCALE; x++)
a[x]=0;
}
|