1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
// **** Declarations ****
#define BY_SEQNUM 0
#define BY_GENID 1
#define BY_LEN 2
struct resultnode{
unsigned long seq_num;
unsigned long len;
unsigned long Generation_ID;
unsigned long generation_member_count;
};
typedef resultnode result;
result *Results;
result **r_pt_by_SeqNum;
unsigned long resultcount;
// **** Call to merge sort ****
r_pt_by_SeqNum = (result **)malloc(resultcount * sizeof(result *));
for(i = 0; i < resultcount; i++) // initialize pointers
r_pt_by_SeqNum[i] = &Results[i]; // (Results sequence is random)
SortResults(r_pt_by_SeqNum, resultcount - 1L, BY_SEQNUM); // sort by seq_num
// **** merge sort routine ***
int SortResults(result **ptpt, unsigned long thismany, int sorttype)
{
void partitioning(result **, unsigned long, unsigned long, int);
partitioning(ptpt, 0L, thismany, sorttype);
return(0);
}
void partitioning(result **ptpt, unsigned long low, unsigned long high, int sorttype)
{
void msort_test1(result **ptpt, unsigned long low, unsigned long mid, unsigned long high);
void msort_test2(result **ptpt, unsigned long low, unsigned long mid, unsigned long high);
void msort_test3(result **ptpt, unsigned long low, unsigned long mid, unsigned long high);
void partitioning(result **ptpt, unsigned long, unsigned long, int);
unsigned long mid;
if(low < high){
mid = (low + high)/2;
partitioning(ptpt, low, mid, sorttype);
partitioning(ptpt, mid+1, high, sorttype);
if(sorttype == BY_SEQNUM)
msort_test1(ptpt, low, mid, high);
else if(sorttype == BY_GENID)
msort_test2(ptpt, low, mid, high);
else if(sorttype == BY_LEN)
msort_test3(ptpt, low, mid, high);
else{ printf("Internal error\n"); exit(-1); }
}
}
void msort_test1(result **ptpt, unsigned long low, unsigned long mid, unsigned long high)
{
unsigned long i,j,k,l;
result **b;
b = (result **)malloc(resultcount * sizeof(result *));
if( b == NULL ){
printf("Mem alloc error\n");
exit(-1);
}
l = low;
i = low;
j = mid+1;
while( (l <= mid) && (j <= high) ){
if( (*ptpt[l]).seq_num <= (*ptpt[j]).seq_num ){
b[i] = ptpt[l];
l++;
}
else
{
b[i] = ptpt[j];
j++;
}
i++;
}
if(l > mid){
for(k = j; k <= high; k++){
b[i] = ptpt[k];
i++;
}
}
else{
for(k=l; k <= mid; k++){
b[i] = ptpt[k];
i++;
}
}
for(k = low; k <= high; k++){
ptpt[k] = b[k];
}
free ( b );
}
// **** End of code ****
// *********************
|