home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1994 #1
/
monster.zip
/
monster
/
PROG_C
/
SNIP9404.ZIP
/
RG_QSORT.C2
< prev
next >
Wrap
Text File
|
1994-04-03
|
1KB
|
51 lines
/*
** quicksort.c -- quicksort integer array
**
** public domain by Raymond Gardner 12/91
*/
static void swap(int *a, int *b)
{
register int t;
t = *a;
*a = *b;
*b = t;
}
void quicksort(int v[], unsigned n)
{
unsigned i, j, ln, rn;
while (n > 1)
{
swap(&v[0], &v[n/2]);
for (i = 0, j = n; ; )
{
do
--j;
while (v[j] > v[0]);
do
++i;
while (i < j && v[i] < v[0]);
if (i >= j)
break;
swap(&v[i], &v[j]);
}
swap(&v[j], &v[0]);
ln = j;
rn = n - ++j;
if (ln < rn)
{
quicksort(v, ln);
v += j;
n = rn;
}
else
{
quicksort(v + j, rn);
n = ln;
}
}
}