构造一个体现出stl::sort不稳定性的测试用例

laomai 2008-04-24 09:48:38
最近在学习stl中的算法函数sort。根据我的个人理解,标准并未保证这个函数的排序一定是稳定的(stable_sort一定是稳定)。于是我写了一个程序来测试sort的稳定性。但是由于我手上目前只有vs系列的编译器,而他们对sort的内部实现是稳定的,因此没得到我想要的结果,希望大家能帮忙把我的程序在你的机器上测试一下,如果能得到最后的输出,请将测试用例或者结果及运行环境告诉我,谢谢!
以下是我的测试程序
/*本程序的目的是构造出一个不稳定的sort函数的输入数据*/
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

//
typedef struct {
int index; //下标
int value; //值
}Data;
bool operator==(const Data& lhs, const Data& rhs){
return lhs.value == rhs.value;
}

bool operator<(const Data& lhs, const Data& rhs){
return lhs.value < rhs.value;
}

//判断数组中是否有相等值,数组应该已经排好序
bool HasSameValue(const vector<Data>& src){
if (adjacent_find(src.begin(), src.end()) != src.end()){
cout<<"has same value."<<endl;
return true;
}
else
{
cout<<"no same value."<<endl;
return false;
}
}

bool IsGreaterIndex(const Data& lhs, const Data& rhs){
return (lhs.value == rhs.value
&& lhs.index > rhs.index);

}

//判断等值元素中是否有下标递减的情况,
bool HasReverseIndex(const vector<Data>& src){
if (adjacent_find(src.begin(), src.end(), IsGreaterIndex) != src.end()){
cout<<"has great index."<<endl;
return true;
}
else
{
cout<<"no great index."<<endl;
return false;
}
}


const int NUMBER = 100;
const int SIZE=10;
const int RANNUM = 4;
void ResetData(vector<Data>& src){
int i=0;
vector<int> ran(RANNUM);
for(i=0; i<RANNUM; ++i){
ran[i] = rand()%NUMBER;
}
for(i=0; i<SIZE; ++i){
src[i].index = i;
src[i].value = ran[rand()%RANNUM];
}
}
void Print(const vector<Data>& src){
cout<<'{';
int i=0;
for(i=0; i<SIZE; ++i) {
cout<<'('<<src[i].value<<','<<src[i].index<<"),";
if ((i+1)%5==0)
cout<<" } "<<endl;
}
}

int main(int argc ,char* argv[]){
srand(static_cast<unsigned int>(time(NULL)));
vector<Data> src(SIZE), temp(SIZE);
do{
ResetData(src);
cout<<"src: "<<endl;
Print(src);
temp = src;
sort(src.begin(), src.end());
cout<<"after sort: "<<endl;
Print(src);
}while(!HasSameValue(src) || !HasReverseIndex(src));
return 0;
}
...全文
519 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
jieao111 2008-08-31
  • 打赏
  • 举报
回复
#include <algorithm> 
#include <functional>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

//
typedef struct {
int index; //下标
int value; //值
}Data;
bool operator==(const Data& lhs, const Data& rhs){
return lhs.value == rhs.value;
}

bool operator <(const Data& lhs, const Data& rhs){
return lhs.value < rhs.value;
}

//判断数组中是否有相等值,数组应该已经排好序
bool HasSameValue(const vector <Data>& src){
if (adjacent_find(src.begin(), src.end()) != src.end()){
cout < <"has same value." < <endl;
return true;
}
else
{
cout < <"no same value." < <endl;
return false;
}
}

bool IsGreaterIndex(const Data& lhs, const Data& rhs){
return (lhs.value == rhs.value
&& lhs.index > rhs.index);

}

//判断等值元素中是否有下标递减的情况,
bool HasReverseIndex(const vector <Data>& src){
if (adjacent_find(src.begin(), src.end(), IsGreaterIndex) != src.end()){
cout < <"has great index." < <endl;
return true;
}
else
{
cout < <"no great index." < <endl;
return false;
}
}


