zoukankan      html  css  js  c++  java
  • 215 数组中的第K个最大元素

    快速排序的思想:

    利用 patition 函数,选出一个 pivot,将比 pivot 大的元素放在pivot的右边,把 pivot 小的元素放在 pivot 的左边,这样 pivot 的最终位置就确定了。

    import java.util.Arrays;
    
    public class Main {
        public static void main(String[] args) {
            int[] nums = new int[]{6,-2,4,10,5};
            quickSort(nums, 0, nums.length - 1);
            System.out.println(Arrays.toString(nums));
        }
    
        public static void quickSort(int[] nums, int l, int r){
            if(l < r){
                //index数组中某数字的下标
                int index = patition(nums, l, r);
                //左右子数组递归排序
                quickSort(nums, l, index - 1);
                quickSort(nums, index + 1, r);
            }
        }
    
        public static int patition(int[] nums, int l, int r){
            //每次选中的 pivot 是最右边的元素
            int pivot = nums[r];
            int i = l - 1;//
            for(int j = l; j < r; j++){
                if(nums[j] < pivot){
                    i ++;
                    swap(nums, i, j);
                }
            }
            swap(nums, i + 1, r);//把基准放在它最终的位置
            return i + 1;
        }
    
        public static void swap(int[] nums, int i, int j){
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
    }
    
    /*
    [-2, 4, 5, 6, 10]
     */
    
    

    代码

    import java.util.*;
    /*
    利用快速排序的patition函数,确定第 k 大的元素的位置即可
     */
    class Solution {
        Random random = new Random();
        public int findKthLargest(int[] nums, int k) {
            return quickSelect(nums, 0, nums.length - 1, nums.length - k);
        }
        /*
        快速选择
         */
        public int quickSelect(int[] a, int l, int r, int index){
            int q = randomPatition(a, l, r);
            if(q == index){
                return a[q];
            }
            else{
                return q < index ? quickSelect(a, q + 1, r, index) : quickSelect(a, l, q - 1, index);
            }
        }
        /*
        随机分
         */
        public int randomPatition(int[] a, int l, int r){
            int i = random.nextInt(r - l + 1) + l;
            swap(a, i, r);
            return patition(a, l, r);
        }
        /*
        将数组分成两半,返回中间数的位置
         */
        public int patition(int[] a, int l, int r){
            int x = a[r], i = l - 1;
            for(int j = l; j < r; j++){
                if(a[j] <= x){
                    swap(a, ++i, j);
                }
            }
            swap(a, i + 1, r);
            return i + 1;
        }
        /*
        交换两数
         */
        public void swap(int[] a, int i, int j){
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
    
  • 相关阅读:
    POJ 2155 Matrix(二维树状数组)
    HDU 1280 前m大的数
    HDU 3183 A Magic Lamp(二维RMQ)
    HDU 3743 Frosh Week(归并排序求逆序数)
    POJ 2299 Ultra-QuickSort ( 归并排序 + 求逆序数 )
    HDU 1166 敌兵布阵(树状数组)
    HDU 2846 Repository(字典树)
    HDU 1896 Stones(优先队列)
    HDU 4393 Throw nails(优先队列)
    进程池
  • 原文地址:https://www.cnblogs.com/realzhaijiayu/p/13537798.html
Copyright © 2011-2022 走看看