C++合并排序实现
C++ merge sort implement
我想用C++和向量实现合并排序(考虑到整数的输入数量未知)。我试图通过维基百科基于自下而上的实现思想来实现:http://en.wikipedia.org/wiki/Merge_sort.我得出了以下代码。它可以通过编译,但在输入数字时会失效。我不知道这里出了什么问题。
#include<iostream>
#include<vector>
using namespace std;
void BottomUpMerge(vector<int>, vector<int>::iterator, vector<int>::iterator, vector<int>::iterator, vector<int>);
void BottomUpSort(int n, vector<int> A, vector<int> B)
{
int width;
/* Each 1-element run in A is already "sorted". */
/* Make successively longer sorted runs of length 2, 4, 8, 16... until whole array is sorted. */
for (width = 1; width < n; width = 2 * width){
vector<int>::iterator leftstart;
/* Array A is full of runs of length width. */
for (leftstart = A.begin(); leftstart <= A.end(); leftstart = leftstart + 2 * width){
/* Merge two runs: A[i:i+width-1] and A[i+width:i+2*width-1] to B[] */
/* or copy A[i:n-1] to B[] ( if(i+width >= n) ) */
vector<int>::iterator rightstart;
vector<int>::iterator rightend;
if(leftstart+width >= A.end()){
rightstart = A.end();
}
else{
rightstart = leftstart+width;
}
if(leftstart+2*width >= A.end()){
rightend = A.end();
}
else{
rightend = leftstart+2*width;
}
BottomUpMerge(A, leftstart, rightstart, rightend, B);
}
/* Now work array B is full of runs of length 2*width. */
/* Copy array B to array A for next iteration. */
/* A more efficient implementation would swap the roles of A and B */
A = B;
/* Now array A is full of runs of length 2*width. */
}
}
void BottomUpMerge(vector<int> A, vector<int>::iterator iLeft, vector<int>::iterator iRight, vector<int>::iterator iEnd, vector<int> B)
{
vector<int>::iterator i0 = iLeft;
vector<int>::iterator i1 = iRight;
vector<int>::iterator j = B.begin() + distance(iLeft, A.begin()); //iterator for vector B
/* While there are elements in the left or right lists */
for (; j != B.end(); j++){
/* If left list head exists and is <= existing right list head */
if (i0 < iRight && (i1 >= iEnd || *i0 <= *i1)){
*j = *i0;
i0 = i0 + 1;
}
else{
*j = *i1;
i1 = i1 + 1;
}
}
}
int main(){
int input;
vector<int> numbers; //input vector of numbers
cout << "Please input the numbers to be sorted:" << endl;
while(cin>>input){
numbers.push_back(input);
}
int length = numbers.size();
vector<int> sorted = numbers; //work vector ensure has the same size of the original vector
BottomUpSort(length, numbers, sorted);
vector<int>::iterator it;
for(it = numbers.begin(); it != numbers.end(); ++it) {
std::cout << (*it) << 'n';
}
system("pause");
return 1;
}
查看此处:
BottomUpMerge(A, leftstart, rightstart, rightend, B);
...
void BottomUpMerge(vector<int> A, vector<int>::iterator iLeft, ...)
{
...
您正在按值传递向量,这意味着BottomUpMerge
正在处理它们的副本。但迭代器仍然指向原始,随之而来的是欢乐。通过引用传递向量,至少其中一些问题应该会消失。
(在编写所有相关代码之前,您应该在测试此函数时发现了这一点。永远不要添加到不起作用的代码中。)
相关文章:
- 这是插入排序的正确实现吗?
- 如何修复我的快速排序实现?
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 分段 排序函数实现中的错误
- 使用向量在 c++ 中合并排序实现
- 我无法让我的合并排序实现运行
- 为什么在此排序算法实现中,向量明显比数组慢?
- 如何在C++中实现气泡排序?
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- 在实现合并排序代码时无法计算所有反转
- 实现 3 路分区以实现快速排序
- 为什么这个快速排序实现给出了一个奇怪的输出
- 当给出预先排序的输入时,为什么我的快速排序实现很慢
- 定制比较器,用于使用多种排序键来实现排序
- 如何实现排序的指针向量
- 使用模板实现排序.如何使用比较器
- 使用模板实现排序类
- 实现排序的双向链表的困难
- 成员函数的实现:排序算法
- 使用 3 个堆栈实现排序.(分段错误 - 核心转储)