Algorithms and Data Structures (3) - Summary of Sorting Algorithms

Six sorting algorithms: insertion sort, Hill sort, selection sort, bubble sort, heap sort, quick sort

1. Insertion sort

1. Starting from the first element, the element can be considered to have been sorted
2. Take the next element tem, and scan from the back to the front of the sorted element sequence
3. If the element is greater than tem, move the element to the next One
bit 4. Repeat step 3 until you find an element that is less than or equal to tem in the sorted elements
5. tem is inserted behind the element, if all the sorted elements are greater than tem, then insert tem into the position with subscript 0
6 .Repeat steps 2~5
and the animation effect is as follows:
Please add a picture description
Idea:
  Among the elements to be sorted, assuming that the first n-1 elements are in order, now insert the nth element into the previously arranged sequence, so that the first n The elements are ordered. Insert all elements according to this method until the entire sequence is in order.
  But we are not sure which part of the elements to be sorted is in order, so we can only think that the first element is in order at first, and insert the following elements into this ordered sequence in turn, until the entire sequence in order.
The c++ code demo is as follows:

void InsertSort(int* arr, int n)
{
    
    
	//这是待插入的元素下标
	for (int i = 1; i < n; i++)
	{
    
    
		//这是已经排序好数组的下标
		for (int j = i - 1; j >= 0; j--)
		{
    
    
			//如果待排序数字大于,则向后移动
			if (arr[j] > arr[j + 1])
			{
    
    
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
			else
			{
    
    
				break;
			}
		}
	}

	//排序结果输出
	for (int i = 0; i < n; i++)
	{
    
    
		cout << arr[i] << "  ";
	}
}

Time complexity: O(N*N) in the worst case, when the column to be sorted is in reverse order, or close to reverse order; in the best case, it is O(N), and the column to be sorted is in ascending order, or close to ascending order.
Space complexity: O(1)

2. Selection sort

Idea: Select a minimum value from the column to be sorted each time, and then place it at the beginning of the sequence until all the data to be sorted is exhausted.
In fact, we can select two values ​​in one pass, a maximum value and a minimum value, and then put them at the beginning and end of the sequence, which can double the efficiency of selection sorting.
The animation effect is as follows:
Please add a picture description
c++ code implementation:

void SelectSort(int* arr, int n)
{
    
    
	//开始从第一个元素进行选择排序
	for (int i = 0; i < n-1; i++)
	{
    
    
		//最小值下标暂存变量
		int min = i;
		//依次从待排序区域取出数据与准备排序的数据进行比较,并暂存最小值下标
		for (int j = i+1; j < n; j++)
		{
    
    
			//如果大于,则设置暂存变量
			if (arr[min] > arr[j])
			{
    
    
				min = j;
			}
		}
		//与找到的最小值下标互换数据
		int temp = arr[i];
		arr[i] = arr[min];
		arr[min] = temp;
	}

	//排序结果输出
	for (int i = 0; i < n; i++)
	{
    
    
		cout << arr[i] << "  ";
	}
}

Time Complexity: Worst Case: O(N^2)
      Best Case: O(N^2)
Space Complexity: O(1)

3. Bubble sort

Idea:
The left side is greater than the right side, and the largest one is on the right side. and so on.
The animation is as follows:
Please add a picture description
The c++ code is as follows:

void BubbleSort(int * arr, int len)
{
    
    

	//如果数组为空或者只有一个数组,则返回
	if (arr == NULL || len<2)
	{
    
    
		return;
	}

	//记录是否存在交换,如果不存在,则说明数组已经有序,直接退出,结束排序
	bool flag = false;
	
	//如果有两个以上的元素时,则开始排序
	//循环外圈
	for (int i = len ; i > 0; i--)
	{
    
    
		for (int j = 0; j < len; j++) 
		{
    
    
			//如果大于,则交换
			if (arr[j] > arr[j+1])
			{
    
    
				arr[j + 1] = arr[j + 1] ^ arr[j];
				arr[j] = arr[j + 1] ^ arr[j];
				arr[j + 1] = arr[j + 1] ^ arr[j];
				flag = true;
			}
		}
		if (!flag) 
			break;
		else 
			flag = false;
	}
	//显示排序结果
	for (int i = 0; i < len; i++)
	{
    
    
		cout << arr[i] << endl;
	}
}

Time Complexity: Worst Case: O(N^2)
      Best Case: O(N)
Space Complexity: O(1)

Fourth, merge sort

Ideas:
1. First split the array

Guess you like

Origin blog.csdn.net/qq_52302919/article/details/130988394