Four sorting algorithms: bubble, insert, select, fast

Bubble Sort

basic concept

Repeatedly visit the column of elements to be sorted, compare two adjacent elements in turn, and exchange them if their order (such as from large to small, initial letter from A to Z) is wrong. The work of visiting elements is repeated until no adjacent elements need to be exchanged, that is to say, the element column has been sorted. After one pass of bubble sorting, the largest (smallest) number will be put at the end, so there is no need to compare it with the last number of the first pass of bubble sorting in the second pass.

C++ code

#include<iostream>
using namespace std;
// 声明冒泡排序算法(参数含义:1.数组 2.数组尺寸)
void BubbleSort(int arr[], int n);
// 声明打印算法
void PrintArr(int arr[], int n);

int main()
{
    
    
	// 数组例子
	int arr[] = {
    
     2, 3, 4, 1, 5, 6, 2, 8, 9 };
	// 数组大小
	int n = sizeof(arr) / sizeof(int);
	// 冒泡排序
	BubbleSort(arr, n);
	PrintArr(arr, n);
	return 0;
}
// 冒泡排序实现
void BubbleSort(int arr[], int n)
{
    
    
	int i, j;
	for (i = 0; i < n - 1; i++)
	{
    
    
		for (j = 0; j < n - 1 - i; j++)
		{
    
    
			// 交换
			if (arr[j + 1] < arr[j])
			{
    
    
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}
// 打印算法实现
void PrintArr(int arr[], int n)
{
    
    
	for (int i = 0; i < n; i++)
	{
    
    
		cout << arr[i] << " ";
	}
	cout << endl;
}

Time Complexity and Space Complexity

The time complexity is: O(n^2) and
the space complexity is: O(1). The space complexity is to see if there is an auxiliary storage object, because there is a temporary variable temp, which is used to exchange two elements. In fact, the space complexity can be reduced to O(0), which is to change the exchange code to:

a = a + b;
b = a - b;
a = a - b;

However, there may be a problem of out-of-bounds in this way, so it is better to use temporary variables honestly

insertion sort

basic concept

Build an ascending or descending sequence step by step from left to right. For unsorted data, gradually find a suitable position in the sorted data and insert the
current key value into the previously sorted array to make it also sorted. Arrays, this cycle will make the entire array sorted.
Insertion sorting is efficient when applied to small-scale data or basically ordered data.
But when it comes to large-scale and unordered data, insertion sorting is not efficient and needs to be performed. Improvement, Hill sorting can achieve this effect

C++ code

#include<iostream>
using namespace std;
// 声明插入排序算法(参数:1.数组 2.数组大小)
void InsertSort(int arr[], int n);
// 声明打印方法
void PrintArr(int arr[], int n);

int main()
{
    
    
	// 例子数组
	int arr[] = {
    
     4, 4, 6, 1, 7, 8, 23, 15, 9, 2 };
	// 数组大小
	int n = sizeof(arr) / sizeof(int);
	InsertSort(arr, n);
	PrintArr(arr, n);
	return 0;
}
// 插入排序实现
void InsertSort(int arr[], int n)
{
    
    
	int i, j, key;
	for (i = 0; i < n; i++)
	{
    
    
		// 选择当前为key的值插入i之前排序好的数组中,让其也变为排序好的数组
		key = arr[i];
		j = i - 1;
		while (j >= 0 && arr[j] > key)
		{
    
    
			arr[j + 1] = arr[j];
			j--;
		}
		arr[j + 1] = key;
	}
}
// 打印输出方法实现
void PrintArr(int arr[], int n)
{
    
    
	for (int i = 0; i < n; i++)
	{
    
    
		cout << arr[i] << "  ";
	}
	cout << endl;
}

Time Complexity and Space Complexity

Time complexity: O(n^2)
Space complexity: O(0), exchange has no temporary variable temp

selection sort

basic concept

For the first time, the smallest (or largest) element is selected from the data elements to be sorted, and stored at the beginning of the sequence, and then the smallest (largest) element is found from the remaining unsorted elements, and then placed in the The end of the sorted sequence. And so on, until the number of all data elements to be sorted is zero.

C++ code

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

void SelectSort(int arr[], int n);//选择排序
void EnterArr(int arr[], int n);
void PrintArr(int arr[], int n);

int main()
{
    
    
	int arr[10] = {
    
     7, 6, 8, 2, 1, 4, 0, 9, 2, 3 }; 
	int n = 10;
	SelectSort(arr, n);
	cout << "排好序后:";
	PrintArr(arr, n);
	return 0;
}

void SelectSort(int arr[], int n)
{
    
    
	int i, j, min, temp;
	for (i = 0; i < n - 1; i++)
	{
    
    
		min = i;
		for (j = i + 1; j < n; j++)
		{
    
    
			if (arr[min] > arr[j])
			{
    
    
				min = j;
			}
		}
		if (i != min)
		{
    
    
			temp = arr[min];
			arr[min] = arr[i];
			arr[i] = temp;
		}
	}
}

void EnterArr(int arr[], int n)
{
    
    
	for (int i = 0; i < n; i++)
	{
    
    
		cin >> arr[i];
	}
}

void PrintArr(int arr[], int n)
{
    
    
	for (int i = 0; i < n; i++)
	{
    
    
		cout << arr[i] << " ";
	}
	cout << endl;
}

Time Complexity and Space Complexity

Time complexity: O(n^2)
Space complexity: O(1), because there is a temporary variable temp

quick sort

basic concept

Divide the data to be sorted into two independent parts by one-pass sorting, all the data in one part is smaller than all the data in the other part, and then quickly sort the two parts of the data according to this method, the whole sorting process can be Recursively, so that the entire data becomes an ordered sequence.

C++ code

#include<iostream>
using namespace std;
void Qsort(int arr[], int low, int high);//快速排序
int main()
{
    
    
	int arr[10] = {
    
     7, 6, 8, 2, 1, 4, 0, 9, 2, 3 }; 
	int n = 10;
	Qsort(arr, 0, sizeof(arr) / sizeof(int) - 1);
	cout << "排好序后:";
	PrintArr(arr, n);
	return 0;
}
void Qsort(int arr[], int low, int high)//快速排序
{
    
    
	if (high <= low)
	{
    
    
		return;
	}
		
	int i = low;
	int j = high + 1;
	int key = arr[low];
	while (true)
	{
    
    
		//从左往右找比key大的值
		while (arr[++i] < key)
		{
    
    
			if (i == high)
			{
    
    
				break;
			}
		}
		//从右往左找比key小的值
		while (arr[--j] > key)
		{
    
    
			if (j == low)
			{
    
    
				break;
			}
		}
		if (i >= j)
		{
    
    
			break;
		}
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}
	//中枢值与j对应的值交换
	int temp = arr[low];
	arr[low] = arr[j];
	arr[j] = temp;
	// 左边递归快排
	Qsort(arr, low, j - 1);
	// 右边递归快排
	Qsort(arr, j + 1, high);
}
void PrintArr(int arr[], int n)
{
    
    
	for (int i = 0; i < n; i++)
	{
    
    
		cout << arr[i] << " ";
	}
	cout << endl;
}

Time Complexity and Space Complexity

Time complexity: best case: O(nlogn), worst case: O(n^2), average: O(nlogn)
space complexity: O(logn)

Guess you like

Origin blog.csdn.net/qq_44858592/article/details/107943290