将值从一个数组插入到另一个直接排序的数组
Insert values from one array to another directly sorted
所以我要做的是将某些值从一个数组插入到另一个数组直接排序,而不必在以后使用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
相关文章:
- C++json插入数组
- 正在插入动态数组
- 如何在向量数组中插入元素?
- 将值插入到标准 c++ 数组的正确索引(升序)中
- C++ - 将元素按升序插入数组的 SEG 错误
- 将二维数组的所有元素插入到一维数组中
- 插入数组时违反写访问冲突
- 尝试将对象插入空指针数组时出现分段错误
- 类数组插入函数
- 将数组的元素插入映射的时间复杂度是多少?
- 当索引位于末尾和空数组时,向数组插入元素时出现问题
- C++ 使用数组插入到二叉搜索树中
- 将 2D 数组插入 std::vector 时"cannot convert from 'const GLfloat [12]' to '_Objty'"错误消息
- 如何使用 mongo-cxx-driver 的 **insert** 函数将包含静态数据的多维数组插入到数据库中
- 如何将字符数组插入浮点向量
- 如何将数组插入无序集合
- 用c++将数组数组插入mongo中
- visual C++将整数的二维数组插入到另一个整数的二维阵列中
- 优化数组插入C/ c++
- c++如何将数组插入哈希集