4-C language - bubble sorting and the use of array names

question:

        Sort the array in ascending order from smallest to largest.

Thought:

  1. Generally, bubble sorting is performed, encapsulated, and written as a function.
  2. This requires the array to be passed in, and when passing the array, the address of the first element of the array is actually passed. Therefore, if you want to calculate the number of array data inside the bubble, use sizeof (a)/sizeof (a[0]) ; is actually 1/1=1, because the a here is not the array name, but the first address of the element, which cannot represent the entire array.
  3. For array names, such as int a[10]; only in the main function, sizeof(a) and &a, represent the entire array. In addition, &a+1. represents the address of the element at the end of the array, directly spanning the entire array. In other cases, all indicate the first address of the element.
  4. Therefore, when passing parameters to the array, bubble_sort(a), the address of the first element of a is passed. What is passed into the function is a pointer variable, int *a. At this time, the pointer is 8 bytes under a 64-bit operating system and 4 bytes under a 32-bit operating system. And int a[0] is an array variable, an int array, and a variable is 4 bytes.
  5. Therefore, we need to calculate the number of data in the array in the main function, and then pass it into the function,
  6. Then write bubble sort. A total of n-1 bubbles are needed, and each trip needs to be compared one by one from beginning to end. Each trip will compare one less, because after each trip, the one with the larger value reaches the end, and you don’t need to worry about it the next day. , so each time it is less than 1.
  7. Also, for optimization. Not all arrays are unordered, and some have only one number that is wrong, but they still need to be compared all times.
  8. Therefore, before each comparison, define a flag to record whether it has changed. If it changes, the flag changes to 0, otherwise it changes to 1, which proves that the numbers in the array are in order, and jumps out directly without performing subsequent operations.
  9. The general idea of ​​bubble sorting: one-dimensional array sorting, two for loops, the total number of runs in the outer loop, and the inner loop is the actual comparison of the arrays. During each pass, compare them two by two. If it is smaller than it, Then the positions are exchanged, and the one with the larger number runs backward. After each trip, the number of this trip falls at the end, and there is no need to move it. Therefore, there is no need to worry about it in the next comparison, so the number of each comparison is 1 less.

code show as below:

#include <stdio.h>
void bubble_sort(int *a,int n)
{
	int i, j;
	for (i = 0; i < n - 1; i++)
	{
		int flag = 1;
		for (j = 0; j < n - 1 - i; j++)
		{
			if (a[j] > a[j + 1])
			{
				int temp = a[j];
				a[j] = a[j + 1];
				a[j+ 1] = temp;
				flag = 0;
			}
		}

		if (flag == 1)//如果,数组中,已经不需要比大小了,直接跳出,不需要给趟数跑完,因为每一趟,都在检查数组内元素是否大于小于,
			break;
	}
}
int main()
{
	int a[10] = {5,2,3,6,4,8,9,5,6,23};

	int sz = sizeof(a) / sizeof(a[0]);

	int p;
	for (p = 0; p < sz; p++)
	{
		printf("%d ", a[p]);
	}
	printf("\n");
	bubble_sort(a,sz);
	int i;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", a[i]);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_59844149/article/details/131503207