求一个特殊的快速排序

eoppggx 2006-07-22 02:33:17
要求按照KEY里面数值的大小排序,同时将VALUE里面的东西也按照KEY的序列排序
例如:
int akey={6,8,9,5};
int avalue ={1,2,3,4);
排序之后:
akey={5,6,8,9};//按照数值大小来排序
avalue={4,1,2,3};//与key对应排序
...全文
334 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
grrrrrr 2006-07-23
  • 打赏
  • 举报
回复
你不如用一个pair,对键进行排序
rand520 2006-07-23
  • 打赏
  • 举报
回复
把KEY写到key.txt中
然后把value写到value.txt中运行就行了
VC++6编译运行的!!!
rand520 2006-07-23
  • 打赏
  • 举报
回复
#include<iostream>
#include<fstream>
#include<algorithm>
#include<vector>
using namespace std;
//-------------------------------
void main(){
vector<int> key,value,vec1,vec2;
ifstream in1("Key.txt");
for(int a;in1>>a;)key.push_back(a);
ifstream in2("value.txt");
for(int b;in2>>b;)value.push_back(b);
vec1=key;
sort(key.begin(),key.end());
for(int i=0;i<=key.size();i++)
for(int j=0;j<=key.size();j++)if(key[i]==vec1[j])vec2.push_back(j);
for(int m=0;m<key.size();m++)cout<<value[vec2[m]]<<" ";
cout<<endl;
}
写了一个应该可以实现
编译通过运行了
结果正确!!!
rand520 2006-07-23
  • 打赏
  • 举报
回复
以前有个人问相式问题
家个向量做比较 找出排序后愿数位置
然后按位置输出 value里的数据既可
要的话可以写出来~~~~不过来晚了
估计没分不写了!!!
郁郁
ybt631 2006-07-22
  • 打赏
  • 举报
回复
构造一个结构体就可以拉。。
struct temp{}
int iKey;
int iDown; //记录之前的下标
};
排完序之后再重新对数组2赋值就可以了
for(int i=0; i<size; ++i){
array2[i]= temp_array2[temp_array[i].iDown];
}
觉得用map不合适,再插入过程中太费时间了(需要对map树进行平衡处理)
yingle2000 2006-07-22
  • 打赏
  • 举报
回复
#include <STACK.h>

class SkG
{
public:

template<typename pp>
static inline void skG(pp fnt, pp bk)
{
int pi = 0;
pp mo, mc;
STACK pis, fnts, bks, mos;
fnts.push(fnt);
bks.push(bk);
mos.push(mo);
pis.push(3);
while(true)
{
switch(pi)
{
case 0:
mo = fnt;
mc = fnt;
if(++mc == bk || fnt == bk)
{
fnt = POP(fnts);
bk = POP(bks);
mo = POP(mos);
pi = POP(pis);
continue;
}
for(; mc != bk; ++mc)
{
if(*mo > *mc)
swap3(*mo, mo, *mc);
}
PUSH(fnts, fnt);
PUSH(bks, bk);
PUSH(mos, mo);
PUSH(pis, 1);
bk = mo;
continue;
case 1:
PUSH(fnts, fnt);
PUSH(bks, bk);
PUSH(mos, ++mo);
PUSH(pis, 2);
fnt = mo;
pi = 0;
continue;
case 2:
fnt = POP(fnts);
bk = POP(bks);
mo = POP(mos);
pi = POP(pis);
continue;
case 3:
return;
}
}
}

protected:

template<typename vt, typename pp>
static inline void swap3(vt &vt1, pp &pt, vt &vt2)
{
vt temp = vt2;
vt2 = *++pt;
*pt = vt1;
vt1 = temp;
}
};
du51 2006-07-22
  • 打赏
  • 举报
回复
map不是快排.
WindYou 2006-07-22
  • 打赏
  • 举报
回复
struct key_less
{
bool operator()(const VALUE_STRUCT& _Right, const VALUE_STRUCT& _Left)
{
return _Right.nKey < _Left.nKey;
}
};
WindYou 2006-07-22
  • 打赏
  • 举报
回复
#include <vector>
#include <iostream>

using namespace std;

typedef struct _VALUE_STRUCT{
int nKey;
int nVaue;
}VALUE_STRUCT, *LPVALUE_STRUCT;

struct key_greater
{
bool operator()(const VALUE_STRUCT& _Right, const VALUE_STRUCT& _Left)
{
return _Right.nKey > _Left.nKey;
}
};

struct key_less
{
bool operator()(const VALUE_STRUCT& _Right, const VALUE_STRUCT& _Left)
{
return _Right.nKey > _Left.nKey;
}
};

void PrintInfo(VALUE_STRUCT& _Value)
{
cout << _Value.nKey << ':' << _Value.nVaue << '\t';
}

void PrintVec(vector<VALUE_STRUCT>& vec)
{
for(vector<VALUE_STRUCT>::iterator it = vec.begin(); it != vec.end(); ++it)
{
PrintInfo(*it);
}
cout << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
vector<VALUE_STRUCT> vec;
for(int i = 0; i < 10; ++i)
{
VALUE_STRUCT val = {rand() % 100, rand() % 100};
vec.push_back( val );
}
PrintVec(vec);
sort(vec.begin(), vec.end(), key_greater());
PrintVec(vec);
sort(vec.begin(), vec.end(), key_less());
PrintVec(vec);

system( "pause" );
return 0;
}
chenhu_doc 2006-07-22
  • 打赏
  • 举报
