C language: binary search (half search), bubble sort

Table of contents

1. Binary search

Notes on binary search:

2. Bubble sort

 Notes on bubble sorting:


This blog explains in detail several commonly used methods, namely binary search and bubble sorting

1. Binary search

Binary search means that it is divided into two parts each time, compare the searched number with the middle number, determine the part of the searched number after judging the size , and then continue to compare this part of the number and divide it into two parts to search for And so on.

The use of binary search is more efficient than finding the required numbers one by one , but it should be noted that the condition for using binary search must be a sorted array (this blog explains the binary search method of sequential arrays ), otherwise binary search cannot be used.

Here is the code part of the binary search:

int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int n = 8;
	int sz = sizeof(arr) / sizeof(arr[0]);
	int left = 0;
	int right = sz - 1;
	while (left <= right)
	{
		int mid = left + (right - left) / 2;
		if (arr[mid] < n)
		{
			left = mid + 1;
		}
		else if (arr[mid] > n)
		{
			right = mid - 1;
		}
		else
		{
			printf("找到所查找的数字,该数字下标是%d\n", mid);
			break;
		}
	}
	if (left > right)
	{
		printf("未找到该数字!\n");
	}
	return 0;
}

The result of the operation is as follows:

Notes on binary search:

1、int n = 8;

Here n is the number you are looking for

2、int sz = sizeof(arr) / sizeof(arr[0]);

sz counts how many elements are in the array

Here sizeof(arr) is to calculate the size of the entire array, sizeof(arr[0]) is to calculate the size of the first element of the array, divide the size of the entire array by the size of the first element of the array, that is, calculate how many elements there are in the array

3、int left = 0;int right = sz - 1;

Let the subscript of the first element of the array be left, and the subscript of the last element of the array be right, so as to facilitate subsequent comparisons

4、int mid = left + (right - left) / 2;

mid is the subscript of the number to be compared. As for why not int mid = (left + right)/2, it is because if the values ​​​​of left and right are large, left+right may cause overflow, so use int mid = left + (right - left) / 2;

5. if (left > right) printf("The number is not found!\n");

In the while loop, left<=right, if no number is found until the end, then left will be mid+1 more than right, so if it is judged that left>right is true, it means that the searched number was not found, and "the number was not found" is printed out.


2. Bubble sort

Bubble sorting actually starts from the first element of the array and compares it with its adjacent elements. If the order is not satisfied, it is replaced, and then compared with the next element, and so on...

Bubble sorting is to sort the elements in the array in ascending order , so as to fulfill more requirements

Here is the code for bubble sort:

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

int main()
{
	int arr[] = { 9,8,7,6,5,4,3,2,1,0 };
	int sz = sizeof(arr) / sizeof(arr[0]);

	bubble_sort(arr, sz);

	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

The result of the operation is as follows:

 Notes on bubble sorting:

1、int sz = sizeof(arr) / sizeof(arr[0]);

This line of code in the main function is the same as the binary search, which calculates the number of elements in the array

2、bubble_sort(arr, sz);

Create a bubble_sort function for the specific operation of bubble sorting, and pass in the address arr of the first element of the array , and the number of array elements sz

3、void bubble_sort(int* arr, int sz)

int* arr is to accept the arr passed in the main function, that is, the address of the first element of the array, received by the integer pointer int*

int sz is to receive the number of array elements passed in the main function

4、for (i = 0; i < sz - 1; i++)

First of all, for the first for loop, it is for the total number of loops that need to be looped

As for why it loops sz-1 times, because for example, for 3 elements, the first pass is to compare the first element with the other two elements, the second pass is to compare the second element with the third element, and the third element There is no need to compare, so 3 elements only need to loop 2 times, and so on, sz elements need to loop sz-1 times

5、for (j = 0; j < sz - 1 - i; j++)

For the second for loop, it is the number of times required for each loop

Why is it looping sz - 1 - i times, because originally each element is compared sz-1 times, but this will lead to repeated comparisons. After the first element is compared with all subsequent elements, the latter elements will not be compared It needs to be compared with the first element again, so in addition to the normal order of sz-1, the number of cycles per cycle is subtracted from i, which is the number of elements that have been compared before, which is the number of comparisons required for each cycle sz- 1 -i

6、

These lines of code are easy to understand. If the order is not satisfied, set flag = 0; then create a temporary variable tmp, and complete the exchange of subscript j and subscript j+1 elements 

7、if (flag == 1) break;

The flag variable here is to improve efficiency , mainly to prevent the elements in an array from being sorted in order , but it still performs two loops, which is time-consuming and labor-intensive. So how to improve it?

Int flag = 1 is defined in the first for loop; after the first loop of the second for loop, if the flag is still 1, it means that the entire array elements themselves are sorted in order, so break jumps out of the first A for loop, the entire loop ends , thus improving efficiency

If a swap occurs, it means that the elements in the array are not sorted in order, then change the flag to 0, ignore the if statement below, and perform normal bubble sorting.


This is the end of this blog. If you are interested in related content, you can follow my other blogs♪(^∇^*) 

Guess you like

Origin blog.csdn.net/m0_64411530/article/details/125237300