home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 9
/
MEDIASHARE_09.ISO
/
progmisc
/
euphor10.zip
/
SHELL.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-07
|
1KB
|
63 lines
#include <stdio.h>
#include <time.h>
#define ITERATIONS 100000
#define TRUE 1
#define NITEMS 50
/* index from 1 to NITEMS */
int list[NITEMS+1] =
{0, 9, 34, 14, 18, 33, 46, 11, 13, 7, 26, 22, 10, 36, 40, 2, 28, 32, 1,
23, 31, 43, 5, 24, 42, 45, 50, 16, 3, 47, 39, 21, 49, 41, 6, 19, 30,
20, 35, 44, 38, 25, 15, 27, 17, 8, 4, 29, 37, 48, 12};
int x[NITEMS+1];
void shell_sort()
/* Shell sort based on insertion sort */
{
int i, gap, j, n;
int tempi, tempj;
gap = NITEMS / 4 + 1;
while (TRUE) {
for (i = gap + 1; i <= NITEMS; i++) {
tempi = x[i];
j = i - gap;
while (TRUE) {
tempj = x[j];
if (tempi >= tempj) {
j = j + gap;
break;
}
x[j+gap] = tempj;
if (j <= gap)
break;
j = j - gap;
}
x[j] = tempi;
}
if (gap == 1)
return;
else
gap = gap / 4 + 1;
}
}
main()
{
int t;
int i, j;
t = clock();
for (i = 1; i <= ITERATIONS; i++) {
for (j = 1; j <= NITEMS; j++)
x[j] = list[j];
shell_sort();
}
printf("%d iterations in %.2f seconds\n", ITERATIONS,
(clock() - t)/(double)CLOCKS_PER_SEC);
for (i = 1; i <= NITEMS; i++)
printf("%d ", x[i]);
}