C++实现选择排序算法(简单选择排序、堆排序)

排序算法分为五大类,一共是有九种,如下:
插入类:直接插入排序、折半插入排序、希尔排序
交换类:冒泡排序、快速排序
选择类:简单选择排序、堆排序
归并类:二路归并排序
基数类:多关键字排序
九种算法的时间复杂度、空间复杂度和稳定性小结如下:
C++实现选择排序算法(简单选择排序、堆排序)_第1张图片
本文放出选择算法的两种排序算法代码。
八种常用的排序算法代码可以到这里下载。

简单选择排序

void SelectSort(int R[], int n) {
     
	int i, j, k, temp;
	cout << "简单选择排序" << endl;
	for (i = 0; i < n; ++i) {
     
		k = i;
		for (j = i + 1; j < n; ++j)
			if (R[k] > R[j])
				k = j;
		temp = R[i];
		R[i] = R[k];
		R[k] = temp;
		for (int k = 0; k < n; k++)
			cout << R[k] << " ";
		cout << endl;
	}
}

堆排序

void swap(int R[], int x, int y) {
     
	int key;
	key = R[x];
	R[x] = R[y];
	R[y] = key;
}
void Down(int R[], int low, int high) {
     						
	int parent = low;										
	int child = 2 * low + 1;									
	while (child < high) {
     
		if (R[child] < R[child + 1] && child + 1 < high) {
     	
			child++;
		}

		if (R[parent] < R[child]) {
     				
			swap(R, parent, child);					
			parent = child;								
		}

		child = child * 2 + 1;								
	}
}
void BuildHeap(int R[], int n)
{
     
	int i = 0;												
	for (i = n / 2 - 1; i >= 0; i--) {
     							
		Down(R, i, n);								
	}
}
void Heapsort(int R[], int n) {
     
	BuildHeap(R, n);								
	cout << "堆排序" << endl;
	for (int i = 0; i < n; i++) {
     
		cout << R[i] << " ";
	}
	printf("\n");

	for (int i = n - 1; i > 0; i--)
	{
     
		swap(R, 0, i);								
		Down(R, 0, i);								

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

总体

#include 
using namespace std;
void SelectSort(int R[], int n) {
     
	int i, j, k, temp;
	cout << "简单选择排序" << endl;
	for (i = 0; i < n; ++i) {
     
		k = i;
		for (j = i + 1; j < n; ++j)
			if (R[k] > R[j])
				k = j;
		temp = R[i];
		R[i] = R[k];
		R[k] = temp;
		for (int k = 0; k < n; k++)
			cout << R[k] << " ";
		cout << endl;
	}
}
void swap(int R[], int x, int y) {
     
	int key;
	key = R[x];
	R[x] = R[y];
	R[y] = key;
}
void Down(int R[], int low, int high) {
     						
	int parent = low;										
	int child = 2 * low + 1;									
	while (child < high) {
     
		if (R[child] < R[child + 1] && child + 1 < high) {
     	
			child++;
		}

		if (R[parent] < R[child]) {
     				
			swap(R, parent, child);					
			parent = child;								
		}

		child = child * 2 + 1;								
	}
}
void BuildHeap(int R[], int n)
{
     
	int i = 0;												
	for (i = n / 2 - 1; i >= 0; i--) {
     							
		Down(R, i, n);								
	}
}
void Heapsort(int R[], int n) {
     
	BuildHeap(R, n);								
	cout << "堆排序" << endl;
	for (int i = 0; i < n; i++) {
     
		cout << R[i] << " ";
	}
	printf("\n");

	for (int i = n - 1; i > 0; i--)
	{
     
		swap(R, 0, i);								
		Down(R, 0, i);								

		for (int i = 0; i < n; i++) {
     
			cout<<R[i]<<" ";
		}
		cout << endl;
	}
}
int main()
{
     
	int A[8] = {
      49,38,65,97,76,13,27,49 };
	int n = 8;
	int R[8];
	cout << "原始数组:";
	for (int k = 0; k < n; k++) {
     
		R[k] = A[k];
		cout << R[k] << " ";
	}
	cout << endl;
	SelectSort(R, n);//简单选择排序
	cout << endl;
	cout << "原始数组:";
	for (int k = 0; k < n; k++) {
     
		R[k] = A[k];
		cout << R[k] << " ";
	}
	cout << endl;
	Heapsort(R, n);//堆排序

	return 0;
}

主要参考的书籍是《数据结构高分笔记》2021版

你可能感兴趣的:(数据结构,排序算法,堆排序)