[Algorithms] The logic is very meticulous and the ideas are clear, so you can learn the common sorting algorithm series (><*) o at one time. Merge sorting

✨Preface✨:

Algorithms are a programmer's internal work, which can well reflect the programmer's programming thinking. By learning and mastering common algorithms, not only can you improve coding ability, but also make it easier to stand out in pen interviews. This column will record the process of blogger brushing algorithm topics, and update some high-quality algorithm topics from time to time. If it helps, don't forget to support three series!

Catalog

✨Preface✨:

✨The idea of merging and sorting✨

💎The general idea of merging and sorting💎

💎How exactly are they merged?💎

✨Total code for merge sort✨

✨Time Complexity Analysis of Merge Sorting✨

✨The idea of merging and sorting✨

💡: Most of our common merge sorting is written recursively. Merge sorting is based on comparison sorting, and all exchanges of merge sorting are done in the merge phase, so to understand merge sorting, we need to dig deeply into the specific operation of merge under the general idea of merge sorting!

Believe that through the above, you already know how to learn merging and sorting, so let me explain the general idea of merging.

💎The general idea of merging and sorting💎

🔑: You should all be familiar with the nature of recursion. Recursion is actually a way to simplify the problem step by step. So when we write recursion, we must grasp the macro view and the general idea of simplifying the problem.

The general idea of merge sort is that we want a range to be ordered. We just need to divide the range into two parts, first order the left part of the range, and then order the right part of the range. Then we just need to merge the two ordered sequences into the whole sequence (merge operation)We just need to keep dividing and advancing the whole array along this general line until the last dividing results in only one element in a half-area. We just need to merge back and forth to get the whole in order.

💎How exactly are they merged?💎

🔑: By learning above, we know that we merge in a recursive backtrace, and that the left and right parts are ordered before each merge, so the problem is how to merge two ordered sequences into a whole and keep them ordered.

How can I solve this problem?

*This question is very simple and you will soon think that we know that the merged minimum must come from the minimum of the left and right parts, so we get the minimum, and we remove the minimum from the left and right parts. Similarly, the second smallest value must come from the existing minimum in the left and right parts, and so on.So, the whole is ordered, and of course, to store the whole, we need to open up an auxiliary array to store the whole.

*💡: I believe you still have a lot of doubts here, after all, code can not be said to be empty forever. Next, I will give the code, I hope you can carefully analyze the code, and practice several times by yourself, many doubts can be solved in practice.

✨Total code for merge sort✨

#include<stdio.h>
#include<stdlib.h>
void print_arr(int arr[], int sz)
{
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
}
void merge(int arr[], int L, int M, int R)
{
	int p1 = L;
	int p2 = M + 1;
	int i = 0;
	int* help = (int*)malloc((R + 1 - L) * sizeof(int));
	while (p1 <= M && p2 <= R)
	{
		//Compare small placements of left and right parts (existing global minimum placement)
		help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++];//Create an auxiliary array
	}
	while (p1 <= M)
	{
		help[i++] = arr[p1++];
	}
	while (p2 <= R)
	{
		help[i++] = arr[p2++];
	}
	for (int i = 0; i < (R + 1 - L); i++)
	{
		arr[L + i] = help[i];
	}
	free(help);
	help= NULL;
}
void process(int arr[], int L, int R)
{
	if (L == R)
	{
		return;
	}
	int mid = L + ((R - L) >> 1);
	process(arr, L, mid);//Recursively Ordered Left
	process(arr, mid + 1, R);//Recursively Ordered Right
	merge(arr,L,mid,R);//Merge operation
}
void mergeSort(int arr[], int sz)
{
	process(arr,0,sz-1);
}
int main()
{
	//This is a test
	int arr[10] = { 2,7,5,1,3,9,10,2,8,4 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	mergeSort(arr, sz);//Merge Sort
	print_arr(arr, sz);
	return 0;
}

✨Time Complexity Analysis of Merge Sorting✨

The above code can also be optimized for reference only. If there is a better algorithm, we hope to be able to inform you privately. Thank you.

Since my level is very limited, please let me know if there are any errors! If it helps, don't forget, thank you very much.

Give the thumbs-up👍Collection✨Concern✌

 

 

 

Tags: C Algorithm Interview

Posted on Thu, 30 Sep 2021 14:09:50 -0400 by wiccanwoman1103