zoukankan      html  css  js  c++  java
  • 各种排序

    //冒泡排序 前往后
    int BubbleSortf(int* a, int aSize) {
        int temp, flag;
        for (int i = 0; i < aSize; i++) {
            flag = 0;
            for (int j = 0; j < aSize - i; j++) {
                if (a[j] < a[j + 1]) {
                    temp = a[j + 1];
                    a[j + 1] = a[j];
                    a[j] = temp;
                    flag = 1;
                }
            }
            if (flag == 0) {
                return *a;
            }
        }
        return *a;
    }
    //冒泡排序 后往前
    int BubbleSortb(int* a, int aSize) {
        int temp, flag;
        for (int i = 0; i < aSize; i++) {
            flag = 0;
            for (int j = aSize - 1; j > i; j--) {
                if (a[j - 1] < a[j]) {
                    temp = a[j - 1];
                    a[j - 1] = a[j];
                    a[j] = temp;
                    flag = 1;
                }
            }
            if (flag == 0) {
                return *a;
            }
        }
        return *a;
    }
    // 快速排序 从小到大
    int QuickSort(int* a, int low, int high) {
        if (low < high) {
            int pivotPos = QuickSortPart(a, low, high);
            QuickSort(a, low, pivotPos - 1);
            QuickSort(a, pivotPos + 1, high);
        }
        return *a;
    }
    
    
    int QuickSortPart(int* a, int low, int high) {
        int temp = a[low];
        while (low < high) {
            while (low < high && a[high] >= temp) {
                high--;
            }
            a[low] = a[high];
            while (low < high && a[low] <= temp) {
                low++;
            }
            a[high] = a[low];
        }
        a[low] = temp;
        return low;
    }
    //归并排序,由小到大
    void Merge(int* a, int * b, int low, int mid, int high) {
        int i, j, k;
        for (k = low; k <= high; k++) {
            b[k] = a[k];
        }
        for (i = low, j = mid + 1, k = i; i < mid + 1 && j <= high; k++) {
            if (b[i] <= b[j]) {
                a[k] = b[i++];
            }
            else {
                a[k] = b[j++];
            }
        }
        while (i < mid + 1) {
            a[k++] = b[i++];
        }
        while (j <= high) {
            a[k++] = b[j++];
        }
    }
    
    
    void MergeSort(int* a, int *b, int low, int high) {
        if (low < high) {
            int mid = (low + high) / 2;
            MergeSort(a, b, low, mid);
            MergeSort(a, b, mid + 1, high);
            Merge(a, b, low, mid, high);
        }
    }
    int times(int *a, int aSize) {
        int max = 0;
        for (int i = 0; i < aSize; i++) {
            if (a[i] > max) {
                max = a[i];
            }
        }
        int times = 0;
        while (max >= 10) {
            max = max / 10;
            times++;
        }
        return times;
    }
    
    
    void BucketSorts(int* a, int aSize) {
        int t = times(a, aSize);
        LinkNode b[10];
        LinkList p, q, r;
        for (int i = 0; i < 10; i++) {
            b[i].next = NULL;
        }
        int j = 0;
        int l = 1;
        while (t >= 0) {
            for (int i = 0; i < aSize; i++) {
                j = a[i] / l % 10;
                p = &b[j];
                while (p->next != NULL) {
                    p = p->next;
                }
                p->next = (LinkNode*)malloc(sizeof(LinkNode));
                p->next->next = NULL;
                p = p->next;
                p->i = a[i];
            }
            int k = 0;
            for (int i = 0; i < 10; i++) {
                q = &b[i];
                while (q->next != NULL) {
                    q = q->next;
                    a[k++] = q->i;
                }
                if (b[i].next != NULL) {
                    p = b[i].next;
                    while (p->next != NULL) {
                        q = p;
                        p = p->next;
                        free(q);
                    }
                    free(p);
                }
                b[i].next = NULL;
            }
            t--;
            l *= 10;
        }
    }
  • 相关阅读:
    Hystrix服务降级
    postman使用教程12-预处理(pre-request) 发送请求
    postman使用教程11- sign 签名预处理(pre-request)
    postman使用教程10-请求前参数预处理(pre-request)
    postman使用教程9-点 code 按钮生成代码段
    postman使用教程8-设置断言(Tests脚本编写)
    postman使用教程7-参数化引用外部文件(txt/csv/json)测试数据
    postman使用教程6-引用随机变量($guid,$timestamp,$randomInt)
    postman使用教程5-Test脚本中自定义变量(参数关联 提取 token 和引用 token )
    postman使用教程4-集合变量(collection variables)的使用
  • 原文地址:https://www.cnblogs.com/mengxinteriri/p/14157683.html
Copyright © 2011-2022 走看看