数据结构之排序算法

#include <jni.h>
#include <android/log.h>
include <stack>
static void insertSort(int num[], int n) {
    for (int i = 1; i < n; i++) {
        if (num[i] < num[i - 1]) {
            int tmp = num[i];
            int j;
            for (j = i - 1; j >= 0 && tmp < num[j]; j--) {
               num[j + 1] = num[j];
            }
            num [j + 1] = tmp;
        }
        __android_log_print(ANDROID_LOG_DEBUG, "MD_SORT", "i=%d, insert sort input:%d %d %d %d %d %d %d %d", i, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
    }
}



static void binaryInsertSort(int num[], int n) {
    for (int i = 1; i < n; i++) {
        if (num[i] < num[i - 1]) {
            int low = 0;
            int high = i - 1;
            int mid;
            do {
                mid = (low + high) / 2;
                if (num[mid] > num[i]) {
                    high = mid - 1;
                } else {
                    low = mid + 1;
                }
            } while(low <= high);
            int tmp = num[i];
            for (int k = i - 1; k >= low; k--) {
                num[k + 1] = num[k];
            }
            num[low] = tmp;
        }
        __android_log_print(ANDROID_LOG_DEBUG, "MD_SORT", "i=%d, insert sort input:%d %d %d %d %d %d %d %d", i, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
    }
}

static void shellSort(int num[], int n) {
    for (int d = n / 2; d > 0; d /= 2) {
        for (int i = d; i < n; i++) {
            if (num[i] < num[i - d]) {
                int tmp = num[i];
                int j;
                for (j = i - d; j >= 0 && tmp < num[j]; j -= d) {
                    num[j + d] = num[j];
                }
                num[j + d] = tmp;
            }
            __android_log_print(ANDROID_LOG_DEBUG, "MD_SORT", "i=%d, insert sort input:%d %d %d %d %d %d %d %d", i, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
        }
    }
}

static void quickSort(int num[], int low, int high) {
    if (low >= high) {
        return;
    }
    int l = low;
    int h = high;
    int s = num[l];
    while (l < h) {
        while (l < h) {
            if (num[h] >= s) {
                h--;
            } else {
                num[l] = num[h];
                break;
            }
        }
        while (l < h) {
            if (num[l] <= s) {
                l++;
            } else {
                num[h] = num[l];
                break;
            }
        }
    }
    num[l] = s;
    quickSort(num, low, l - 1);
    quickSort(num, l + 1, high);
    __android_log_print(ANDROID_LOG_DEBUG, "MD_SORT", "l=%d, h=%d, insert sort input:%d %d %d %d %d %d %d %d", low, high, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
}

static void quickSort(int num[], int n) {
    quickSort(num, 0, n - 1);
}

struct Range {
    int low;
    int high;
};

static int partion(int num[], int low, int high) {
    int l = low;
    int h = high;
    int s = num[l];
    while (l < h) {
        while (l < h) {
            if (num[h] >= s) {
                h--;
            } else {
                num[l] = num[h];
                break;
            }
        }
        while (l < h) {
            if (num[l] <= s) {
                l++;
            } else {
                num[h] = num[l];
                break;
            }
        }
    }
    num[l] = s;
    __android_log_print(ANDROID_LOG_DEBUG, "MD_SORT", "l=%d, h=%d, insert sort input:%d %d %d %d %d %d %d %d", low, high, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
    return l;
}

static void quickSortV2(int num[], int n) {
    if (n < 1) {
        return;
    }
    Range tmp;
    tmp.low = 0;
    tmp.high = n - 1;
    std::stack<Range> stack;
    stack.push(tmp);
    while(!stack.empty()) {
        Range r = stack.top();
        stack.pop();
        int pivot = partion(num, r.low, r.high);
        tmp.low = r.low;
        tmp.high = pivot - 1;
        if (tmp.low < tmp.high) {
            stack.push(tmp);
        }
        tmp.low = pivot + 1;
        tmp.high = r.high;
        if (tmp.low < tmp.high) {
            stack.push(tmp);
        }
    }
}

static void heapAdjust(int num[], int k, int n) {
   int top = num[k];
   for (int i = 2 * k + 1; i < n; i = 2 * k + 1) {
       if (i + 1 < n && num[i + 1] > num[i]) {
           i++;
       }
       if (num[i] > top) {
           num[k] = num[i];
           k = i;
       } else {
           break;
       }
   }
   num[k] = top;
}

static void heapBuild(int num[], int n) {
    for (int i = n / 2 - 1; i >= 0; i--) {
        heapAdjust(num, i, n);
        __android_log_print(ANDROID_LOG_DEBUG, "MD_SORT", "i=%d,heap sort do:%d %d %d %d %d %d %d %d", i, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
    }
}

static void headSort(int num[], int n) {
    heapBuild(num, n);
    for (int i = n - 1; i >= 1; i--) {
        std::swap(num[0], num[i]);
        heapAdjust(num, 0, i);
        __android_log_print(ANDROID_LOG_DEBUG, "MD_SORT", "i=%d,heap sort sort:%d %d %d %d %d %d %d %d", i, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
    }
}

static void heapInsert(int num[], int n, int v) {
    int childPos = n;
    while (childPos > 0) {
        int parentPos = (childPos - 1) / 2;
        if (num[parentPos] > v) {
           num[childPos] = num[parentPos];
            childPos = parentPos;
        } else {
            break;
        }
    }
    num[childPos] = v;
}

static void heapRemove(int num[], int n, int i) {
    num[i] = num[n - 1];
    heapAdjust(num, i, n - 1);
}

extern "C" JNIEXPORT void JNICALL
Java_com_zhangyue_stardict_Test_nativeTest(JNIEnv *env, jclass clazz) {
    int num[20]={0, 2, 4, 1, 10, 3, 20, 8};
    int n = 7;
    __android_log_print(ANDROID_LOG_DEBUG, "MD_SORT", "insert sort input:%d %d %d %d %d %d %d %d", num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
//    insertSort(num, n);
//    binaryInsertSort(num, n);
//    shellSort(num, n);
//    quickSort(num, n);
//    quickSortV2(num, n);
//    headSort(num, n);
    mergeSort(num, n);
    __android_log_print(ANDROID_LOG_DEBUG, "MD_SORT", "insert sort output:%d %d %d %d %d %d %d %d", num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
`
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值