数据结构基础代码实现(C++版)(二)

插入排序

/*
 * author:起风了_Zoe
 * date:2020.03.31
 */

// 插入排序:选择一个数出来与已排好序的进行比较
// 从高到低比较,如果比前一个小,把前一个往后移一位
// 直到满足条件,插入a[in]=temp

#include <iostream>
using namespace std;

template <typename T>
void InsertionSort(T *a, int n);


int main()
{
	double a[] = {2.3, 4, 6.6, 8, 0, 1, 3, 5, 7, 9};
	InsertionSort(a,10);
	for (int i = 0; i < 10; ++i)
	{
		/* code */
		cout << a[i] << endl;
	} 
	return 0;
}

template <typename T>
void InsertionSort(T *a, int n)
{
	int in, out;
	for (out = 1;out < n; out++)
	{
		T temp = a[out]; // 待排序的数据
		in = out;
		while(in > 0 && temp <= a[in-1])
		{
			a[in] = a[in-1];
			in--;
		}
		a[in] = temp;
	}
}

快速排序

/*
* author:起风了_Zoe
* date:2020.03.31
*/

// 快速排序: 选择一个a[left]为标准,从左边找a[i] > a[left],
// 从右边找a[j] < a[left],交换a[i],a[j],再半分递归。

#include <iostream>
using namespace std;

template <typename T>
void QuickSort(T *a, const int left, const int right)
{
	if (left < right)
	{
		int i = left, j = right + 1; // 加1是重点
		int pivot = a[left];

		do
		{
			/* code */
			do i++; while(a[i] < pivot);
			do j--; while(a[j] > pivot);
			if(i < j) swap(a[i], a[j]);
		} while (i < j);
		swap(a[left], a[j]); // 在a[i] < a[j] && i+1==j时,因为i < j,
							 // 所以,do{}之后,j == i-1,所以交换a[left],a[j]

		QuickSort(a, left, j-1);
		QuickSort(a, j+1, right);
	}
}

int main(int argc, char const *argv[])
{
	double a[] = {2, 4, 6, 8, 0, 1, 3, 5, 7, 9, 1e10}; // 1e10是加上去改进算法的
	QuickSort(a, 0, 9);
	for (int i = 0; i < 10; ++i)
	{
		/* code */
		cout << a[i] <<" ";
	}
	return 0;
}

归并排序

/*
* author:起风了_Zoe
* date:2020.03.31
*/

// 归并排序:先分成单个的数据,再两个两个排序,再归并成四个排序,
// 再归并成八个排序.

#include <iostream>
#include <algorithm>
using namespace std;

template <typename T>
void Merge(T *initList, T *mergeList, const int l, const int m, const int n)
{ // 初始数组,归并数组,下标,第一个数组最后一个,数字数量
	int i1, i2, iResult;
	for (i1 = l, i2 = m+1, iResult = l; i1 <= m && i2 <= n; iResult++){
		if (initList[i1] <= initList[i2]){
			mergeList[iResult] = initList[i1];
			i1++;
		}
		else{
			mergeList[iResult] = initList[i2];
			i2++;
		}
	}
	copy(initList+i1, initList+m+1, mergeList+iResult);
	copy(initList+i2, initList+n+1, mergeList+iResult);
}

template <typename T>
void MergePass(T *initList, T *resultList, const int n, const int s)
{ // 初始数组,放结果的数组,元素个数,第s遍归并及数组长度
	int i;
	for (i = 1; i <= n-2*s+1; i+=2*s){
		Merge(initList, resultList, i, i+s-1, i+2*s-1);
	}
	if ((i+s-1) < n){
		Merge(initList, resultList, i, i+s-1, n);
	}else{
		copy(initList+i, initList+n+1, resultList+i);
	}
}

template <typename T>
void MergeSort(T *a, const int n)
{
	T *tempList = new int[n+1]; // tempList[0]不用
	for (int l = 1; l < n; l*=2){
		/* code */
		MergePass(a, tempList, n, l);
		l *= 2;
		MergePass(tempList, a, n, l);
	}
	delete[] tempList;
}

int main()
{
	int a[] = {0, 23, 47, 81, 95, 7, 14, 39, 55, 62, 74}; // a[0]不用
	int b[11] = {0}; // 这个数组用来保存数据
	Merge(a, b, 1, 4, 10);
	for (int i = 1; i < 11; ++i){
		cout << b[i] <<" ";
		/* code */
	}
	cout << endl;

	int m[] = {0, 26, 5, 77, 1, 61, 11, 59, 15, 48, 19}; // m[0]不用
	int n[11] = {0};
	MergeSort(m, 10);
	for (int i = 1; i < 11; ++i){
		cout << m[i] << " ";
	}
	cout << endl;

	return 0;
}

猜你喜欢

转载自www.cnblogs.com/Wind-Flies/p/12608093.html
今日推荐