排序

请原谅我,上来就是在大家眼中水的1B的冒泡排序

可是我相信冒泡排序也是大家看到算法--排序这个大家庭的第一朵鲜花了

 冒泡排序

/*冒泡排序法 容易实现,但是要进行所有轮的比较,运行速度较慢*/
#include<iostream>
using namespace std;
void bubble(int*,int);
void main(){
	int array[]={5,4,6,8,9,3,1,2,7};
	const int len=sizeof(array)/sizeof(int);
	for(int i=0;i<len;++i)
		cout<<array[i]<<" ";
	cout<<endl;
	bubble(array,len);
}
void bubble(int*a,int size){//数组名会自动转化为指向第一个元素的指针,虽然不能直接传递数组,但函数的形参可以写成数组的形式(int*,int[],int[10])形参类型都是int*
	int i,temp;
	for(int pass=1;pass<size;++pass){
		for(i=0;i<size-pass;++i)
			if(a[i]>a[i+1]){
				temp=a[i];
				a[i]=a[i+1];
				a[i+1]=temp;
			}
		for(i=0;i<size;++i)
			cout<<a[i]<<" ";
		cout<<endl;
	}
}

快速排序

快速排序可谓是排序大家族中的姣姣者

#include<iostream>
#include<vector>
using namespace std;
int pivotIndex(vector<int>&v, int first, int last)
{
	// index for the midpoint of [first,last) and the
	// indices that scan the index range in tandem
	int mid, scanUp, scanDown;
	// pivot value and object used for exchanges
	int pivot, temp;

	if (first == last)
		return last;
	else if (first == last-1)
		return first;
	else
	{
		mid = (last + first)/2;
		pivot = v[mid];

		// exchange the pivot and the low end of the range
		// and initialize the indices scanUp and scanDown.
		v[mid] = v[first];
		v[first] = pivot;

		scanUp = first + 1;
		scanDown = last - 1;

		// manage the indices to locate elements that are in
		// the wrong sublist; stop when scanDown <= scanUp
		for(;;)
		{
			// move up lower sublist; stop when scanUp enters
			// upper sublist or identifies an element >= pivot
			while (scanUp <= scanDown && v[scanUp] < pivot)
				scanUp++;

			// scan down upper sublist; stop when scanDown locates
			// an element <= pivot; we guarantee we stop at arr[first]
			while (pivot < v[scanDown])
				scanDown--;

			// if indices are not in their sublists, partition complete
			if (scanUp >= scanDown)
				break;

			// indices are still in their sublists and identify
			// two elements in wrong sublists. exchange
			temp = v[scanUp];
			v[scanUp] = v[scanDown];
			v[scanDown] = temp;

			scanUp++;
			scanDown--;
		}

		// copy pivot to index (scanDown) that partitions sublists
		// and return scanDown
		v[first] = v[scanDown];
		v[scanDown] = pivot;
		return scanDown;
	}
}

void quicksort(vector<int>& v, int first, int last)
{
   // index of the pivot
   int pivotLoc;
	// temp used for an exchange when [first,last) has
	// two elements
	int temp;

   // if the range is not at least two elements, return
   if (last - first <= 1)
		return;

	// if sublist has two elements, compare v[first] and
	// v[last-1] and exchange if necessary
   else if (last - first == 2)
	{
		if (v[last-1] < v[first])
		{
			temp = v[last-1];
			v[last-1] = v[first];
			v[first] = temp;
		}
		return;
	}
   else
	{
		pivotLoc = pivotIndex(v, first, last);

		// make the recursive call
		quicksort(v, first, pivotLoc);

		// make the recursive call
		quicksort(v, pivotLoc +1, last);
	}
}
int main()
{
	vector<int>ivec;
	int array[]={2,4,5,6,78,88,34,32,43,24,23,43,24,23,4,23,484};
	int length=sizeof(array)/sizeof(int);
	for(int i =0 ;i<length;++i)
		ivec.push_back(array[i]);
	quicksort(ivec,0,length);
	for(i =0 ;i<length;++i)
		cout<<ivec[i]<<" ";
}

基数排序

想象第一次知道的不基于比较的排序,我记得我当时就直接“震惊了”大笑偷笑

#include<iostream>
#include<queue>
#include<vector>
#include "time.h";
using namespace std;
void distribute(const vector<int>& v, queue<int> digitQueue[],
                int power)
{
	int i;

	// loop through the vector, inserting each element into
	// the queue (v[i] / power) % 10
	for (i = 0; i < v.size(); i++)
		digitQueue[(v[i] / power) % 10].push(v[i]);
}

