C++实现各种排序以及复杂度,稳定性分析

Posted 唐火

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++实现各种排序以及复杂度,稳定性分析相关的知识,希望对你有一定的参考价值。

代码如下:

#include<iostream>
using namespace std;

void Bubble_Sort(int *a, int n)

	bool flag;
	int tmp = 0;
	for (int i = n - 1; i >= 0; i--)
	
		flag = false;
		for (int j = 0; j < i; j++)
		
			if (a[j] > a[j + 1])
			
				swap(a[j], a[j + 1]);
				flag = true;
			
		
		if (!flag) break;
	



int partition(int *a, int low, int high)

	int point = a[low];
	while (low < high)
	
		while (low < high && a[high] >= point)
		
			high--;
		
		swap(a[low], a[high]);
		while (low < high && a[low] <= point)
		
			low++;
		
		swap(a[low], a[high]);
	
	return low;


void quicksort(int *a, int low, int high)

	if (low < high)
	
		int point = partition(a, low, high);
		quicksort(a, low, point - 1);
		quicksort(a, point + 1, high);
	


void Quick_Sort(int *a, int n)

	quicksort(a, 0, n - 1);





void Insertion_Sort(int *a, int n)

	int tmp;
	int j;
	for (int i = 1; i < n; i++)
	
		tmp = a[i];
		for ( j = i; j > 0 && a[j - 1] > tmp; j--)
		
			a[j] = a[j - 1];
		
		a[j] = tmp;
	



void Shell_Sort(int *a, int n)

	int tmp;
	int j;
	for (int d = n / 2; d > 0; d /= 2)
	
		for (int i = d; i < n; i++)
		
			tmp = a[i];
			for ( j = i; j >= d && a[j - d] > tmp; j -= d)
			
				a[j] = a[j - d];
			
			a[j] = tmp;
		
	



void merge(int *a, int mid, int low, int high)

	int i = low;
	int j = mid + 1;
	int k = 0;
	int *w = new int[high - low + 1];
	while (i <= mid && j <= high)
	
		if (a[i] < a[j]) w[k++] = a[i++];
		else w[k++] = a[j++];
	

	while (i <= mid) w[k++] = a[i++];
	while (j <= high) w[k++] = a[j++];

	for (int i = low, j = 0; i <= high; i++, j++) a[i] = w[j];

	delete[] w;


void mergesort(int *a, int low, int high)

	if (low < high)
	
		int mid = (low + high) >> 1;
		mergesort(a, low, mid);
		mergesort(a, mid + 1, high);
		merge(a, mid, low, high);
	


void Merge_Sort(int *a, int n)

	mergesort(a, 0, n - 1);




void Selection_Sort(int *a, int n)

	int min;
	for (int i = 0; i < n; i++)
	
		min = i;
		for (int j = i + 1; j < n; j++)
		
			if (a[j] < a[min])
				min = j;
		
		if (min != i)
		
			swap(a[min], a[i]);
		
	


void percDown(int *a,int p,int n)

	int parent, child;
	int x = a[p];
	for (parent = p; (parent * 2+1) < n; parent = child)
	
		child = 2 * parent+1;
		if (child != n-1 && a[child] < a[child + 1])
		
			child++;
		
		if (x >= a[child]) break;
		else a[parent] = a[child];
	
	a[parent] = x;




void Heap_Sort(int *a, int n)

	for (int i = n / 2 - 1; i >= 0; i--)
	
		percDown(a, i, n);
	
	for (int i = n - 1; i > 0; i--)
	
		swap(a[0], a[i]);
		percDown(a, 0, i);
	


void printElem(int *a,int n)

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


void updataElem(int *a, int *b,int n)

	for (int i = 0; i < n; i++)
	
		a[i] = b[i];
	





int main()


	int a[] =  213,432,213,64,5,532,412,3,12412,312 ;
	int b[] =  213,432,213,64,5,532,412,3,12412,312 ;
	Bubble_Sort(a, 10);//冒泡排序
	printElem(a,10);
	updataElem(a, b,10);
	Quick_Sort(a, 10);//快速排序
	printElem(a, 10);
	updataElem(a, b, 10);
	Insertion_Sort(a, 10);//插入排序
	printElem(a, 10);
	updataElem(a, b,10);
	Shell_Sort(a, 10);//普通希尔排序
	printElem(a, 10);
	updataElem(a, b, 10);
	Merge_Sort(a, 10);//归并排序
	printElem(a, 10);
	updataElem(a, b,10);
	Heap_Sort(a, 10);//堆排序
	printElem(a, 10);
	updataElem(a, b, 10);
	Selection_Sort(a, 10);//选择排序
	printElem(a, 10);
	return 0;

复杂度以及稳定性分析:

以上是关于C++实现各种排序以及复杂度,稳定性分析的主要内容,如果未能解决你的问题,请参考以下文章

各种排序算法复杂度和稳定性分析

排序算法——快速排序的图解代码实现以及时间复杂度分析

习题之冒泡排序的C++实现

C++面试题(算法基础-排序算法)

排序算法实现

十大排序算法对比和C++实现