C language implementation of bubble sort algorithm

The English name of bubble sort is Bubble Sort, which is the most basic exchange sorting method.

Bubble sort is a simple sorting algorithm and it is also a stable sorting algorithm. The implementation principle is to repeatedly scan the sequence to be sorted, compare each pair of adjacent elements, and exchange the elements when the order of the pair is incorrect. This process is repeated until no two adjacent elements can be exchanged, indicating that the sorting is completed.

Generally speaking, calling a sorting algorithm stable means that when there are the same elements in the sequence to be sorted, their relative positions will not change before and after sorting.

Assume that the sequence to be sorted is (5,1,4,2,8). If bubble sort is used to sort it in ascending order (from small to large), the entire sorting process is as follows:
1) The first round of sorting. At this time, the elements in the entire sequence are located in the sequence to be sorted. Each pair of adjacent elements is scanned in turn, and the positions of the element pairs with incorrect order are exchanged. The whole process is as shown below shown.

As you can see, after the first round of bubble sorting, the maximum number 8 was found from the sequence to be sorted, and placed at the end of the sequence to be sorted, and merged into the sorted sequence.

2) The second round of sorting. At this time, the sequence to be sorted only contains the first 4 elements. Each pair of adjacent elements is scanned in turn, and the positions of elements in incorrect order are exchanged. The whole process is as shown below shown.

As you can see, after the second round of bubble sorting, the maximum number 5 was found from the sequence to be sorted, and placed at the end of the sequence to be sorted, and merged into the sorted sequence.

3) The third round of sorting. At this time, the sequence to be sorted contains the first 3 elements. Each pair of adjacent elements is scanned in turn, and the positions of elements in incorrect order are exchanged. The whole process is as shown in the figure below. Show.

After this round of bubble sorting, the maximum number 4 is found from the sequence to be sorted, and placed at the end of the sequence to be sorted, and merged into the sorted sequence.

4) The fourth round of sorting. At this time, the sequence to be sorted contains the first 2 elements. The entire process of bubble sorting is shown in the figure below.

After this round of bubble sorting, the maximum number 2 is found from the sequence to be sorted, and placed at the end of the sequence to be sorted, and merged into the sorted sequence.

5) When performing the fifth round of bubble sorting, since there is only 1 element left in the sequence to be sorted, no matter how adjacent elements are compared, it is directly merged into the sorted sequence. , the sequence at this time is considered a sorted sequence (as shown in the figure below).

The implementation code of the bubble sort algorithm is as follows (C language):

//冒泡排序算法的简单实现
#include <stdio.h>
int main()
{
	int a[10] = { 21,15,36,3,45,67,-13,99,-10,-2 };
	int n = sizeof(a) / sizeof(a[0]);
	for (int i = 0; i < n - 1; i++)		//比较的轮数
	{
		for (int j = 0; j < n - 1 - i; j++)		// 每一轮需要比较的次数
		{
			if (a[j] > a[j + 1])		//比较相邻数组元素的大小,顺序不对即交换
			{
				int tmp = a[j + 1];
				a[j + 1] = a[j];
				a[j] = tmp;
			}
		}
		//输出本轮冒泡排序之后的序列
		printf("第%d轮冒泡排序结果: ", i + 1);
		for (int loop = 0; loop < n; loop++)
		{
			printf("%d ", a[loop]);
		}
		printf("\n");
	}
	return 0;
}

The running results are as follows

The first round of bubble sorting results: 15 21 3 36 45 -13 67 -10 -2 99
The second round of bubble sorting results: 15 3 21 36 -13 45 -10 -2 67 99
The results of the third round of bubble sorting: 3 15 21 -13 36 -10 -2 45 67 99
The fourth round of bubble sorting Sorting results: 3 15 -13 21 -10 -2 36 45 67 99
The fifth round of bubble sorting results: 3 -13 15 -10 -2 21 36 45 67 99
The result of the 6th round of bubble sorting: -13 3 -10 -2 15 21 36 45 67 99
The result of the 7th round of bubble sorting: -13 -10 -2 3 15 21 36 45 67 99
The result of the 8th round of bubble sort: -13 -10 -2 3 15 21 36 45 67 99
The 9th round of bubble sorting Sorting results: -13 -10 -2 3 15 21 36 45 67 99

By analyzing the implementation code of bubble sort, we can know:

The worst time complexity of this algorithm isO(n2), the optimal time complexity is O(n), and the average time complexity is < a i=3>. O(n2)

Guess you like

Origin blog.csdn.net/GYN_enyaer/article/details/125946918