MergeSort

/**************************************************** ********************************

Merge Sort (Merge Sort, Taiwan translation: Merge Sort) is a method based on the merge operation. an efficient sorting algorithm. This algorithm is a very typical application of Divide and Conquer.

The merge operation (merge), also known as the merge algorithm, refers to the operation of merging two sorted sequences into one sequence. The working principle of the merge operation is as follows:
1. Apply for a space whose size is the sum of the two sorted sequences, and this space is used to store the merged sequence.
2. Set two pointers whose initial positions are the starting positions of the two sorted sequences.
3. Compare the elements pointed to by the two pointers, select a relatively small element to put into the merge space, and move the pointer to the next position.
4. Repeat step 3 until a pointer reaches the end of the sequence.
5. Copy all remaining elements of the other sequence directly to the end of the merged sequence.

The specific working principle of merge sort is as follows (assuming that the sequence has n elements in total):
1. Merge every two adjacent numbers in the sequence to form floor(n / 2) sequences. After sorting, each sequence contains two element.
2. Merge the above sequences again to form floor(n/4) sequences, each of which contains four elements.
3. Repeat step 2 until all elements are sorted.

The number of comparison operations is between (nlogn)/2 and nlogn - n + 1. The number of assignment operations is (2nlogn). The space complexity of the merge algorithm is: Θ(n)

************************************************************************
void Merge(int array[], int first, int mid, int last)
{
	int i, j = 0;
	int begin1 = first, end1 = mid, begin2 = mid + 1, end2 = last;
	int *temp = (int *)malloc((last - first + 1) * sizeof(int));
	if (!temp)
	{
		fprintf(stderr, "\nMemory allocation failed, program will force quit!\n");
		getch();
		exit(1);
	}
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (array[begin1] < array[begin2])
		{
			temp[j++] = array[begin1];  begin1++;
		}
		else
		{
			temp[j++] = array[begin2];  begin2++;
		}
	}
    while (begin1 <= end1)
	{
		temp[j++] = array[begin1++];
	}
	while(begin2 <= end2)
	{
		temp[j++] = array[begin2++];
	}
	for (i = 0; i < (last - first + 1); i++)
	{
		array[first + i] = temp[i];
	}
	free(temp);
}

void _MergeSort(int *array, int first, int last)
{
	int mid;
	if (first < last)
	{
		mid = (first + last) / 2;
		_MergeSort(array, first, mid);
		_MergeSort(array, mid + 1, last);
		Merge(array, first, mid, last);
	}
}

void MergeSort(int *array, int length)
{
	_MergeSort(array, 0, length - 1);
}

*/

This code took me one night, haha, I am still not skilled enough, I have no clue at all, I have been thinking about it for a long time, and now I can't even understand the time performance very well.
This sorting algorithm uses a divide and conquer strategy,
1) Decomposition
2) Solve
3) Merge
Three steps, broken down into small steps, sometimes solved and merged together, merge sort is just that.
The idea is: decompose a set of numbers continuously through recursion, exit the recursive nesting through a certain condition, and execute the solution sorting and merging

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325742788&siteId=291194637