算法打卡之排序

快排

基于分治的思想

模板题:

给定你一个长度为 n的整数数列。

请你使用快速排序对这个数列按照从小到大进行排序。

并将排好序的数列按顺序输出。

输入格式

输入共两行,第一行包含整数 n。

第二行包含 n个整数(所有整数均在 1∼109 范围内),表示整个数列。

输出格式

输出共一行,包含 n个整数,表示排好序的数列。

数据范围

1≤n≤100000

输入样例:
5
3 1 2 4 5
输出样例:
1 2 3 4 5
输入样例:
2
2 1
输出样例:
1 2
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void quick_sort(int[] a, int begin, int end){
        if(begin>=end)    return;
        int x = a[begin], t = 0;
        int i = begin-1, j = end+1;
        while (i<j){
            while (a[++i]<x);
            while (a[--j]>x);
            if(i<j){
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
        quick_sort(a,begin,j);
        quick_sort(a,j+1,end);
    }

    public static void main(String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.valueOf(bufferedReader.readLine());
        int[] a = new int[n];
        String[] str = bufferedReader.readLine().split(" ");
        for (int i=0;i<n;i++){
            a[i] = Integer.parseInt(str[i]);
        }
        quick_sort(a,0,n-1);
        for (int i=0;i<n;i++){
            System.out.print(a[i]+" ");
        }
        System.out.println();
    }
}

例题:第k个数:

给定一个长度为 n的整数数列,以及一个整数 k,请用快速选择算法求出数列从小到大排序后的第 k个数。

输入格式

第一行包含两个整数 n 和 k。

第二行包含 n个整数(所有整数均在 1∼109 范围内),表示整数数列。

输出格式

输出一个整数,表示数列的第 k小数。

数据范围

1≤n≤100000,
1≤k≤n

输入样例:
5 3
2 4 1 5 3
输出样例:
3
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void quick_sort(int[] a, int begin, int end){
        if(begin>=end) return;
        int x = a[begin], t = 0;
        int i = begin-1, j = end+1;
        while (i<j){
            while (a[++i]<x);
            while (a[--j]>x);
            if(i<j){
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
        quick_sort(a,begin,j);
        quick_sort(a,j+1,end);
    }

    public static void main(String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.valueOf(str[0]);
        int k = Integer.valueOf(str[1]);
        int[] a = new int[n];
        str = bufferedReader.readLine().split(" ");
        for (int i=0;i<n;i++){
            a[i] = Integer.parseInt(str[i]);
        }
        quick_sort(a,0,n-1);
        System.out.println(a[k-1]);
    }
}

归并排序

基于分治的思想

模板题:

给定你一个长度为 n的整数数列。

请你使用归并排序对这个数列按照从小到大进行排序。

并将排好序的数列按顺序输出。

输入格式

输入共两行,第一行包含整数 n。

第二行包含 n个整数(所有整数均在 1∼109范围内),表示整个数列。

输出格式

输出共一行,包含 n个整数,表示排好序的数列。

数据范围

1≤n≤100000

输入样例:
5
3 1 2 4 5
输出样例:
1 2 3 4 5
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void mergeSort(int[] a, int begin, int end){
        if(begin>=end) return;
        int mid = (begin+end)/2;
        mergeSort(a,begin,mid);
        mergeSort(a,mid+1,end);
        merge(a,begin,end,mid);
    }

    public static void merge(int[] a, int i, int j, int k){
        int n = j-i+1;
        int[] c = new int[n];
        int x = i, y = k+1, t = 0;
        while (x<=k&&y<=j){
            if(a[x]<a[y])    c[t++] = a[x++];
            else    c[t++] = a[y++];
        }
        while (x<=k)     c[t++] = a[x++];
        while (y<=j)    c[t++] = a[y++];
        for (t=0;t<n;t++){
            a[i++] = c[t];
        }
    }

    public static void main(String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.valueOf(str[0]);
        int[] a = new int[n];
        str = bufferedReader.readLine().split(" ");
        for (int i=0;i<n;i++){
            a[i] = Integer.parseInt(str[i]);
        }
        mergeSort(a,0,n-1);
        for (int i=0;i<n;i++){
            System.out.print(a[i]+" ");
        }
        System.out.println();
    }
}

例题:逆序对的数量:

给定一个长度为 n的整数数列,请你计算数列中的逆序对的数量。

逆序对的定义如下:对于数列的第 i个和第 j个元素,如果满足 i<j 且 a[i]>a[j],则其为一个逆序对;否则不是。

输入格式

第一行包含整数 n,表示数列的长度。

第二行包含 n个整数,表示整个数列。

输出格式

输出一个整数,表示逆序对的个数。

数据范围

1≤n≤100000,
数列中的元素的取值范围 [1, 1 0 9 10^9 109]。

输入样例:
6
2 3 4 5 6 1
输出样例:
5
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    static long res = 0;

    public static void mergeSort(int[] a, int begin, int end) {
        if (begin >= end) return;
        int mid = (begin + end) / 2;
        mergeSort(a, begin, mid);
        mergeSort(a, mid + 1, end);
        merge(a, begin, end, mid);
    }

    public static void merge(int[] a, int i, int j, int k) {
        int n = j - i + 1;
        int[] c = new int[n];
        int x = i, y = k + 1, t = 0;
        while (x <= k && y <= j) {
            if (a[x] <= a[y]) c[t++] = a[x++];
            else {
                c[t++] = a[y++];
                res += (k - x + 1);
            }
        }
        while (x <= k) c[t++] = a[x++];
        while (y <= j) c[t++] = a[y++];
        for (t = 0; t < n; t++) {
            a[i++] = c[t];
        }
    }

    public static void main(String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.valueOf(str[0]);
        int[] a = new int[n];
        str = bufferedReader.readLine().split(" ");
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(str[i]);
        }
        mergeSort(a, 0, n - 1);
        System.out.println(res);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

I'm 程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值