快速排序,c++ 模板技术表述,完全兼容标准库!!!

快速排序,c++ 模板技术表述,完全兼容标准库!!!

代码说明了一切,贴代码!!!(封装于名字空间mystd中)

#pragma once 
#include<iterator> // std::iterator_traits
#include<functional> // std::less

#define MYSTD_BEGIN namespace mystd {
#define MYSTD_END }

MYSTD_BEGIN  //名字空间mystd

template<class InputIterator> //输入迭代器
void swap(InputIterator first,InputIterator second)
{
	typedef typename std::iterator_traits<InputIterator>::value_type value_type;
	value_type temp_val = *first;
	*first = *second;
	*second = temp_val;
}


//随机访问迭代器
template<class random_access_iterator,class CMP>
void quick_sort(random_access_iterator first,random_access_iterator last,CMP compare)
{
	typedef typename std::iterator_traits<random_access_iterator>::value_type value_type;
	int n = last - first;
	if( n == 0 || n == 1) return ;
	if(n == 2)
	{
		if(compare(*first,*(first + 1))) return ;
		mystd::swap(first,first + 1);
		return ;
	}
	random_access_iterator middle = first + (n - 1) / 2;
	if(!compare(*first,*middle)) mystd::swap(first,middle);
	if(!compare(*first,*(last - 1))) mystd::swap(first,last - 1);
	if(!compare(*middle,*(last - 1))) mystd::swap(middle,last - 1);
	mystd::swap(middle,last - 1);
	value_type middle_val = *(last - 1);
	random_access_iterator first_1 = first;
	random_access_iterator last_1 = last;
	first_1++;
	last_1 -= 2;

	while(first_1 < last_1)
	{
		if(!compare(*first_1,middle_val) && !compare(middle_val,*last_1))
		{
			mystd::swap(first_1++,last_1--);
			continue;
		}
		if(compare(*first_1,middle_val))
		{
			++first_1;
			continue;
		}
		if(compare(middle_val,*last_1))
		{
			--last_1;
			continue;
		}
	}
	mystd::swap(first_1,last - 1);
	quick_sort(first,first_1,compare); // 递归调用
	quick_sort(first_1 + 1,last,compare); // 递归调用
}

template<class random_access_iterator>
void quick_sort(random_access_iterator first,random_access_iterator last)
{  //默认升序排列
	typedef typename std::iterator_traits<random_access_iterator>::value_type value_type;
	quick_sort(first,last,std::less<value_type>()); //转调
}

MYSTD_END // 名字空间mystd


下面是一段测试程序!!!上面的文件命名为"test.h"


#include<iostream>
#include"test.h"
#include<functional> // std::less,std::greater 
#include<cstdlib>  // rand()
#include<vector>


using std::cout;
using std::endl;

int main()
{
	const int mn = 100;
	std::vector<int> my;
	for(int i = 0; i < mn; ++i)
		my.push_back(rand() % 10000);  //随机数赋值

	cout<<"没有排序的情况"<<endl;
	for(int i = 0; i < mn; ++i)
		cout<<my[i]<<"\t";
	cout<<endl;

	cout<<"升序排列的情况"<<endl;
	mystd::quick_sort(my.begin(),my.end()); // 升序
	for(int i = 0; i < mn; ++i)
		cout<<my[i]<<"\t";
	cout<<endl;

	cout<<"降序排列的情况"<<endl;
	mystd::quick_sort(my.begin(),my.end(),std::greater<int>()); //降序
	for(int i = 0; i < mn; ++i)
		cout<<my[i]<<"\t";
	cout<<endl;

	system("pause");
	return 0;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值