C++ 第一k元素排序算法
它给了我错误的输出。有什么问题吗。 我想把数字按这种方式排序,我怎么做? 如何移动数组并将数字插入到正确的位置C++ 第一k元素排序算法,c++,C++,它给了我错误的输出。有什么问题吗。 我想把数字按这种方式排序,我怎么做? 如何移动数组并将数字插入到正确的位置 int a; int N = 0; cin >> N; // Taking size of the inputs int* numbers = new int[k]; // ı want the kth greatest number . int x = 0; while (x < k) { cin >> a; numbers[x]
int a;
int N = 0;
cin >> N; // Taking size of the inputs
int* numbers = new int[k]; // ı want the kth greatest number .
int x = 0;
while (x < k) {
cin >> a;
numbers[x] = a;
x++;
}
int i, anahtar, j; // after ı take k number from user, I Sort the numbers via insertion sort
for (i = 1; i < k; i++)
{
anahtar = numbers[i];
j = i - 1;
while (j >= 0 && numbers[j] > anahtar)
{
numbers[j + 1] = numbers[j];
j = j - 1;
}
numbers[j + 1] = anahtar;
}
// then I am taking other numbers one by one and ı am comparing with numbers in the array. After ı insert it the correct place, shifthing the array to the right
for (int e = x; e < N; e++) {
cin >> a;
if (a > numbers[k-1]) {
for (int i = k-2; i >= 0; i--) {
if (i == 0) {
numbers[i+1] = numbers[i];
numbers[i] = a;
}
else if (a > numbers[i]) {
numbers[i+1] = numbers[i];
}
else {
numbers[i+1] = a;
}
}
}
}
return numbers[k -1];
inta;
int N=0;
cin>>N;//考虑输入的大小
整数*数字=新整数[k];//ı想要第k个最大的数字。
int x=0;
while(x>a;
数字[x]=a;
x++;
}
int i,anahtar,j;//在ı从用户处获取k编号后,我通过插入排序对编号进行排序
对于(i=1;i=0&&numbers[j]>anahtar)
{
数字[j+1]=数字[j];
j=j-1;
}
数字[j+1]=一个数;
}
//然后我一个接一个地取其他数字,并与数组中的数字进行比较。ı插入正确的位置后,将数组向右移动
对于(int e=x;e>a;
如果(a>数字[k-1]){
对于(int i=k-2;i>=0;i--){
如果(i==0){
数字[i+1]=数字[i];
数字[i]=a;
}
如果(a>数字[i]){
数字[i+1]=数字[i];
}
否则{
数字[i+1]=a;
}
}
}
}
返回编号[k-1];
欢迎来到堆栈溢出。请阅读,获取,阅读,以及。听起来像你要找的是std::nth\u元素
并按降序排序。