排序算法实现

//version 1.03

#include<iostream>
#include<cstring>
#include<list>
using namespace std;

void swap(int &a, int &b)
{
	int temp = a;
	a = b;
	b =temp;
}

template <typename T>
T myMax(T a, T b)
{
	return a >= b ? a : b;
}

///
// bubble sort
void BubbleSort(int a[], int n)
{
	cout << "BubbleSort" << endl;
	for(int i =0; i < n; ++i)
	{
		for(int j = n-1; j > i; --j)
		{
			if(a[j] < a[j-1])
				swap(a[j], a[j-1]);
		}
	}
	for(int i=0; i<n; ++i)
		cout << a[i] << " ";
	cout << endl;
}

///
//     insert sort
void InsertSort(int a[], int n)
{
	cout << "InsertSort" << endl;
	for(int i=0; i<n-1; ++i)
	{
		for(int j=i+1; j>0; --j)
		{
			if(a[j] < a[j-1])
				swap(a[j], a[j-1]);
		}
	}

	for(int i=0; i<n; ++i)
		cout << a[i] << " ";
	cout << endl;
}


///
// quick sort
void quickSort(int a[], int n)
{
	//cout << "quickSort" << endl;
	if(n <= 1) return;
	int first = 0;
	int last  = n - 1;
	int key = a[0];

	while(first < last)
	{
		while(first < last && a[last] >= key) --last;
		if(first < last)
		a[first] = a[last];

		while(first < last && a[first] < key) ++first;
		if(first < last)
		a[last] = a[first];
	}
	a[first] = key;
	quickSort(a, first);
	quickSort(a+first+1, n-first-1);

}

//
//   select sort
void selectSort(int a[], int n)
{
	cout << "selectSort" << endl;
	int minIdx = 0;
	for(int i = 0; i < n-1 ; ++i)
	{
		minIdx = i;
		for(int j = i+1; j < n; ++j)
			if(a[j] < a[minIdx])
				minIdx = j;
		if(minIdx != i)
			swap(a[i], a[minIdx]);
	}
}

///
// merge sort
void mergeArray(int a[], int first, int mid, int last, int temp[])
{
	int m = mid, n = last;
	int i = first, j = mid + 1;
	int k = first;
	while(i <= m && j <= n)
	{
		if(a[j] < a[i])
		{
			temp[k++] = a[j++];
		}
		else
		{
			temp[k++] = a[i++];
		}
	}

	while(i <= m)
	{
		temp[k++] = a[i++];
	}
	while(j <= n)
	{
		temp[k++] = a[j++];
	}

	while(first <= last)
	{
		a[first] = temp[first];
		first++;
	}
}

void mergeSortImp(int a[], int start , int end, int *p)
{
	if(start < end)
	{
	    int mid = (start + end) / 2;
		mergeSortImp(a, start, mid, p);
		mergeSortImp(a, mid+1, end, p);
		mergeArray(a, start, mid, end, p);
	}
}

void mergeSort(int a[], int n)
{
	cout << "mergeSort" << endl;
	int *p = new int[n];
	int start = 0;
	int end   = n - 1;
	mergeSortImp(a, start, end , p);
    memcpy(a, p, sizeof(a[0]) * n);
	delete[] p;

}

///
// shell sort
void shellSort(int a[], int n)
{
	cout << "shellSort" << endl;
	int gap , i, j;
	for(gap = n/2; gap >= 1; gap /= 2)
	{
	    for(i=gap; i<n; ++i)
		    for(j = i-gap; j>=0 && a[j] >a[j+gap]; j-=gap)
			    swap(a[j], a[j+gap]);
	}
}


//heap sort
//http://blog.csdn.net/shuilan0066/article/details/8659235
template <class T>
void AdjustHeapNode(T a[], int i, int n)
{
	if(n==1 || i>(n-2)/2) return;
	int iLeft  = 2 * i + 1;
	int iRight = 2 * i + 2;

	if(iRight <= n-1)
	{
		if(a[i] > a[iLeft] && a[i] > a[iRight]) return;
		if(a[iLeft] >= a[iRight])
		{
			swap(a[iLeft], a[i]);
			AdjustHeapNode(a, iLeft, n);
		}
		else
		{
			swap(a[iRight], a[i]);
			AdjustHeapNode(a, iRight, n);
		}
	}
	else
	{
		if(a[iLeft] < a[i]) return;
		swap(a[iLeft], a[i]);
		AdjustHeapNode(a, iLeft, n);
	}
}

