首页 > 代码库 > 排序算法总结---代码+性能

排序算法总结---代码+性能

// data_sort_alg.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "sort_alg.h"
#include <iostream>
#include <vector>

void show(std::vector<int> &a)
{
	std::vector<int>::iterator it=a.begin();

	while(it!=a.end())
	{
		std::cout<<*it<<" ";
		++it;
	}
	std::cout<<std::endl;
	return;
}

int main(void)
{
	int a[11]={1,-4,43,12,0,6,78,32,23,10,8};
	int b[12]={0,1,-4,43,12,0,6,78,32,23,10,8};						
	int c[11]={9,3,45,43,95,92,90,72,32,30,66};					
	int d[11]={9,34,79,23,345,78,120,4578,547,89,500};	
	std::vector<int> src(a,a+11);
	std::vector<int> src1(b,b+12);					//heap sort
	std::vector<int> src2(c,c+11);					//bucket sort
	std::vector<int> src3(d,d+11);					//	radix sort
	
	std::cout<<"The original data order is:"<<std::endl;
	show(src3);
	radix_sort(src3);
	std::cout<<"The sorting result is:"<<std::endl;
	show(src3);

	system("pause");
	return 0;
}
#ifndef SORT_ALG_H
#define SORT_ALG_H
#include <vector>

void bubble_sort(std::vector<int> &a);
void shell_sort(std::vector<int> &a);
void simple_sort(std::vector<int> &a);
void merge_sort(std::vector<int> &s,int b,int e);
void insert_sort(std::vector<int>& a);
void heap_sort(std::vector<int> &a);
void fast_sort(std::vector<int> &a,int start,int end);
void bucket_sort(std::vector<int> &a);
void radix_sort(std::vector<int> &a);

#endif
#include "stdafx.h"
#include <vector>
#include<algorithm>

#define STEP 5

/*==============================
1 name:bubble sort
--------------------------------
time complexity:
worst		average		best
O(n^2)	O(n^2)		O(n)
--------------------------------
space complexity:
O(1)
--------------------------------
stability:
stable
==============================*/

void bubble_sort(std::vector<int> &a)
{
	for(int i=a.size()-1;i>0;i--)
	{
		for(int j=0;j<i;j++)
		{
			if(a[j]>a[j+1])
			{
				std::swap(a[j],a[j+1]);
			}
		}
	}
	return;
}

/*==============================
2 name:shell sort
--------------------------------
time complexity:
worst		average		best
O(ns)	O(nlogn)		O(n)
1<s<2
--------------------------------
space complexity:
O(1)
--------------------------------
stability:
unstable
==============================*/

void shell_sort_assist(std::vector<int> &a,int d)
{
	int i=d;
	while(i<a.size())
	{
		int j=0;
		while(j<i)
		{
			if(a[i]<a[j])
			{
				int temp=a[i];
				int k=i;
				while(k-d>=j)
				{
					a[k]=a[k-d];
					k=k-d;
				}
				a[j]=temp;
				break;
			}	
			j=j+d;
		}
		i=i+d;
	}
	return;
}

void shell_sort(std::vector<int> &a)
{
	int d=STEP;
	while(d!=0)
	{
		shell_sort_assist(a,d);
		d/=2;
	}
	return;
}

/*==============================
3 name:simple sort
--------------------------------
time complexity:
worst		average		best
O(n^2)	O(n^2)		O(n^2)
--------------------------------
space complexity:
O(1)
--------------------------------
stability:
unstable
==============================*/

void simple_sort(std::vector<int> &a)
{
	std::vector<int>::iterator it=a.begin(),it1,saveit;
	for(;it!=a.end();it++)
	{
		int min=*it;
		saveit=it;
		
		it1=it;
		while(it1!=a.end())
		{
			if(min>*it1)
			{
				min=*it1;
				saveit=it1;
			}
			it1++;
		}
		if(saveit!=it)
		{
			std::swap(*it,*saveit);
		}
	}
	return;
}

/*==============================
4 name: merge sort
--------------------------------
time complexity:
worst			average			best
O(nlogn)	O(nlogn)		O(nlogn)
--------------------------------
space complexity:
O(n)
--------------------------------
stability:
stable
==============================*/

void merge(std::vector<int> &s,int b,int m,int e)
{
	int* temp=new int[e-b+1];

	int i=b;
	int j=m+1;
	int k=0;
	while(i<m+1 && j<e+1)
	{
		if(s[i]>s[j])
		{
			temp[k++]=s[j++];
		}
		else
		{
			temp[k++]=s[i++];
		}
	}

	if(i<m+1)
	{
		while(i<m+1)
		{
			temp[k++]=s[i++];
		}
	}
	if(j<e+1)
	{
		while(j<e+1)
		{
			temp[k++]=s[j++];
		}
	}

	for(int m=0;m<e-b+1;m++)
	{
		s[b+m]=temp[m];
	}
	
	delete [] temp;
	return;
}

void merge_sort(std::vector<int> &s,int b,int e)
{	
	int m;
	if(e>b)
	{
		m=(b+e)/2;
		merge_sort(s,b,m);
		merge_sort(s,m+1,e);
		merge(s,b,m,e);
	}
	return;
}

