求实现快排的C/C++代码

密斯刘 2009-08-16 12:20:37
如题 定义我看了好久 就是不知道如何实现
...全文
921 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoguizi1987 2009-10-29
  • 打赏
  • 举报
回复
非常谢谢
superbtl 2009-08-16
  • 打赏
  • 举报
回复
为什么自己不GOOGLE选个自己喜欢的风格的?
mstlq 2009-08-16
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090806/13/B810F4B6-7612-49EC-ADB1-D1CBBDB37614.html

1楼就是……
太乙 2009-08-16
  • 打赏
  • 举报
回复
http://download.csdn.net/source/403514

里面有源码、ppt讲解等……
pwwmwx 2009-08-16
  • 打赏
  • 举报
回复
//快速排序思想:
//1.取中间数 ,头尾指针元素与其相比较
//2.如果*head < middle  head指针继续往后移动 , *head >= middle 时停止
// 如果*tail > middle  tail指针继续往前移动 , *tail <= middle 时停止
//3.交换头尾指针指向的元素 , 注意:指针所指位置不变
//4.头尾指针继续移动比较, 直到head > = tail

#include <iostream>
using namespace std;
//分段函数,将待排序序列分成两段,前一段的各个数比中间数小,后一段比中间数大

int *split (int *head , int *tail)
{
int middle = *(head + (tail - head) / 2);
while (1)
{
while (*head < middle)
head++;
tail--;
while (*tail > middle)
tail--;
if (head >= tail)
return head;
swap (*head , *tail);
head++;
}
}
//快速排序 , 递归调用
void QuickSort(int * first , int *last)
{
if (last > first+1)
{
int *t=split(first, last); //返回中间值所在的位置
//判断前一段和后一段的长度, 为了提高效率长度小的先操作 ,
if (last -t < t - first)
{
QuickSort(first , t);
QuickSort(t, last);
}
else
{
QuickSort(first , t);
QuickSort(t, last);
}
}
}
thy38 2009-08-16
  • 打赏
  • 举报
回复
我是个STL的初学者,自己写了个,你看看行不?
你只要看quick_sort就可以了,中间一大段是我试验用字符串形式输入数组的test,写着玩的。
#include <iostream>
#include <sstream>
#include <vector>

#define _TEST

using namespace std;

typedef int TEST_TYPE;

template <typename T>
struct test_node
{
vector<T> original;
vector<T> sorted;
};

template <typename T>
ostream& operator<< (ostream& strm, const vector<T> &v)
{
typename vector<T>::const_iterator it;
for(it = v.begin(); it != v.end(); ++it) strm << *it << ' ';
strm << endl;
return strm;
}

template<typename BI>
void quick_sort(BI head, BI tail)
{
BI start, end;
typedef typename iterator_traits<BI>::value_type value_type;
value_type tmp;

if(head!=tail)
{
start=head;
end=tail-1;
tmp=*start;

while(start!=end){
while(start!=end && tmp <= *end) {--end;}
*start=*end;

while(start!=end && *start <= tmp) {++start;}
*end=*start;
}
*start=tmp;
quick_sort(head,start);
quick_sort(start+1,tail);
}
}

void load_node(vector<test_node<TEST_TYPE> > &test_vector, const string &original, const string &sorted)
{
test_node<TEST_TYPE> tn;
test_vector.push_back(tn);

typedef vector<test_node<TEST_TYPE> >::iterator _itervtn;
_itervtn it = test_vector.end()-1;

typedef vector<TEST_TYPE>::value_type value_type;
value_type n;

istringstream ins;
ins.str(original);
while(ins >> n) {(*it).original.push_back(n);}

ins.clear();
ins.str(sorted);
while(ins >> n) {(*it).sorted.push_back(n);}
}

template <typename T>
void load_node(vector<test_node<T> > &test_vector, const T & original, const T & sorted)
{
test_node<T> tn;
test_vector.push_back(tn);

typedef typename vector<test_node<T> >::iterator _itervtn;
_itervtn it = test_vector.end()-1;

(*it).original.push_back(original);
(*it).sorted.push_back(sorted);
}

void test_quick_sort()
{
vector<test_node<TEST_TYPE> > test_vector;
load_node(test_vector, "3 7 2 0 9 -5 4", "-5 0 2 3 4 7 9");
//delibrately alter 5 to 4
load_node(test_vector, "1 2 3 4 5 6 7 8 9", "1 2 3 4 4 6 7 8 9");
load_node(test_vector, "9 8 7 6 5 4 3 2 1", "1 2 3 4 5 6 7 8 9");
load_node(test_vector, "4 1", "1 4");
load_node(test_vector, "3", "3");

int passed=0;
typedef vector<test_node<TEST_TYPE> >::iterator _itervtn;

for(_itervtn it=test_vector.begin(); it != test_vector.end(); ++it)
{
quick_sort<vector<TEST_TYPE>::iterator>((*it).original.begin(),
(*it).original.end());
if((*it).original == (*it).sorted)
++passed;
else {
cout << "* No." << distance(test_vector.begin(),it)
<< " did not pass test.\n";
cout << " ---Result: \t" << (*it).original
<< " ---Expected: \t" << (*it).sorted;
}
}
cout << endl << "* Passed " << passed << '/' << test_vector.size() << endl;
}

int main()
{
#ifdef _TEST
test_quick_sort();
#else
int a[]={4,1};
vector<int> v(a, a+sizeof(a)/sizeof(a[0]));

cout << "Original: \t" << v;
quick_sort(v.begin(), v.end());
cout << "Sorted: \t" << v;
#endif
return 0;
}

64,662

社区成员

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

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