template <typename T>
void CreateHeap(T a[], int n)
{
	int iFirst = (n -1) / 2;
	for(; iFirst >= 0; --iFirst)
	{
		AdjustHeapNode(a, iFirst, n);
	}
}

template <typename T>
void HeapSort(T a[], int n)
{
	CreateHeap(a, n);
	for(int i = 1; i <= n-1; ++i)
	{
		swap(a[0], a[n-i]);
		AdjustHeapNode(a, 0, n-i);
	}
}
//end

///
//bucket sort
struct bucket
{
	int *node;
	int count;
	bucket()
	{
		node  = NULL;
		count = 0;
	}
};
int GetBucketIndex(int number)
{
	return number / 10;
}
void BucketSort(int a[], int n)
{
	cout << "BucketSort !!!" << endl;
	int min = 10000, max = -100000;
	for(int i = 0; i < n; ++i)
	{
		if(a[i] > max) max = a[i];
		if(a[i] < min) min = a[i];
	}

	int bucket_count = (max - min) / 10 + 1;
	bucket *pBucket[bucket_count];
	for(int i = 0; i < bucket_count; ++i)
	{
		pBucket[i] = new bucket;
		pBucket[i]->node = new int[10];
	}

	for(int i = 0; i < n; ++i)
	{
		int idx = GetBucketIndex(a[i] - min);
		int &insert_idx = pBucket[idx]->count;
		pBucket[idx]->node[insert_idx++] = a[i];

	}

	int pos = 0;
	for(int i = 0; i < bucket_count; ++i)
	{
		if(pBucket[i]->count > 0)
			quickSort(pBucket[i]->node, pBucket[i]->count);
		for(int j = 0; j < pBucket[i]->count; j++)
			a[pos++] = pBucket[i]->node[j];
	}

	for(int i = 0; i < bucket_count; ++i)
	{
		delete[] pBucket[i]->node;
		delete   pBucket[i];
	}

}
//end bucket sort

//cocktail_sort
void CockTailSort(int a[], int n)
{
	cout << "CockTailSort!!!" << endl;
	int start = 0, end = n - 1;
	while(start < end)
	{
		for(int i = start ; i < end; ++i)
		{
			if(a[i] > a[i+1])
				swap(a[i], a[i+1]);
		}
		--end;

		for(int i = end; i > start; --i)
		{
			if(a[i] < a[i-1])
				swap(a[i], a[i-1]);
		}
		++start;
	}
}
//end cocktail sort

//count sort
void CountSort(int a[], int n)
{
	cout << "CountSort!!!" << endl;
	int max = -10000;
	int temp[n];
	for(int i = 0; i < n; ++i)
	{
		if(a[i] > max) max = a[i];
	}
	int *b = new int[max + 1];
	for(int i = 0; i < max + 1; ++i)
		b[i] = 0;

	for(int i = 0; i < n; ++i)
	{
		b[a[i]]++;
	}
	for(int i = 1; i < n; ++i)
	{
		b[i] += b[i-1];
	}

	for(int i = 0 ; i < n; ++i)
	{
		int pos   = b[a[i]] - 1;
		temp[pos] = a[i];
		b[a[i]]++;
	}

	memcpy(a, temp, sizeof(*a) * n);
	delete[] b;

}
//end count sort

///
//radix sort
int GetNumByPos(int in, int pos)
{
	int temp = 1;
	for(int i = 0; i < pos; ++i)
	{
		temp *= 10;
	}
	return in / temp % 10;
}
#define keyNum 10
void RadixSort(int a[], int n, int d)
{
	int *pArr[10];
	for(int i = 0; i < 10; ++i)
	{
		pArr[i] = new int[10];
		pArr[i][0] = 0;
	}

	for(int i = 0; i < d; ++i)
	{
		for(int j = 0; j < n; ++j)
		{
			int idx = GetNumByPos(a[j], i);
			int pos = pArr[idx][0] + 1;
			pArr[idx][pos] = a[j];
			++pArr[idx][0];
		}
		int pos_t = 0;
		for(int k = 0; k < 10; ++k)
		{
			for(int m = 0; m < pArr[k][0]; ++m)
			{
				a[pos_t++] = pArr[k][m+1];
			}
			pArr[k][0] = 0;
		}
	}

	for(int i = 0; i < 10; ++i)
	{
		delete[] pArr[i];
	}
}
//end radix sort

