#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;
}