快速排序 的 算法请教??

nocrack 2004-05-08 02:13:03
vecter <veector<int> > v0 =

{
1,2,6 ,4 ,7 ---v1
3, 3, 3, 3, 3 ---v2
9, 1, 4, 6, 7 ---v3
9 ,8, 7, 6, 5 ---v4
2, 4 ,6 ,2 ,6 ---v5
}
v1,v2,v3,v4,v5组成了v0

我要根据 v0[N][2]进行 "快速排序" 。。即比较2,3,1,8,4 的顺序确定v1,v2,v3,v4,v5在v0中的排列顺序。
最后顺序为

9, 1, 4, 6, 7 ---v3
1,2,6 ,4 ,7 ---v1
3, 3, 3, 3, 3 ---v2
2, 4 ,6 ,2 ,6 ---v5
9 ,8, 7, 6, 5 ---v4


请大家帮忙
...全文
65 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
nocrack 2004-05-10
  • 打赏
  • 举报
回复
hehe,谢谢大家~~~

我是这样做的,虽然苯了点,但总比冒泡快得多..

//其实我做的是报表的分组排序 ,只不过数据集是自己定义的
//m_pListData->m_vctList[i][j] 是我自定义的数据集 类型为: vecter<vector<CString> >
//nCul 相当于"列". 通过对QuickSort的循环,达到多重排序的目的..
//算法是抄CSDN上的,呵呵,自己写,肯定要写错了..(本人转VC不太久)
//基本上没有问题了..结帐哦

void QuickSort(long low ,long high ,int nCul )
{
CString pivot;
CString strLow;
CString strHigh;
long scanUp, scanDown;
long mid;

// if the range is not at least two elements, return
if (high - low <= 0)
return;
else
// if sublist has two elements, compare them and
// exchange their values if necessary
if (high - low == 1)
{
//if (m_pListData->m_vctList[high][nCul] < m_pListData->m_vctList[low][nCul])
strLow = m_pListData->m_vctList[low][nCul];
strHigh = m_pListData->m_vctList[high][nCul];

if (TRUE == CompareData(strLow ,strHigh ,nCul) )
{
std::swap(m_pListData->m_vctList[low], m_pListData->m_vctList[high]);
}
return;
}

// get the mid index and assign its value to pivot
mid = (low + high)/2;
pivot = m_pListData->m_vctList[mid][nCul];

// exchange the pivot and the low end of the range
// and initialize the indices scanUp and scanDown.
std::swap(m_pListData->m_vctList[mid], m_pListData->m_vctList[low]);
scanUp = low + 1;
scanDown = high;

// manage the indices to locate elements that are in
// the wrong sublist; stop when scanDown < scanUp
do
{
// move up lower sublist; stop when scanUp enters
// upper sublist or identifies an element > pivot
while (scanUp <= scanDown && m_pListData->m_vctList[scanUp][nCul] <= pivot )
scanUp++;

// scan down upper sublist; stop when scanDown locates
// an element <= pivot; we guarantee we stop at A[low]
while (pivot < m_pListData->m_vctList[scanDown][nCul] )
scanDown--;

// if indices are still in their sublists, then they
// identify two elements in wrong sublists. exchange
if (scanUp < scanDown )
{
std::swap(m_pListData->m_vctList[scanUp], m_pListData->m_vctList[scanDown]);
}

}
while (scanUp < scanDown);

// copy pivot to index (scanDown) that partitions sublists
m_pListData->m_vctList[low][nCul] = m_pListData->m_vctList[scanDown][nCul];
m_pListData->m_vctList[scanDown][nCul] = pivot;

// if the lower sublist (low to scanDown-1) has 2 or more
// elements, make the recursive call
if (low < scanDown - 1)
QuickSort(low, scanDown-1,nCul);

// if higher sublist (scanDown+1 to high) has 2 or more
// elements, make the recursive call
if (scanDown + 1 < high)
QuickSort(scanDown+1, high,nCul);

}

whalefish2001 2004-05-09
  • 打赏
  • 举报
回复
其实,说到头,楼主就是要一个快速排序的算法啊

其他的全是附属,没用的东西。
只要介绍一下快速排序的思想,楼主自己会编写吧?

在第2列中,找第一个数作为比较数A,做2个指针,一个指向头(指向第1个数(空挡),X指针),一个指向尾巴(Y指针),
首先,看Y,如果Y 的数大于A,Y指针继续左移,直到找出第一个比A小的数,把Y指向的数放到X指向的位置,同时,当前指针为X,X右移,道理与上面类似,依次反复,最后把A插入到中间的空挡上,
把A左右两边都看做1列,依次反复操作,

说的有些简洁,如果没有接触过,可能有些看不太明白,建议看看书。

至于程序事例,书上也有。
sharkhuang 2004-05-08
  • 打赏
  • 举报
回复
分段递规!重载> or <
Jinhao 2004-05-08
  • 打赏
  • 举报
回复
其实直接用函数更漂亮,唯一的缺点就是 没有方便的信息传递了,例如那个 index 1
zhouqingyuan 2004-05-08
  • 打赏
  • 举报
回复
单就这个题目来说,写一个简单的比较函数也是可以的。
bool MyCompare(const vector<int>& x,const vector<int>& y)
{
return x[1]<y[1];
}

然后sort(v0.begin(), v0.end(), MyCompare);

这样虽然没有上面那样漂亮,可能对初学者更容易理解!
Jinhao 2004-05-08
  • 打赏
  • 举报
回复
严重失败,看了N久才找到为什么和楼主想得到的结果不同

sort(v0.begin(), v0.end(), MYFUNCTOR<vector<int> >(1));
^^^ 下标
Jinhao 2004-05-08
  • 打赏
  • 举报
回复
No Comment

-----------------------
template<typename _T>
class MYFUNCTOR{
public:
explicit MYFUNCTOR(int index):index_(index){}
bool operator()(const _T& x,const _T& y){
return x[index_] < y[index_];
}
private:
int index_;
};

int main()
{
vector<vector<int> > v0;
vector<vector<int> >::iterator Iter1, End1;
vector<int>::iterator Iter2,End2;
int ia1[]={1, 2, 6, 4, 7};
int ia2[]={3, 3, 3, 3, 3};
int ia3[]={9, 1, 4, 6, 7};
int ia4[]={9, 8, 7, 6, 5};
int ia5[]={2, 4, 6, 2 ,6};
v0.push_back(vector<int>(ia1,ia1+5));
v0.push_back(vector<int>(ia2,ia2+5));
v0.push_back(vector<int>(ia3,ia3+5));
v0.push_back(vector<int>(ia4,ia4+5));
v0.push_back(vector<int>(ia5,ia5+5));
sort(v0.begin(), v0.end(), MYFUNCTOR<vector<int> >(2));
End1=v0.end();
for(Iter1=v0.begin(); Iter1!=End1; Iter1++){
End2=(*Iter1).end();
for(Iter2=(*Iter1).begin(); Iter2!=End2; Iter2++){
cout<<*Iter2<<" ";
}
cout<<endl;
}
}

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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