const int NUMBER = 100;
const int SIZE=10;
const int RANNUM = 4;
void ResetData(vector <Data>& src){
int i=0;
vector <int> ran(RANNUM);
for(i=0; i <RANNUM; ++i){
ran[i] = rand()%NUMBER;
}
for(i=0; i <SIZE; ++i){
src[i].index = i;
src[i].value = ran[rand()%RANNUM];
}
}
void Print(const vector <Data>& src){
cout < <'{';
int i=0;
for(i=0; i <SIZE; ++i) {
cout < <'(' < <src[i].value < <',' < <src[i].index < <"),";
if ((i+1)%5==0)
cout < <" } " < <endl;
}
}

int main(int argc ,char* argv[]){
srand(static_cast <unsigned int>(time(NULL)));
vector <Data> src(SIZE), temp(SIZE);
do{
ResetData(src);
cout < <"src: " < <endl;
Print(src);
temp = src;
sort(src.begin(), src.end());
cout < <"after sort: " < <endl;
Print(src);
}while(!HasSameValue(src) ¦ ¦ !HasReverseIndex(src));
return 0;
}
jieao111 2008-08-31
  • 打赏
  • 举报
回复
#include <algorithm> 
#include <functional>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

//
typedef struct {
int index; //下标
int value; //值
}Data;
bool operator==(const Data& lhs, const Data& rhs){
return lhs.value == rhs.value;
}

bool operator <(const Data& lhs, const Data& rhs){
return lhs.value < rhs.value;
}

//判断数组中是否有相等值,数组应该已经排好序
bool HasSameValue(const vector <Data>& src){
if (adjacent_find(src.begin(), src.end()) != src.end()){
cout < <"has same value." < <endl;
return true;
}
else
{
cout < <"no same value." < <endl;
return false;
}
}

bool IsGreaterIndex(const Data& lhs, const Data& rhs){
return (lhs.value == rhs.value
&& lhs.index > rhs.index);

}

//判断等值元素中是否有下标递减的情况,
bool HasReverseIndex(const vector <Data>& src){
if (adjacent_find(src.begin(), src.end(), IsGreaterIndex) != src.end()){
cout < <"has great index." < <endl;
return true;
}
else
{
cout < <"no great index." < <endl;
return false;
}
}


const int NUMBER = 100;
const int SIZE=10;
const int RANNUM = 4;
void ResetData(vector <Data>& src){
int i=0;
vector <int> ran(RANNUM);
for(i=0; i <RANNUM; ++i){
ran[i] = rand()%NUMBER;
}
for(i=0; i <SIZE; ++i){
src[i].index = i;
src[i].value = ran[rand()%RANNUM];
}
}
void Print(const vector <Data>& src){
cout < <'{';
int i=0;
for(i=0; i <SIZE; ++i) {
cout < <'(' < <src[i].value < <',' < <src[i].index < <"),";
if ((i+1)%5==0)
cout < <" } " < <endl;
}
}

int main(int argc ,char* argv[]){
srand(static_cast <unsigned int>(time(NULL)));
vector <Data> src(SIZE), temp(SIZE);
do{
ResetData(src);
cout < <"src: " < <endl;
Print(src);
temp = src;
sort(src.begin(), src.end());
cout < <"after sort: " < <endl;
Print(src);
}while(!HasSameValue(src) ¦ ¦ !HasReverseIndex(src));
return 0;
}
huang_8228 2008-04-28
  • 打赏
  • 举报
回复
路过
laomai 2008-04-26
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 sheenl 的回复:]
这样的方法, 你永远看不到不稳定的情况的。
没看错的话, 你的数组才10个元素。 而vs2005的sort算法, 32元素以内的时候采用插入排序, 32元素以上采用
快速排序和插入排序共同工作, 所以实际上你的排序是插入排序完成的, 是稳定的排序。