//
//   gnome sort
void GnomeSort(int a[], int n)
{
	cout << "Gnome Sort" << endl;
	int i = 0;
	while(i < n)
	{
		if(i == 0 || a[i] >= a[i-1])
			++i;
		else
		{
			swap(a[i], a[i-1]);
			--i;
		}
	}
}
//end gonme sort

///
// odd_even sort
void OddEvenSort(int a[], int n)
{
	cout << "Odd_Even Sort!" << endl;
	bool sorted = false;
	while(!sorted)
	{
		sorted = true;
		for(int i = 1; i < n-1; i += 2)
		{
			if(a[i+1] < a[i])
			{
				swap(a[i+1], a[i]);
				sorted = false;
			}
		}

		for(int i = 0; i < n-1; i += 2)
		{
			if(a[i+1] < a[i])
			{
				swap(a[i+1], a[i]);
				sorted = false;
			}
		}
	}
}
// end odd_even sort

/
//  comb sort
void CombSort(int a[], int n)
{
	cout << "Comb Sort!" << endl;
	double temp = 1.3;
	int step    = static_cast<int>(n / temp);
	while(step > 0)
	{
		for(int i = 0; i + step < n; ++i)
		{
			if(a[i] > a[i + step])
				swap(a[i], a[i + step]);
		}
		step = static_cast<int>(step / temp);
	}
}
//  end comb sort

//
// strand sort
void MergeList(list<int> &src, list<int> &dest)
{
	list<int>::iterator iter1 = src.begin();
	list<int>::iterator iter2 = dest.begin();
	list<int> temp;
	while(iter1 != src.end() && iter2 != dest.end())
	{
		if(*iter1 > *iter2)
		{
			temp.push_back(*iter2);
			++iter2;
		}
		else
		{
			temp.push_back(*iter1);
			++iter1;
		}
	}

	while(iter1 != src.end())
	{
		temp.push_back(*iter1);
		++iter1;
	}
	while(iter2 != dest.end())
	{
		temp.push_back(*iter2);
		++iter2;
	}
	src.clear();
	dest.clear();
	for(iter2 = temp.begin(); iter2 != temp.end(); ++iter2)
		dest.push_back(*iter2);
	temp.clear();
}
void StrandSort(int a[], int n)
{
	list<int> data(a, a + n);
	list<int> result;
	list<int> temp;
	list<int>::iterator iter;
	list<int>::iterator iter2;
	while(!data.empty())
	{
		int key = data.front();
		temp.push_back(key);
		data.pop_front();
		for(iter = data.begin(); iter != data.end();)
		{
			if(*iter > key)
			{
				temp.push_back(*iter);
				key = *iter;
				iter2 = iter;
				++iter;
				data.erase(iter2);
			}
			else
			{
				++iter;
			}
		}
		MergeList(temp, result);
	}
	int i = 0;
	for(iter = result.begin();iter != result.end(); ++iter,++i)
		a[i] = *iter;
}
//

int main()
{
	//int a[] ={1, 6, 5, 8, 0, 2, 4, 3, 7, 9 };
	//int a[] ={2,1};
	int a[] ={78, 17, 39, 26, 72, 94, 21, 12, 23, 91};
	//BubbleSort(a, 10);
	//InsertSort(a, 10);
	//quickSort(a, 10);
	//selectSort(a, 10);
	//mergeSort(a, 10);
	//shellSort(a, 10);
	//HeapSort(a, 10);
    //BucketSort(a, 10);
	//CockTailSort(a, 10);
	//CountSort(a, 10);
	//RadixSort(a, 10, 2);
	//GnomeSort(a, 10);
	//OddEvenSort(a, 10);
	//CombSort(a, 10);
	StrandSort(a, 10);
	for(int i=0; i<10; ++i)
		cout << a[i] << " ";
	cout << endl;
	return 0;
}


好久没有写博客了,在网易云笔记上写了不少,但是比较凌乱,也懒得整理。前一段时间有空,实现了一部分排序算法,贴出来吧,欢迎大家批评指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值