/*==============================
5 name: insert sort
--------------------------------
time complexity:
worst			average		best
O(n^2)		O(n^2)		O(n)
--------------------------------
space complexity:
O(1)
--------------------------------
stability:
stable
==============================*/

void insert_sort(std::vector<int>& a)
{
	std::vector<int>::iterator it,it1,it2;
	int temp;

	it=a.begin();
	while(it!=a.end())
	{
		it1=a.begin();
		while(it1!=it)
		{
			if(*it<*it1)
			{
				temp=*it;
				it2=it;
				while(it2!=it1)
				{
					*it2=*(it2-1);
					it2--;
				}
				*it1=temp;
				break;
			}
			it1++;
		}
		it++;
	}
}

/*==============================
6 name: heap sort
--------------------------------
time complexity:
worst				average			best
O(nlogn)		O(nlogn)		O(nlogn)
--------------------------------
space complexity:
O(1)
--------------------------------
stability:
unstable
==============================*/
void heap_adjust(std::vector<int> &a,int i,int end)
{
	int lchild=2*i;
	int rchild=2*i+1;
	int max=i;

	if(rchild<end+1)
	{
		if(a[lchild]>a[max])
		{
			max=lchild;
		}
		if(a[rchild]>a[max])
		{
			max=rchild;
		}
		if(max!=i)
		{
			std::swap(a[max],a[i]);
			heap_adjust(a,max,end);
		}
	}
}

void build_heap(std::vector<int> &a)
{
	int size=a.size()-1;
	for(int i=size/2;i>0;i--)
	{
		heap_adjust(a,i,size);
	}
	return;
}

void heap_sort(std::vector<int> &a)
{
	build_heap(a);

	for(int i=a.size()-1;i>0;i--)
	{
		std::swap(a[1],a[i]);
		heap_adjust(a,1,i-1);
	}
}

/*==============================
7 name: fast sort
--------------------------------
time complexity:
worst				average			best
O(n^2)			O(nlogn)		O(nlogn)
--------------------------------
space complexity:
O(logn)
--------------------------------
stability:
unstable
==============================*/

void fast_sort(std::vector<int> &a,int start,int end)
{
	int med=a[start];

	int i=start;
	int j=end;
	while(j>i)
	{
		while(a[j]>=med && j>i)
		{
			j--;
		}
		a[i]=a[j];
		while(a[i]<=med && j>i)
		{
			i++;
		}
		a[j]=a[i];
	}
	a[i]=med;

	if(start<i)
	{
		fast_sort(a,start,i);
	}
	if(i+1<end)
	{
		fast_sort(a,i+1,end);
	}
	return;
}

/*==============================
8 name:bucket sort
--------------------------------
time complexity:
average		
O(n+nlogn-nlogm)
--------------------------------
space complexity:
O(n)
--------------------------------
stability:
unstable
==============================*/

//suppose: 0<data[i]<100 
//we should design the project function based on the data distribution
void bucket_sort(std::vector<int> &a)
{
	std::vector<std::vector<int>> bucket;
	bucket.resize(10);

	std::vector<int>::iterator it=a.begin();
	while(it!=a.end())
	{
		int idx=*it/10;
		bucket[idx].push_back(*it);
		it++;
	}

	std::vector<std::vector<int>>::iterator it1=bucket.begin();
	while(it1!=bucket.end())
	{
		simple_sort(*it1);
		it1++;
	}

	it=a.begin();
	it1=bucket.begin();
	while(it!=a.end() && it1!=bucket.end())
	{
		std::vector<int>::iterator tmp_it=(*it1).begin();
		while(tmp_it!=(*it1).end())
		{
			*it=*tmp_it;
			tmp_it++;
			it++;
		}
		it1++;
	}
}

/*==============================
9 name: radix sort
--------------------------------
time complexity:
average		
O(2dn)
--------------------------------
space complexity:
O(n)
--------------------------------
stability:
stable
==============================*/

void refresh_data(std::vector<int> &a, std::vector<std::vector<int>> &sto)
{
	std::vector<int>::iterator it,it1;
	std::vector<std::vector<int>>::iterator it2;

	it=a.begin();
	it2=sto.begin();
	while(it!=a.end() && it2!=sto.end())
	{
		it1=(*it2).begin();
		while(it1!=(*it2).end())
		{
			*it=*it1;
			it1++;
			it++;
		}
		(*it2).clear();
		it2++;
	}

	return;
}

//suppose:there are no minus number
void radix_sort(std::vector<int> &a)
{
	std::vector<std::vector<int>> sto;
	sto.resize(10);
	int max=0;

	std::vector<int>::iterator it=a.begin();
	while(it!=a.end())
	{
		int idx;

		if(max<*it)
		{
			max=*it;
		}
		idx=(*it)%10;
		sto[idx].push_back(*it);
		it++;
	}
	refresh_data(a,sto);

	int d=0;
	int temp=max;
	while(1)
	{
		if(temp!=0)
		{
			d++;
		}
		else
		{
			break;
		}
		temp/=10;
	}

	for(int i=1;i<=d;i++)
	{
		int div=pow(10.0,i);
		it=a.begin();
		while(it!=a.end())
		{
			int idx;
			idx=(*it/div)%10;
			sto[idx].push_back(*it);
			it++;
		}
		refresh_data(a,sto);
	}

	return;
}