kth number

kth number

Given an integer sequence of length n, and an integer k, please use the quick selection algorithm to find the kth number after sorting the sequence from small to large.

input format
The first line contains two integers n and k
The second row contains n integers (all integers in the range 1-1e9), representing the sequence of integers.

output format
Output an integer representing the kth decimal of the sequence.

data range
1≤n≤100000
1≤k≤n

Input sample:
5 3
2 4 1 5 3
Sample output:
3

Solutions

This code implements an algorithm to find the kth smallest number in an array. The quick_sort function in the code uses the idea of quick sorting to find the kth smallest number by recursively sorting the sub-arrays.

Specifically, the parameters of the quick_sort function include an integer array q, two integer variables l and r representing the left and right boundaries of the subarray to be sorted, and an integer variable t representing the kth smallest number to be found. The function returns the kth smallest number in the subarray q[l...r].

In the quick_sort function, we first select a reference value mid, and then place the elements in the array that are less than or equal to the reference value to the left of the reference value, and the elements greater than the reference value to the right of the reference value. We then recursively sort the left and right halves until we find the kth smallest number.

In the recursive process, we need to determine which part the kth smallest number is in. If the kth smallest number is in the left half, we recursively sort the left half; if the kth smallest number is in the right half, we recursively sort the right half and decrement the value of k by Go the length of the left half.

In the main function, we first read in the length n of the array and the kth smallest number to find. Then, we read the elements of the array q, and call the quick_sort function to find the kth smallest number, and output the result.

Overall, this code implements an efficient algorithm for finding the kth smallest number in an array with a time complexity of O(nlogn).

#include <iostream>

using namespace std;

const int N = 100010;

int n, k;
int q[N];

int quick_sort(int q[], int l, int r, int t) {
    if (l >= r) return q[l];
    int mid = q[(l + r) >> 1], i = l - 1, j = r + 1;
    while (i < j) {
        do (i ++ ); while(q[i] < mid);
        do (j -- ); while(q[j] > mid);
        if (i < j) swap(q[i], q[j]);
    }
    if ((j - l  + 1) >= t) return quick_sort(q, l, j, t);
    else return quick_sort(q, j + 1, r, t - (j - l + 1));
}

int main() {
    cin >> n >> k;

    for (int i = 0; i < n; i ++ ) cin >> q[i];

    cout << quick_sort(q, 0, n - 1, k) << endl;

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值