// support function for radixSort()
// gather elements from the queues and copy back to the vector
void collect(queue<int> digitQueue[], vector<int>& v)
{
	int i = 0, digit;

	// scan the vector of queues using indices 0, 1, 2, etc.
	for (digit = 0; digit < 10; digit++)
		// collect items until queue empty and copy items back
		// to the vector
		while (!digitQueue[digit].empty())
		{
			v[i] = digitQueue[digit].front();
			digitQueue[digit].pop();
			i++;
		}
}

void radixSort(vector<int>& v, int d)
{
	int i;
	int power = 1;
	queue<int> digitQueue[10];

	for (i=0;i < d;i++)
	{
		distribute(v, digitQueue, power);
		collect(digitQueue, v);
		power *= 10;
	}
}
void main(){

	vector<int>ivec;
	int i;
	srand(time(NULL));
	for(i=0;i<20;++i)
	{
		ivec.push_back(rand()%20);
	}
	radixSort(ivec,2);

	for(i=0; i<ivec.size();++i){
		cout<<ivec[i]<<" ";
		if((i+1)%5==0)
			cout<<endl;
	}
	cout<<endl;

	
}


归并排序


 

#include<iostream>
#include<vector>
using namespace std;
template <typename T>
void merge(vector<T>& v, int first, int mid, int last)
{
	// temporary vector to merge the sorted sublists
	vector<T> tempVector;
	int indexA, indexB, indexV;

	// set indexA to scan sublistA (index range [first,mid)
	// and indexB to scan sublistB (index range [mid, last)
	indexA = first;
	indexB = mid;

	// while both sublists are not exhausted, compare v[indexA] and
	// v[indexB]copy the smaller to vector temp using push_back()
	while (indexA < mid && indexB < last)
		if (v[indexA] < v[indexB])
		{
			tempVector.push_back(v[indexA]);	// copy element to temp
			indexA++;								// increment indexA
		}
		else
		{
			tempVector.push_back(v[indexB]);	// copy element to temp
			indexB++;								// increment indexB
		}

	// copy the tail of the sublist that is not exhausted
	while (indexA < mid)
	{
		tempVector.push_back(v[indexA]);
		indexA++;
	}

	while (indexB < last)
	{
		tempVector.push_back(v[indexB]);
		indexB++;
	}

	// copy vector tempVector using indexV to vector v using indexA
	// which is initially set to first
	indexA = first;

	// copy elements from temporary vector to original list
	for (indexV = 0; indexV < tempVector.size(); indexV++)
	{
		v[indexA] = tempVector [indexV];
		indexA++;
	}
}

// sorts v in the index range [first,last) by merging
// ordered sublists
template <typename T>
void mergeSort(vector<T>& v, int first, int last)
{
	// if the sublist has more than 1 element continue
	if (first + 1 < last)
  {
		// for sublists of size 2 or more, call mergeSort()
		// for the left and right sublists and then
		// merge the sorted sublists using merge()
		int midpt = (last + first) / 2;

		mergeSort(v, first, midpt);
		mergeSort(v, midpt, last);
		merge(v, first, midpt, last);
   }
}

int main()
{
	vector<int> ivec;
	int array[]={1,2,3,4,5,8,9,7,1,5,2,54,544,54,45,4,5,4,454,45,45,44,54,4,34,2,256,34,926};
	int length=sizeof(array)/sizeof(int);
	for(int i=0;i<length;++i)
	{
		ivec.push_back(array[i]);
	}
	mergeSort(ivec,0,length);

	for(i=0;i<length;++i)
	{
		cout<<ivec[i]<<" ";
	}
	return 0;
}


 

鸽巢排序(来自C++版块精华帖)

排序字节串、宽字节串最快的排序算法,计数排序的变种(将计数缓冲区大小固定,少一次遍历开销),速度是STL中std::sort的20多倍,更重要的是实现极其简单!缺点是需要一个size至少等于待排序数组取值范围的缓冲区,不适合int等大范围数据

#include<iostream>
#include"time.h"
using namespace std;
//鸽巢排序
void PigeonholeSort(int *array,int length)
{

	int b[256]={0};
	int i,j,k;
	for(i=0;i<length;i++)
		b[array[i]]++;
	for(i=0,j=0;i<256;i++)
		for(k=0;k<b[i];k++)
			array[j++]=i;
}
void main()
{	
/*	time_t nowtime;
	nowtime=time(NULL);
	cout<<nowtime;*/
	int array[]={2,5,6,4,1,9,8,7};
	int length=sizeof(array)/sizeof(int);
	PigeonholeSort(array,length);

	for(int m=0; m !=length; ++m)
		cout<<array[m]<<" ";
}


 

 先附上好东西一个

冒泡和选择排序该被踢出教材了

这是C++版块关于排序的一个精华帖,很不错的的说

 

今天写不完了,明天继续。

 

我的新浪微博是@酥西黄

我的邮箱是suziewong@163.com

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值