Three basic sorting algorithms (bubble sort, selection sort, insertion sort)

Three basic sorting algorithms (bubble, select, insert)

1. Bubble sorting method

Principle analysis:

Time complexity: O(n²)

  1. Compare adjacent elements. If the first one is larger than the second one, swap the two of them.
  2. Do the same work for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. At this point, the last element should be the largest number.
  3. Repeat the above steps for all elements except the last one.
  4. Continue to repeat the above steps for fewer and fewer elements each time until there is no pair of numbers to compare.

Code:

Complete realization through two-layer circulation

Outer loop: bubbling times

Inner loop: bubbling times

note:

1 Every time one more data is arranged, the number of inner loops can be reduced by one, thereby improving efficiency.

2 In total, only n-1 data needs to be sorted, and the remaining one is the minimum value, and there is no need to sort again

int main() {
    
    
	// 定义一个未序一维数组
	int arr[10] = {
    
     1,3,6,9,5,8,-1,2,5,7 };

	// 冒泡排序
	// 外层循环: 控制比较的"趟数",每一趟排好一个数据
	for (int i = 9; i > 0; i--)
	{
    
    
		// 内层循环: 控制比较的"次数"
		// 次数受外层循环控制 每趟少比较一次
		for (int j = 0; j < i; j++) {
    
    
			// 比较大小
			// 当前数据比后一个大
			if (arr[j] > arr[j + 1]) {
    
    
				// 交换
				arr[j] = arr[j] ^ arr[j + 1];
				arr[j + 1] = arr[j] ^ arr[j + 1];
				arr[j] = arr[j] ^ arr[j + 1];
			}
		}
	}
    
    // 输出
	for (size_t i = 0; i < 10; i++)
	{
    
    
		printf("%d ",arr[i]);
	}

	return 0;
}

2. Selection and Sorting Method

Principle analysis:

Time complexity: O(n^2)

First find the smallest (large) element in the unsorted sequence, store it at the beginning of the sorted sequence, and then continue to find the smallest (large) element from the remaining unsorted elements, and then put it at the end of the sorted sequence. And so on, until all elements are sorted.

Code:

Two-level loop nesting, the inner loop finds the subscript of the maximum value

note:

  1. When selecting the maximum value, assume that the first data is the largest and update the subscript when it encounters a larger one.
  2. The subscript of the maximum value before each cycle should be reset
#include<stdio.h>

int main() {
    
    
	// 定义一个未序一维数组
	int arr[10] = {
    
     1,3,6,9,5,8,-1,2,5,7 };

	// 选择排序
	int maxIndex = 0;
	int temp;
	for (int i = 9; i > 0; i--)
	{
    
    
		maxIndex = 0;
		for (int j = 0; j <= i; j++) {
    
    
			if (arr[maxIndex] < arr[j]) {
    
    
				maxIndex = j;
			}
		}
		if (maxIndex != i) {
    
    
			temp = arr[maxIndex];
			arr[maxIndex] = arr[i];
			arr[i] = temp;
		}
	}
    
    // 输出
	for (size_t i = 0; i < 10; i++)
	{
    
    
		printf("%d ",arr[i]);
	}

	return 0;
}

Three. Insertion sort method

Principle analysis:

Time complexity: O(N^(1-2))

Insert elements into corresponding positions in an ordered array

For unsorted arrays, whether in ascending or descending order, the front element (single element) can be regarded as ordered

Code:

Treat the frontmost element as an ordered array, and select the position to insert according to the sorting rules.

Two levels of loop nesting.

Outer loop: number of data

Inner loop: control the number of comparisons

#include<stdio.h>

int main() {
    
    
	// 定义一个未序一维数组
	int arr[10] = {
    
     1,3,6,9,5,8,-1,2,5,7 };

	// 插入排序
	for (int i = 1; i < 10; i++)  // 进行9次排序(第0个元素当做已序)
	{
    
    
		// 内层循环: arr[i]从arr[1]开始比较
		for (int j = i - 1; j >= 0; j--) {
    
    
			// 比较
			if (arr[j + 1] < arr[j]) {
    
    
				// 如果后面的值小于(升序)/大于(降序)前面的值则交换
				arr[j + 1] = arr[j + 1] ^ arr[j];
				arr[j] = arr[j + 1] ^ arr[j];
				arr[j + 1] = arr[j + 1] ^ arr[j];
			}
			else break; // 减少多余的判断
		}
	}
    
    // 输出
	for (size_t i = 0; i < 10; i++)
	{
    
    
		printf("%d ",arr[i]);
	}

	return 0;
}

r[j + 1] ^ arr[j];
			}
			else break; // 减少多余的判断
		}
	}
    
    // 输出
	for (size_t i = 0; i < 10; i++)
	{
    
    
		printf("%d ",arr[i]);
	}

	return 0;
}

Guess you like

Origin blog.csdn.net/zhuiyizhifengfu/article/details/114052848