Bubble sort (teaching for fools)


train of thought

讲解思路

Bubble sorting is to make a set of unordered data into order, as follows:

Unordered: 1 3 2 6 4 7 5
Ordered: 1 2 3 4 5 6 7
Now we need to consider how to make this set of data orderly. It helps to understand in a few steps first.
The first step
is to compare before and after. We must be familiar with the sequence of numbers in mathematics, such as n and n+1, which represent the previous digit and the next digit , which can be realized with an array.
In the second step,
you need to consider how many times you need to compare, and you can know whether you are using a loop statement or a branch statement by comparing the front and rear bits within one pass.
With a general idea, you can start to operate.


提示:以下是本篇文章正文内容,下面案例可供参考

1. Complete code

完整代码如下

void aa(int* arr, int sz)
{
    
    
	int i = 0;
	for (i = 0; i < sz - 1; i++)	//有多少趟,sz-1因为最后一个元素不需要排序
	{
    
    
		int j = 0;
		int a = 1;
		for (j = 0; j < sz - 1 - i;j++)		//一趟里的比较,sz-1-i这里是根据趟数的变化,比较次数也变化
		{
    
    
			if (arr[j] > arr[j + 1])		//比较判断,这里就相当数学里的数列n跟n+1,就是前一位跟后一位
			{
    
    
				int tem = arr[j];			//前后变量的交换,需要一个临时变量
				arr[j] = arr[j+1];
				arr[j + 1] = tem;
				a = 0;
			}
		}
		if (a == 1)
		{
    
    
			break;
		}
	}
}

int main()
{
    
    
	int arr[] = {
    
     1,4,7,3,8,2 };
	int sz = sizeof(arr) / sizeof(arr[0]);	//计算数组长度
	aa(arr, sz);
	for (int i = 0; i < sz; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
}

2. Code explanation

1. Calculate the length of the array

You can use the sizeof function to find the total size of the array, and then divide it by the first element of the array to get the length of the array.

The code is as follows (example):

int main()
{
    
    
	int arr[] = {
    
     1,4,7,3,8,2 };
	int sz = sizeof(arr) / sizeof(arr[0]);	//计算数组长度
	aa(arr, sz);
	for (int i = 0; i < sz; i++)
	{
    
    
		printf("%d ", arr[i]);
	}
}

2. Array parameter passing

The arr passed is the first element of the array. If the formal parameter wants to change the value of the actual parameter, remember to use the pointer. sz is the calculated value of the array. Don’t forget to pass this.

The code is as follows (example):

//这里我就先简写了

//函数
void aa(int *arr, int sz);

//传参
aa(arr, sz);

The data requested by the url network used here.

3. Sorting explanation

I will explain here in a few steps

这里是排序需要多少趟

int i = 0;
	for (i = 0; i < sz - 1; i++)	//有多少趟,sz-1因为最后一个元素不需要排序
	{
    
    
		
	}

一趟内的比较

int i = 0;
	for (i = 0; i < sz - 1; i++)	//有多少趟,sz-1因为最后一个元素不需要排序
	{
    
    
		int j = 0;					
		for (j = 0; j < sz - 1 - i;j++)		//一趟里的比较,sz-1-i这里是根据趟数的变化,比较次数也变化
		{
    
    
		
		}

前后位的比较

Let's talk about variable exchange in detail here.
The exchange of front and back variables requires a temporary variable, because the value of arr[j] has changed to the value of arr[j+1] at the step of arr[j] = arr[j+1], so a temporary variable is needed The variable holds the value of arr[j].
Fundamental

			if (arr[j] > arr[j] + 1])		//比较判断,这里就相当数学里的数列n跟n+1,就是前一位跟后一位
			{
    
    
				int tem = arr[j];			
				arr[j] = arr[j+1];
				arr[j + 1] = tem;
			}

代码优化

Orderly does not need to be exchanged. You can use temporary variable a to monitor whether the data is exchanged. If a is exchanged, it will be = 0. If there is no exchange, 1 will not change. Then use if and break to jump out of the loop.

void aa(int* arr, int sz)
{
    
    
	int i = 0;
	for (i = 0; i < sz - 1; i++)	
	{
    
    
		int j = 0;
		int a = 1;
		for (j = 0; j < sz - 1 - i;j++)		
		{
    
    
		int a = 0;
		}
		if (a == 1)
		{
    
    
			break;
		}
	}
}


Summarize

有讲解的不好的地方还请指正

Guess you like

Origin blog.csdn.net/m0_66977204/article/details/130535804