回复
while里面换一个,发现了一个有趣的问题。。。。

cout << mapit->first <<'\t';
cout << mapit->second <<'\t';
cout << &mapit->first <<'\t';
cout << &mapit->second <<endl;

result
akey avalue

5 4 0x3d25c8 0x3d25cc
6 1 0x3d2568 0x3d256c
8 2 0x3d2588 0x3d258c
9 3 0x3d25a8 0x3d25ac
请按任意键继续. . . // mapit->first的地址和akey的顺序地址一样。。
// 只是mapit->second移动了

再看看: akey的地址: cout << &akey[i] << '\t';
0x22ff50 0x22ff54 0x22ff58 0x22ff5c
恩, 呵呵还是到mapit->first 移动了....

chenhu_doc 2006-07-22
  • 打赏
  • 举报
回复
由于map是根据键值按默认的'<'来自动排序的,所以只需要将要快速排序的对象设置为键值,那么经过读取: akey和avalue 就可以完成了......

本程序在devcpp中调试通过。。。。

output::

result
akey avalue

5 4
6 1
8 2
9 3
请按任意键继续. . .
chenhu_doc 2006-07-22
  • 打赏
  • 举报
回复
#include <iostream>
#include <map>
using namespace std;

int main()
{
int akey[4]={6,8,9,5};
int avalue[4] ={1,2,3,4};
map<int, int> mp;
for(size_t i = 0; i<4; ++i )
{
mp[akey[i]] = avalue[i];
}

map<int, int>::iterator mapit = mp.begin();

cout << "result" <<endl << " akey "<< " avalue"<<endl<<endl;
while( mapit != mp.end() )
{
cout << mapit->first <<'\t';
cout << mapit->second <<endl;
++mapit;
}
system("PAUSE");
return 0;
}
//参考了jixingzhong的意见,用stl中的map
jixingzhong 2006-07-22
  • 打赏
  • 举报
回复
基本的 map 思想 ...

楼主可以看看 C++ STL 中 map 类 ...
jixingzhong 2006-07-22
  • 打赏
  • 举报
回复
或者,
楼主写一个结构体:
struct map{
int key;
int value;
}a[4];

然后对 数组a 赋值,然后根据 a.key 快排, 就OK了 ...
jixingzhong 2006-07-22
  • 打赏
  • 举报
回复
楼主不要被忽悠了 ...

这里其实和一般的 快速排序 一点差别也没有,
只要你对 akey 排序换元素的时候,
同时 avalue 对应的元素更换位置 就可以了 ...
chenhu_doc 2006-07-22
  • 打赏
  • 举报
回复
我就只提供思路了,
楼主应该是会快速排序的,书上讲的很清楚....

这个过程其实就是:
int akey={6,8,9,5};
int avalue ={1,2,3,4);

对akey做快速排序,排序的过程有akey的下标参与,根据题意可以知道:
在akey做相应移动的同时,移动avalue..( 用下标值 i 作为纽带。),而不管中间有什么样的划分过程。
程序中出现akey的操作,相应的添上对avalue的操作就行~
lyskyly 2006-07-22
  • 打赏
  • 举报
回复
不久前写过一个相似的
#include<iostream>
#include<map>

using namespace std;

int main()
{
int a[4]={2,3,0,8};
multimap<int,int> myMap;

for(int i = 0;i< 4;i++)
{
myMap.insert(make_pair(a[i],i));
}

multimap<int,int>::iterator iter;
multimap<int,int>::iterator end = myMap.end();
for(iter = myMap.begin();iter != end;++iter)
{
cout<<iter->second<<" ";
}
}
http://community.csdn.net/Expert/topic/4890/4890740.xml?temp=.9027368
这篇帖子的楼主提供了一种不错的方法,可以看一看
我啃 2006-07-22
  • 打赏
  • 举报
回复
我把我以前写的一个改了改不知道是否是这个要求
int * quick_sort(int *s1,int *s2,long l,long h)
{
long i = l,j = h;
int t = s1[l],x = s2[l];
while(i<j)
{
while(i<j&&t<=s1[j])
--j;
if(i<j)
{
s1[i] = s1[j];
s2[i] = s2[j];
++i;
}
while(i<j&&t>s1[i])
++i;
if(i<j)
{
s1[j] = s1[i];
s2[j] = s2[i];
--j;
}
}
s1[i] = t;
s2[i] = x;
if(l<i)
quick_sort(s1,s2,l,i-1);
if(i<h)
quick_sort(s1,s2,j+1,h);
return s2;
}
int main()
{
int akey={6,8,9,5};
int avalue ={1,2,3,4);
quick_sort(akey,avalue,0,4);
for (i=0;i<=3;i++)
cout<<"akey["<<i<<"]="<<akey[i]<<" avalue["<<i<<"]="<<avalue[i]<<endl;
return 0;
}
输出:
akey[0]=5 avalue[0]=4
akey[1]=6 avalue[1]=1
akey[2]=8 avalue[2]=2
akey[3]=9 avalue[3]=3

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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