注: vc6.0 和 sgi stl则是以16作为阀值, 16元素以下采用插入排序, 16元素以上采用快速排序和插入排序共同
工作。
[/Quote]
多谢sheenl的指点,我前天在飞雪的帮助下也找到了这个原因。
在vc6下把数组扩到20就得到了我希望的结果。
准备结贴了。
JoyerHuang_悦 2008-04-26
  • 打赏
  • 举报
回复
你只能看源代码来确认,很不幸。。。
因为,无数个测试样本的成功也只能是必要条件,而不是充分条件~~
hastings 2008-04-25
  • 打赏
  • 举报
回复
无耻占位。
编程夜猫 2008-04-25
  • 打赏
  • 举报
回复
牛人。学习了。正好也刚学习STL,不过还没到算法。先学了。呵呵。
con_con 2008-04-25
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 sheenl 的回复:]
这样的方法, 你永远看不到不稳定的情况的。
没看错的话, 你的数组才10个元素。 而vs2005的sort算法, 32元素以内的时候采用插入排序, 32元素以上采用
快速排序和插入排序共同工作, 所以实际上你的排序是插入排序完成的, 是稳定的排序。

注: vc6.0 和 sgi stl则是以16作为阀值, 16元素以下采用插入排序, 16元素以上采用快速排序和插入排序共同
工作。
[/Quote]

有道理
babyvox1999 2008-04-25
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hbqing_ting 的回复:]
好复杂啊!
[/Quote]
sheenl 2008-04-25
  • 打赏
  • 举报
回复
这样的方法, 你永远看不到不稳定的情况的。
没看错的话, 你的数组才10个元素。 而vs2005的sort算法, 32元素以内的时候采用插入排序, 32元素以上采用
快速排序和插入排序共同工作, 所以实际上你的排序是插入排序完成的, 是稳定的排序。

注: vc6.0 和 sgi stl则是以16作为阀值, 16元素以下采用插入排序, 16元素以上采用快速排序和插入排序共同
工作。
  • 打赏
  • 举报
回复
复杂,不懂
星羽 2008-04-24
  • 打赏
  • 举报
回复
复杂好啊!
hbqing_ting 2008-04-24
  • 打赏
  • 举报
回复
好复杂啊!
healer_kx 2008-04-24
  • 打赏
  • 举报
回复
我师父太有学习热情了。。。
baihacker 2008-04-24
  • 打赏
  • 举报
回复
1.稳定性比较
插入排序、冒泡排序、二叉树排序、二路归并排序及其他线形排序是稳定的
选择排序、希尔排序、快速排序、堆排序是不稳定的
2.时间复杂性比较
插入排序、冒泡排序、选择排序的时间复杂性为O(n2)
其它非线形排序的时间复杂性为O(nlog2n)
线形排序的时间复杂性为O(n);
3.辅助空间的比较
线形排序、二路归并排序的辅助空间为O(n),其它排序的辅助空间为O(1);
4.其它比较
插入、冒泡排序的速度较慢,但参加排序的序列局部或整体有序时,这种排序能达到较快的速度。
反而在这种情况下,快速排序反而慢了。
当n较小时,对稳定性不作要求时宜用选择排序,对稳定性有要求时宜用插入或冒泡排序。
若待排序的记录的关键字在一个明显有限范围内时,且空间允许是用桶排序。
当n较大时,关键字元素比较随机,对稳定性没要求宜用快速排序。
当n较大时,关键字元素可能出现本身是有序的,对稳定性有要求时,空间允许的情况下,宜用归并排序。
当n较大时,关键字元素可能出现本身是有序的,对稳定性没有要求时宜用堆排序
星羽 2008-04-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 Treazy 的回复:]
不给我100分,我就 bs 你
[/Quote]
Treazy 2008-04-24
  • 打赏
  • 举报
回复
不给我100分,我就 bs 你
yuanchuang 2008-04-24
  • 打赏
  • 举报
回复
BS
baihacker 2008-04-24
  • 打赏
  • 举报
回复
加一个int i = 0;和 while (...&&i++<5)
Treazy 2008-04-24
  • 打赏
  • 举报
回复

gcc 3.4.4

始终运行,无法停止

加载更多回复(3)

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