Find an array of k small numbers

Time complexity of O (n)

#include <stdio.h>
int SearchKth(int a[],int L,int R,int k)
{
    int i=L,j=R;
    int pivot=a[i];
    while(i<j)
    {
        while(i<j&&a[j]>=pivot) --j;
        a[i]=a[j];
        while(i<j&&a[i]<=pivot) ++i;
        a[j]=a[i];
    }
    a[i]=pivot;
    if(i==k-1) return a[i];
    else if(i<k-1) SearchKth(a,i+1,R,k);
    else SearchKth(a,L,i-1,k);
}
int main()
{
    int a[]={9,4,5,7,3,6,2,1,8};
    int k=3;
    int ans=SearchKth(a,0,8,k);
    printf("the k-th number is %d", ans);
    return 0;
}

Intelligent Recommendation

Full array, a subset of all, to find the array of small value k

1. The full array:   2. subset of the total   3. find a small number of first K in an array Ideas: ①:Array with a variable length of the mark is inconvenient, then the array with the ends po...

PubMed data structure 1_ find the small numbers K

First, the idea of ​​analysis For indexes start at zero, no sentry position.If a quick sort ofDivideIdea, the idea combined with the specific problem (find the small k) ideas formed as follows: accord...

[Algorithm] to find the first small numbers [k] JS achieve

problem: the known sequence of A [1 ... n], and an integer k, 1⇐k⇐n, try to find a smaller number of A, k This problem is generally referred to as order statistics or Selection. Conventional...

A daily algorithm problem (32) - the output array of k small numbers

1. Topic Fast output of small numbers K 2. Ideas Use quick sort of thinking, recursive solution. If key positions i and k are equal,. If more than k, the k-th looking at a large number of [start, i-1]...

Algorithm Design - Find disorderly array of large numbers K

A given array, this array required to find out the order ranked K large numbers. Idea: using the idea of ​​quick sort, the elements of the partition of the array, and calculates the position of the in...

More Recommendation

Find the top K smallest numbers in an array-heap sort

Use heap sort to find the smallest k number in the array:...

Algorithm, find the smallest K numbers in the array, return K numbers in any order

import java.util.PriorityQueue; public class smallestK { /** * Design an algorithm to find the smallest K numbers in the array, and return K numbers in any order * Input: arr = [1,3,5,7,2,4,6,8] * Out...

(Minimum K number) Design an algorithm to find the smallest k number in the array. These k numbers can be returned in any order.

Design an algorithm to find the smallest k number in the array. These k numbers can be returned in any order. Example: Input: arr = [1,3,5,7,2,4,6,8], k = 4 Output: [1,2,3,4] prompt: 0 <= len(arr) ...

Find the two already sorted array of K small number [# 66]

Preface: This question is a very common interview questions, but also an ability to be able to examine a question programming and algorithms of a person. If required complexity is O (k), it is relativ...

BFPRT algorithm-find the first k small number in an unordered array

1. In the problem of finding the first k small numbers in an unordered array, the classic algorithm: Using quick sorting, a number is randomly selected as the division value, divided into areas less t...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top