C language: bubble sort (ascending sort, nanny-level tutorial)

1. What is bubble sort?

Bubble sort in English is Bubble Sot, which is the most basic exchange sort. It is called bubble sort because each element can be moved to one side of the array bit by bit according to its size, like a small bubble.

2. Under what circumstances can bubble sort be used?

Bubble sorting is relatively simple, but has high time complexity and is used for small-scale sorting.

3. The principle of bubble sort operation:

Compare adjacent elements, and if the first is greater than the second, swap them both; do the same for each pair of adjacent elements, starting with the first pair and ending with the last pair, at which point, The last element should be the largest number. 

 3. Bubble sorting operation flow diagram:

Insert image description here

 4. Specific process explanation (please refer to: http://t.csdn.cn/8aXz2 )

First, we can define an unordered integer array

           arr[10]={8,1,5,6,7,4,3,1,2,0};

According to the principle of bubble sorting, we can compare the first element with its adjacent elements, and exchange the values ​​if the previous element has a greater value than the latter element.

Therefore, we can find that 8 is greater than 1, so the values ​​of the two arrays are exchanged.

Before exchange:

 After exchange:

In the same way, and so on, the exchanged value is compared with the next value to perform value-related operations.

(The process in the middle is too long-winded and repetitive. In order not to delay the audience, I have omitted the small part here. Thank you for your love and hope for your understanding)

 

Until the largest value is sorted to the last position of the array, this is the sorting process of bubble sort.

And we can decompose the entire bubble sorting process into multiple similar sub-processes to achieve sorting of all elements of the array.

Therefore, for the implementation of bubble sort, we can either implement it by using nested loops, or we can implement bubble sort by using function recursion.

5. Summary of the bubble sorting process:

 

 Implementation of the algorithm:

 Bubble sort source code:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

void bubble_sort(int* arr, int n)
{
	for (int i = 0; i < n; i++)//需要n-1
	{
		int flag = 1;
		for (int j = 0; j < n - 1 - i; j++)
		{
			int tmp = 0;
			if (arr[j] > arr[j + 1])
			{
				flag = 0;
				tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;
			}
		}
		if (1 == flag)
			break;
	}
}

int main()
{
	int arr[20] = { 0 };
	int n = 0;
	scanf("%d", &n);	
	for (int j = 0; j < n; j++)
	{
		scanf("%d", &arr[j]);
	}
	int sz = sizeof(arr) / sizeof(arr[0]);
	bubble_sort(arr,sz);
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

 6. Summarize:

This article still has a lot of flaws in the introduction and explanation of bubble sorting. If there are any mistakes, please correct me. Thank you for your love. Thank you to everyone who has read this article. I am very grateful! ! !

Guess you like

Origin blog.csdn.net/2201_75998194/article/details/130900094