排序

請原諒我,上來就是在大家眼中水的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++版塊關於排序的一個精華帖,很不錯的的說

 

今天寫不完了,明天繼續。

 

我的新浪微博是@酥西黃

我的郵箱是[email protected]

 

 

發佈了23 篇原創文章 · 獲贊 15 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章