R
RoSsIaCrIiLoIA
Write a function that gets an array of unsigned int
fill it with random values all differents,
and sorts it. It should be faster than qsort too.
Do you like my solution?
_______________________
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void
ArrRand(unsigned* a, unsiged size, unsigned superiore)
{static int m = 1;
unsigned i, j, h, temp;
if(a==0 || size<2 || superiore==0)
return;
if(m)
{ m = 0; srand(time(0));}
for( i = 0; i < size; ++i)
{label:
a = _lrand() % superiore; /*_lrand is for Borland */
for( j = 0; j < i; ++j) /* if not, use your_lrand() */
{if(a == a[j]) goto label;
else if(a < a[j]) /* that fill an unsigned */
{temp = a;
for( h = i; h > j; --h)
a[h] = a[h - 1];
a[j] = temp;
break;
}
}
}
}
/* it is a c++ main */
int main(void)
{unsigned a[100] = {0}, i;
ArrRand( a, (sizeof a)/(sizeof *a), 80000000);
for( i = 0; i < 99; ++i)
cout << " " << a;
cout << "\n";
return 0;
}
fill it with random values all differents,
and sorts it. It should be faster than qsort too.
Do you like my solution?
_______________________
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void
ArrRand(unsigned* a, unsiged size, unsigned superiore)
{static int m = 1;
unsigned i, j, h, temp;
if(a==0 || size<2 || superiore==0)
return;
if(m)
{ m = 0; srand(time(0));}
for( i = 0; i < size; ++i)
{label:
a = _lrand() % superiore; /*_lrand is for Borland */
for( j = 0; j < i; ++j) /* if not, use your_lrand() */
{if(a == a[j]) goto label;
else if(a < a[j]) /* that fill an unsigned */
{temp = a;
for( h = i; h > j; --h)
a[h] = a[h - 1];
a[j] = temp;
break;
}
}
}
}
/* it is a c++ main */
int main(void)
{unsigned a[100] = {0}, i;
ArrRand( a, (sizeof a)/(sizeof *a), 80000000);
for( i = 0; i < 99; ++i)
cout << " " << a;
cout << "\n";
return 0;
}