(Java)【深基9.例4】求第 k 小的数

【深基9.例4】求第 k 小的数

一、题目描述

输入 n n n 1 ≤ n < 5000000 1 \le n < 5000000 1n<5000000 n n n 为奇数)个数字 a i a_i ai 1 ≤ a i < 10 9 1 \le a_i < {10}^9 1ai<109),输出这些数字的第 k k k 小的数。最小的数是第 0 0 0 小。

请尽量不要使用 nth_element 来写本题,因为本题的重点在于练习分治算法。

二、样例输入 #1
5 1
4 3 2 1 5
三、样例输出 #1
2
四、错误经历

使用了快速排序的标准模板,超时了

import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        quickSort(a, 0, n - 1);
        System.out.println(a[k]);
    }

    public static void quickSort(int[] a, int low, int high) {
        int i,j;
        int temp,t;
        if (low > high){
            return;
        }
        i = low;
        j = high;
        //temp就是基准位
        temp = a[low];

        while (i < j){
            //先看右边,依次往左递减
            while (a[j] >= temp && j > i){
                j--;
            }
            //再看左边,依次往右递增
            while (a[i] <= temp && i < j){
                i++;
            }
            //如果满足条件则交换
            if (i < j){
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
        //最后将基准为与i和j相等位置的数字交换
        a[low] = a[i];
        a[i] = temp;
        //递归调用左半数组
        quickSort(a,low,j - 1);
        //递归调用右半数组
        quickSort(a,j + 1,high);
    }
}

在这里插入图片描述

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        Quick_Sort(a, 0, n - 1);
        System.out.println(a[k]);
    }

    private static void Quick_Sort(int[] a, int l, int r) {
        if (l >= r){
            return;
        }
        int x = a[l];
        int i = l - 1;
        int j = r + 1;
        while (i < j){
            do {
                i++;
            }while (a[i] < x);
            do {
                j--;
            }while (a[j] > x);
            if (i < j){
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
        Quick_Sort(a,l,j);
        Quick_Sort(a,j+1,r);
    }
}
import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        Quick_Sort(a, 0, n - 1,k);
        System.out.println(a[k]);
    }

    public static void Quick_Sort(int arr[], int begin, int end,int k){
        if(begin > end)
            return;
        int tmp = arr[begin];
        int i = begin;
        int j = end;
        while(i != j){
            while(arr[j] >= tmp && j > i)
                j--;
            while(arr[i] <= tmp && j > i)
                i++;
            if(j > i){
                int t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
            }
        }
        arr[begin] = arr[i];
        arr[i] = tmp;
        if(k == i || k == j) {
            return;
        } else if(k < i){
            Quick_Sort(arr, begin, i-1,k);
        }else if(k > j){
            Quick_Sort(arr, j+1, end,k);
        }else {
            Quick_Sort(arr, i, j,k);
        }
    }
}

在这里插入图片描述
直接使用了Arrays.sort( ),也超时了请添加图片描述

import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        Arrays.sort(a);
        System.out.println(a[m]);
    }
}

在这里插入图片描述

我废了,栓Q

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

呦呦呦欸哟哟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值