# 快速排序 的 算法请教??

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

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

...全文
37 7 打赏 收藏 举报

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

• 打赏
• 举报

sharkhuang 2004-05-08

• 打赏
• 举报

Jinhao 2004-05-08

• 打赏
• 举报

zhouqingyuan 2004-05-08

bool MyCompare(const vector<int>& x,const vector<int>& y)
{
return x[1]<y[1];
}

• 打赏
• 举报

Jinhao 2004-05-08

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;
}
}
• 打赏
• 举报

2.4w+

C/C++ 工具平台和程序库

2004-05-08 02:13