#include
//#include "algo.h"
void msort2(int lo, int hi, int S[]);
void mg2(int lo, int mid, int hi, int S[]);
void msort2(int lo, int hi, int S[])
{
int mid;
if ((lo < hi) && ((hi - lo) > 1))
{
mid = (lo + hi) / 2;
//printf(".S[%d]: %d, S[%d]: %d\n", lo, S[lo], mid, S[mid]);
msort2(lo, mid, S);
//printf("..S[%d]: %d, S[%d]: %d\n", mid + 1, S[mid + 1], hi, S[ hi]);
msort2(mid + 1, hi, S);
// Upper is OK
printf("\nlo: %d, mid: %d, hi: %d\n", lo, mid, hi);
mg2(lo, mid, hi, S);
}
}
void mg2(int lo, int mid, int hi, int S[])
{
int i = lo;
int j = mid + 1;
int k = lo;
int U[16];
while ((i <= mid) && (j <= hi))
{
if (S[i] < S[j]) U[k] = S[i++];
else U[k] = S[j++];
k++;
}
int a;
if (i < mid)
for (a = i; a <= mid; a++)
U[a+k] = S[i+a];
else
for (a = j; a <= hi; a++)
U[k+a] = S[j+a];
for (a = lo; a <= hi; a++)
{
S[a] = U[a];
printf("S[%d]: %d, ", a, S[a]);
}
printf("\n");
}
int main(void)
{
int S[] = { 27, 10, 12, 20, 25, 13, 15, 22 };
int sz = sizeof(S) / 4;
int lo = 0;
int hi = sz - 1;
msort2(lo, hi, S);
printf("\nFinally: ");
int a;
for (a = 0; a < 8; a++)
printf("%d, ", S[a]);
printf("\n");
return 0;
}
2006-11-20
jmsort2.c
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言