A
Andrew Tomazos
In follow-up to these two threads:
<http://groups.google.com/group/comp.programming/browse_thread/thread/
f7212303f3789b37>
<http://groups.google.com/group/comp.lang.c/browse_thread/thread/
58b60dda280aaccf>
Here is my C implementation of an O(N) time, O(1) space solution to
the "one algorithm problem" of sorting an unordered array of N records
(A[]) that have unique integer keys in the range 0..(N-1), according
to an array of integers (B[]) that are a permutation of the keys of A.
I would appreciate it if someone could verify that it is correct and
indeed runs in O(N) time.
void assert(int b) { if (!b) while (1) /* hang */; }
struct V {/* satalite data */};
struct R { int key; struct V value; };
void one_algo_prob(struct R* A, const int* B, const int N)
{
int i, j, a0, b0;
for (i = 0; i < N; i++)
if (A.key == 0)
{
A = A[0];
A[0].key = 0;
break;
}
for (i = 1; i < N; i++)
{
while (A.key != i)
{
A[0] = A;
A = A[A[0].key];
A[A[0].key] = A[0];
A[0].key = 0;
}
}
for (i = 0; i < N; i++)
assert(A.key == i);
a0 = 0;
for (i = 0; i < N; i++)
{
if (B == 0)
b0 = i;
}
assert(A[a0].key == 0);
assert(B[b0] == 0);
if (a0 != b0)
{
A[a0] = A[b0];
A[b0].key = 0;
a0 = b0;
}
assert(a0 == b0);
for (i = 0; i < N; i++)
{
j = i;
while (A[j].key != B[j])
{
A[a0] = A[j];
A[j] = A[B[j]];
A[B[j]] = A[a0];
A[a0].key = 0;
j = B[j];
}
}
for (i = 0; i < N; i++)
{
assert(A.key == B);
}
}
int main(int argc, char** argv)
{
struct R A[10];
int B[10];
B[0] = 8;
B[1] = 2;
B[2] = 7;
B[3] = 1;
B[4] = 5;
B[5] = 0;
B[6] = 3;
B[7] = 9;
B[8] = 4;
B[9] = 6;
A[0].key = 5;
A[1].key = 9;
A[2].key = 0;
A[3].key = 8;
A[4].key = 3;
A[5].key = 4;
A[6].key = 6;
A[7].key = 7;
A[8].key = 1;
A[9].key = 2;
one_algo_prob(A,B,10);
}
Regards,
Andrew.
<http://groups.google.com/group/comp.programming/browse_thread/thread/
f7212303f3789b37>
<http://groups.google.com/group/comp.lang.c/browse_thread/thread/
58b60dda280aaccf>
Here is my C implementation of an O(N) time, O(1) space solution to
the "one algorithm problem" of sorting an unordered array of N records
(A[]) that have unique integer keys in the range 0..(N-1), according
to an array of integers (B[]) that are a permutation of the keys of A.
I would appreciate it if someone could verify that it is correct and
indeed runs in O(N) time.
void assert(int b) { if (!b) while (1) /* hang */; }
struct V {/* satalite data */};
struct R { int key; struct V value; };
void one_algo_prob(struct R* A, const int* B, const int N)
{
int i, j, a0, b0;
for (i = 0; i < N; i++)
if (A.key == 0)
{
A = A[0];
A[0].key = 0;
break;
}
for (i = 1; i < N; i++)
{
while (A.key != i)
{
A[0] = A;
A = A[A[0].key];
A[A[0].key] = A[0];
A[0].key = 0;
}
}
for (i = 0; i < N; i++)
assert(A.key == i);
a0 = 0;
for (i = 0; i < N; i++)
{
if (B == 0)
b0 = i;
}
assert(A[a0].key == 0);
assert(B[b0] == 0);
if (a0 != b0)
{
A[a0] = A[b0];
A[b0].key = 0;
a0 = b0;
}
assert(a0 == b0);
for (i = 0; i < N; i++)
{
j = i;
while (A[j].key != B[j])
{
A[a0] = A[j];
A[j] = A[B[j]];
A[B[j]] = A[a0];
A[a0].key = 0;
j = B[j];
}
}
for (i = 0; i < N; i++)
{
assert(A.key == B);
}
}
int main(int argc, char** argv)
{
struct R A[10];
int B[10];
B[0] = 8;
B[1] = 2;
B[2] = 7;
B[3] = 1;
B[4] = 5;
B[5] = 0;
B[6] = 3;
B[7] = 9;
B[8] = 4;
B[9] = 6;
A[0].key = 5;
A[1].key = 9;
A[2].key = 0;
A[3].key = 8;
A[4].key = 3;
A[5].key = 4;
A[6].key = 6;
A[7].key = 7;
A[8].key = 1;
A[9].key = 2;
one_algo_prob(A,B,10);
}
Regards,
Andrew.