zoukankan      html  css  js  c++  java
  • 4. 归并排序和快速排序

     1.递归形式的归并排序

    #include<iostream>
    using namespace std;
    int n;
    int *a;
    
    void merge(int l, int mid, int r, int *b){
        int i = l, j = mid + 1, k = 0;
        while (i <= mid && j <= r) {
            if (a[i] < a[j]) {
                b[k] = a[i];
                i++;
            }
            else {
                b[k] = a[j];
                j++;
            }
            k++;
        }
        if (i <= mid) {
            while (i <= mid) {
                b[k] = a[i];
                k++;
                i++;
            }
        }
        if (j <= r) {
            while (j <= r) {
                b[k] = a[j];
                k++;
                j++;
            }
        }
    }
    
    void copy(int l, int r, int *b) {
        int j = 0;
        for (int i = l; i <= r; i++)
            a[i] = b[j++];
    }
    
    void sort(int l, int r) {
        if (l == r) return;
        
        int *b = new int[r - l + 1];
        int mid = (l + r) / 2; 
        sort(l, mid);
        sort(mid + 1, r);
        merge(l, mid, r, b);
        copy(l, r, b);
        delete[] b;
    }
    
    int main() {
        scanf("%d", &n);
        a = new int[n];
        for (int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        sort(0, n - 1);
        for (int i = 0; i < n; i++)
            printf("%d ", a[i]);
        delete[] a;
        return 0;
    } 
    View Code

    2.非递归形式的归并排序

    #include<iostream>
    using namespace std;
    int n;
    int *a;
    
    void merge(int l, int mid, int r, int *b){
        if (r > n - 1) r = n - 1;
        if (mid > n - 1) mid = n - 1;
        int i = l, j = mid + 1, k = 0;
        while (i <= mid && j <= r) {
            if (a[i] < a[j]) {
                b[k] = a[i];
                i++;
            }
            else {
                b[k] = a[j];
                j++;
            }
            k++;
        }
        if (i <= mid) {
            while (i <= mid) {
                b[k] = a[i];
                k++;
                i++;
            }
        }
        if (j <= r) {
            while (j <= r) {
                b[k] = a[j];
                k++;
                j++;
            }
        }
    }
    
    void copy(int l, int r, int *b) {
        if (r > n - 1) r = n - 1;
        int j = 0;
        for (int i = l; i <= r; i++)
            a[i] = b[j++];
    }
    
    void sort(){
        for (int i = 1; i < n; i += i){
            for (int j = 0; j < n; j += 2*i){
                int *b = new int[i];
                merge(j, j+i-1, j+2*i-1, b);
                copy(j, j+2*i-1, b);
                delete[] b;
            }    
        }
    }
    
    int main() {
        scanf("%d", &n);
        a = new int[n];
        for (int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        sort();
        for (int i = 0; i < n; i++)
            printf("%d ", a[i]);
        delete[] a;
        return 0;
    }
    View Code

    3.快速排序

    #include<iostream>
    using namespace std;
    int n;
    int *a;
    
    int partition(int left, int right)
    {
        int i = left + 1 ;
        int j = right;
        int temp = a[left];
    
        while(i <= j)
        {
            while (a[i] < temp) i++;
            while (a[j] > temp ) j--;
            if (i < j) swap(a[i++], a[j--]);
            else i++;
        }
        swap(a[j], a[left]);
        return j;
    
    }
    
    void quick_sort(int left, int right) 
    {
        if (left > right)
            return;
        int j = partition(left, right);
        quick_sort(left, j - 1);
        quick_sort(j + 1, right);
    }
    
    int main() {
        scanf("%d", &n);
        a = new int[n];
        for (int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        quick_sort(0, n - 1);
        for (int i = 0; i < n; i++)
            printf("%d ", a[i]);
        delete[] a;
        return 0;
    }
    View Code
  • 相关阅读:
    Python3 session实现带密码访问网站后台
    Python3实现利用url请求百度翻译
    Python3 parse模块
    Python3 requests模块
    Nignx gzip 文件压缩
    Cnetos7 Yum安装Ningx
    喜极而泣,我终于学会了Nginx!
    Centos7 给磁盘创建Lvm虚拟盘
    Pyhton Tkinter图形工具(原创)
    nginx四种均衡策略
  • 原文地址:https://www.cnblogs.com/astonc/p/11661378.html
Copyright © 2011-2022 走看看