Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C++ to select the k-th small number in an unordered array

Shulou Source: shulou.com Published: 2022-05-31 22:48:00 09月18日 Update

Today, the editor will share with you how to use C++ to select the k smallest number in an unordered array. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge. So share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Select the k-th smallest number from an unordered array of integers, such as KF1 is the minimum number and KFN is the maximum number. Here the array can have duplicate values!

The following is a function written by myself, write it down here to remember the traces I left!

/ / Select the k-th number in the unordered array # include using namespace std; bool failed = false; / / only consider int findnumber (int * array,int start, int end, int k) {if (array = = NULL | | start > end | | k) whose array is int.

< start || k >

End+1 | | k tk) {end = f;} else {start = f + 1; tk = k-start; / / this place made a mistake, that is, it was written as k=k-start, and always found infinite loops when debugging. Later, when printing the value of k, it was found that the value of k was negative. This bug, this fault makes it possible to get the correct data in one run, but the program crashes after many runs. } len = end-start + 1; ps = rand ()% len + start;}} int main () {int array [10] = {1 cout 1, 2, 4, 1, 1}; for (int I = 0; I < 10; I + +) {cout

Tags: Array knowledge article selection number Category real content that is data time fault face loop run different very large maximum minimum function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Information Linux Shulou Technology Shulou Tech Info