求两个等长升序序列的中位数 .

#include <stdio.h>
#include <stdlib.h>

static void printNumber(int* a, int n) {
    int i = 0;
   
    for (i = 0; i < n; i++) {
        printf("a[%d] = %d\t", i, *(a + i));   
    }
}

static void randCreateNumber(int* a, int n) {
    int i = 0;
   
    for (i = 0; i < n; i++) {
        *(a + i) = rand() % 100;   
    }
}
      
static int getMiddleNumber(int* a, int start, int end) {
    printf("middle-number = %d\n", *(a + start + (end - start) / 2));
    if (start <= end) {
        return *(a + start + (end - start) / 2);      
    } else {
        return -1;      
    }
}

static int minDefine(int a, int b) {
    return a > b ? b : a;      
}

typedef int (*dataCompareFunc)(void* ctx, void* data);

static int cmp(void* a, void* b) {
    return (int)a - (int)b;
}

static void quickSortImpl(void** array, int left, int right, dataCompareFunc cmp) {
    int saveLeft = left;
    int saveRight = right;
    void* x = array[left];
   
    while (left < right) {
        while (cmp(array[right], x) >= 0 && left < right) {
            right--;     
        }     
       
        if (left !=  right) {
           array[left] = array[right];        
           left++;
        } 
             
        while  (cmp(array[left], x) <= 0 && left < right) {
            left++;      
        }
       
        if (left != right) {
            array[right] = array[left];
            right--;        
        }
    }
   
    array[left] = x;
   
    if (saveLeft < left) {
        quickSortImpl(array, saveLeft, left - 1, cmp);            
    }
   
    if (saveRight > left) {
        quickSortImpl(array, left + 1, saveRight, cmp);             
    }
}
 
static quickSort(void** array, int n, dataCompareFunc cmp) {
    quickSortImpl(array, 0, n - 1, cmp);      
}

static int  getMiddleNumberTwo(int* a, int* b, int startA, int endA, int startB, int endB) {
    int middleA = getMiddleNumber(a, startA, endA);
    int middleB = getMiddleNumber(b, startB, endB);
    int middleIndexA = startA + (endA - startA) / 2;
    int middleIndexB = startB + (endB - startB) / 2;
    int tmp[3];
   
    if (startA == endA || startB == endB) {
        printf("---middle-number: %d---\n", middleA);
        return minDefine(middleA, middleB);         
    }
   
    if (endA - startA == 1 || endB - startB == 1) {
       *(tmp) = a[startA];
       *(tmp + 1) = a[endA];
       *(tmp + 2) = b[startB];
       *(tmp + 3) = b[endB];
      
       return getMiddleNumber(tmp, 0, 3);
    }
              
    if (middleA == middleB) {
        printf("---middle-number: %d---\n", middleA);
        return middleA;           
    } else if (middleA < middleB) {
        if ((endA - startA ) % 2 == 1) {
            printf("--------------1\n");
            return getMiddleNumberTwo(a, b, middleIndexA, endA, startB, middleIndexB + 1);
        } else {
            printf("--------------2\n");
            return getMiddleNumberTwo(a, b, middleIndexA, endA, startB, middleIndexB);
        }
    } else {
        if ((endB - startB) % 2 == 1) {
            printf("--------------3\n");
            return getMiddleNumberTwo(a, b, startA, middleIndexA + 1, middleIndexB, endB);
        } else {
            printf("--------------4\n");
            return getMiddleNumberTwo(a, b, startA, middleIndexA, middleIndexB, endB);  
        }  
    }
}

#define LENGTH 10
static void testInt() {
    int a[LENGTH];
    int b[LENGTH];  
    int i = 0;
      
    randCreateNumber(a, LENGTH);
    printNumber(a, LENGTH);
    getMiddleNumber(a, 0, LENGTH -1);
   
    //sort
    quickSort(&a, LENGTH, cmp);
    printNumber(a, LENGTH);
    getMiddleNumber(a, 0, LENGTH -1);
   
    randCreateNumber(b,  LENGTH);
    printNumber(b, LENGTH);
    getMiddleNumber(b, 0, LENGTH - 1);
   
    //sort
    quickSort(&b, LENGTH, cmp);
    printNumber(b, LENGTH);
    getMiddleNumber(b, 0, LENGTH - 1);
       
    printf("\n");
    getMiddleNumberTwo(a, b, 0, LENGTH - 1, 0, LENGTH - 1);
}

int main(int argc, char *argv[])
{
  testInt();
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值