将值从一个数组插入到另一个直接排序的数组

Insert values from one array to another directly sorted

本文关键字:数组 插入 另一个 排序 一个      更新时间:2023-10-16

所以我要做的是将某些值从一个数组插入到另一个数组直接排序,而不必在以后使用BubbleSort或QuickSort或任何其他方法进行排序。我想不出一个办法来做这件事。我要把它们从最大值插入到最小值。以下是我到目前为止的记录:

void palindroame (int x[100], int y[100]) {
    int i=0, j, k=0, aux;
    while (x[i]!=0) { 
        k++; i++; 
    }
    i=0;
    for (i=0; i<=k-1; i++) y[i]=0;
    for (i=0; i<=k-1; i++) { 
        if (palindrom(x[i])!=0 && palindrom(x[i+1])!=0) 
            if (x[i]<x[i+1]) { 
                aux=x[i+1]; x[i+1]=x[i]; x[i]=aux; 
            } 
    } //wrong
    for (i=0; i<=k-1; i++) { 
        if (palindrom(x[i])) y[i]=x[i]; 
    } //wrong
}

提前感谢!

你需要的算法是选择排序,你可以用它来排序和复制在同一时间

您可以查看优先级队列:

http://www.cplusplus.com/reference/queue/priority_queue/

这是我最近做的一个选择排序的例子(其中a是一个向量)

应该给你足够的力量继续下去希望它能有所帮助,如果你想问问题

for (unsigned int i = 0; i < a.size()-1; i++)
{
    int min = i;
    for(unsigned int j = i +1; j < a.size(); j++)
    {
        // If new minimum is found then stores this as the new minimum
        if(a[j] < a[min])
        {
            min = j;
        }
    }
    // Stores the values in the array in ascending order
    if (min != i)
    {
        int temp = a[i];
        a[i] = a[min];
        a[min] = temp;
    }
}
// Returns the array in ascending order
return a;
编辑:只是为了澄清这是在一个已经有值的矢量上工作,以防不清楚,但我认为有代码注释的例子足以帮助你IMO