算法之排序-----排序第一篇 快速排序

快速排序

  • 引言

 

 排序是所有算法里比较基本的算法了,并且非常简单。那么我为什么还要自己再写一遍呢?我觉得知识是大家的,只有你掌握了,这个知识才属于你,才能为你所用。所以接下来,我要连续的将所有的算法都做成博文。其目的,一是总结知识,提高自己;二是为大家共享知识,经验。希望大家督促并给予支持。
  • 算法介绍
快速排序是七中排序算法里比较常用的算法.分治法的基本思想是:将原问题分解为若干个规模更小但结构与原问题相似的子问题。递归地解这些子问题,然后将这些子问题的解组合为原问题的解。事实上,快速排序通常明显比其他Ο(n log n) 算法更快。
  1. 平均时间复杂度O(n * log n),最坏时间复杂度O(n*n)
  2. 平均空间复杂度O ( log n ), 最坏空间复杂度O( n )
  3. 如果用于大量数据排序,可能函数栈溢出。
  • 实验数据
单项遍历

                    

双向遍历

                    

调用库中的qsort,我想说差别真大。。。。。现在我怀疑往后还要自己写代码么?????

                   
  • 代码
        单向遍历

#include<iostream>
#include<vector> 
#include <windows.h>
#include<Winbase.h>
#include <ctime>
using namespace std;

// data declare
typedef vector<int> IntVector ;
size_t const size = 10000 ;

// function declare
void Initialize( IntVector * &V );
void QuickSort(IntVector * &V,size_t s,size_t e);
void Swap(IntVector * &V,size_t i,size_t j);
void VectorCout(IntVector * &V);



int main()
{
	IntVector * V = new IntVector;
	DWORD time_s ,time_e;

	Initialize(V);

	//VectorCout(V);
	cout<<"quicksort"<<endl;
	time_s = GetTickCount();
	QuickSort(V,0,V->size());
	time_e = GetTickCount() ; 
	cout<<"给 "<<size<<" 个数据排序 用时 "<<time_e-time_s<<" 毫秒"<<endl;
	//VectorCout(V);
	delete V;

	system("pause");
	return 0;
}

void Initialize(IntVector * &V)
{
	if(V)
	for(size_t i = 0; i < size; i++)
	{
		V->insert(V->begin(),rand() % 1000 );
	}
}

void QuickSort(IntVector * &V,size_t s,size_t e)
{
	if(V && s < e)
	{	
		size_t key = s;
		size_t i, j;
		for(i = s,j = s + 1; j < e; j++)
		{
			if(*(V->begin()+j)<*(V->begin()+key))
			{
				Swap(V,++i,j);		
			}
		}
		Swap(V,i,key);
		QuickSort(V,s,i);
		QuickSort(V,i+1,e);
	}
}

void Swap(IntVector * &V,size_t i,size_t j)
{
	if(i >= 0 && i < V -> size() && j >= 0 && j < V -> size() )
	{
		int d = *(V->begin()+i);
		*(V->begin()+i) = *(V->begin()+j);
		*(V->begin()+j) = d;
	}
}

void VectorCout(IntVector * &V)
{
	cout<<"data follows"<<endl;
	for(size_t i = 0; i < V->size(); i++)
	{
		cout<<*(V->begin()+i)<<" ";
	}
	cout<<endl;
}



      双向遍历

#include<iostream>
#include<vector> 
#include <windows.h>
#include<Winbase.h>
#include <ctime>
using namespace std;

// data declare
typedef vector<int> IntVector ;
size_t const size = 10000 ;

// function declare
void Initialize( IntVector * &V );
void QuickSort(IntVector * &V,size_t s,size_t e);
void Swap(IntVector * &V,size_t i,size_t j);
void SwapInt(size_t &i,size_t &j);
void VectorCout(IntVector * &V);



int main()
{
	IntVector * V = new IntVector;
	DWORD time_s ,time_e;

	Initialize(V);

	//VectorCout(V);
	cout<<"quick sort"<<endl;
	time_s = GetTickCount();
	QuickSort(V,0,V->size()-1);
	time_e = GetTickCount() ; 
	cout<<"给 "<<size<<" 个数据排序 用时 "<< time_e - time_s <<" 毫秒"<<endl;
	//VectorCout(V);
	delete V;

	system("pause");
	return 0;
}

void Initialize(IntVector * &V)
{
	if(V)
		for(size_t i = 0; i < size; i++)
		{
			V->insert(V->begin(),rand() % 1000 );
		}
}

// Bi-directed
void QuickSort(IntVector * &V,size_t s,size_t e)
{
	if(V && 0 <= s && s < e && e < V->size() )
	{	
		size_t key = s;
		size_t i = e ;

		while( i != key )
		{
			if( i > key )
			{
				if( *(V -> begin()+i) < *(V -> begin()+key) )
				{
					Swap(V,i,key);
					SwapInt(i,key);
					i++;
				}
				else
					i--;
			}
			else
			{
				if( *(V -> begin()+i) > *(V -> begin()+key) )
				{
					Swap(V,i,key);
					SwapInt(i,key);
					i--;
				}
				else
					i++;
			}
		}
		QuickSort(V,s,key-1);
		QuickSort(V,key+1,e);
	}
}

void Swap(IntVector * &V,size_t i,size_t j)
{
		if(i >= 0 && i < V -> size() && j >= 0 && j < V -> size() )
		{
			int d = *(V->begin()+i);
			*(V->begin()+i) = *(V->begin()+j);
			*(V->begin()+j) = d;
		}
}

void SwapInt(size_t &i,size_t &j)
{
	size_t d = i ;
	i = j;
	j = d;
}

void VectorCout(IntVector * &V)
{
	cout<<"data follows"<<endl;
	for(size_t i = 0; i < V->size(); i++)
	{
		cout<<*(V->begin()+i)<<" ";
	}
	cout<<endl;
}


库中的qsort调用

#include<iostream>
#include<algorithm>
#include<vector> 
#include <windows.h>
#include<Winbase.h>
#include <ctime>
using namespace std;

// data declare 1000,000
size_t const size = 1000000 ;

// function declare
int compare(const void * a, const void * b);


int main()
{
	DWORD time_s ,time_e;
	int *d = new int[size];
	for(size_t i =0; i<size; i++)
	{
		d[i] = rand() % 1000;
	}

	cout<<"quick sort"<<endl;

	time_s = GetTickCount();
	qsort(d,size,sizeof(int),compare);
	time_e = GetTickCount() ;

	cout<<"给 "<<size<<" 个数据排序 用时 "<< time_e - time_s <<" 毫秒"<<endl;

	system("pause");
	for(size_t i =0; i<size; i++)
	{
		cout<<d[i]<<" ";
	}
	system("pause");
	return 0;
}


int compare(const void * a, const void * b)
{
	return *(int*)a - *(int*)b;
}




 

转载于:https://www.cnblogs.com/riasky/p/3473411.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值