The principle of merge sorting and C++ source code implementation


1. Principle

An effective and stable algorithm based on the merge operation, using the divide-and-conquer method to first make each subsequence ordered, and then merge the ordered subsequences to obtain a completely ordered sequence. If two ordered lists are combined into one ordered list, it becomes a two-way merge.

Sort the idea, one is divided into two, two is divided into four, and four is divided into eight, until the division is the smallest; then gradually merge back. Recursive method is adopted when dividing.


2. Thinking

First group the original sequence, divide it into multiple subsequences, sort each subsequence, then merge the subsequences pairwise and sort to get the new ordered sequence as the next merged subsequence, and then put the new subsequence Merge.... At the end of this loop, an ordered sequence is obtained.
Insert picture description here

Example:
Merge and sort data [1, 4, 2, 6, 7, 3, 5, 10, 9].

Initial state: 1, 4, 2, 6, 7, 3, 5, 10, 9

  1. First grouping: {1,4},{2,6},{7,3},{5,10},{9}, after merging, we get {1,4},{2,6},{3 ,7},{5,10},{9}
  2. Second grouping: {1,4,2,6},{3,7,5,10},{9}, after merging, {1,2,4,6},{3, 5,7,10 },{9}
  3. The third grouping: {1,2,4,6,3, 5,7,10},{9}, after merging, we get {1,2,3,4,5,6,7,10},{9 }
  4. The fourth grouping: {1,2,3,4,5,6,7,10,9}, after merging, we get [1,2,3,4,5,6,7,9,10]

Three, merge sort characteristics

  • Stable sorting, the order of equal elements will not change. For example, when inputting record 1(a) 3(b) 2(c )2(d) 5(e) (the key of the record is in parentheses), output 1(a) 2(c )2(d) 3(b) ) 2 and 2 in 5(e) are in the order of input. This is very important when the data to be sorted contains multiple information and must be sorted by one of them, and other information is required to be arranged in the order of input as much as possible.

  • The number of comparisons of merge sort is less than that of quick sort, and the number of moves is generally more than that of quick sort.


Four, C++ source code implementation

After the split is completed, the split data needs to be sorted. At this time, the merge function has a total of 5 parameters
sort(int *data, int *temp, int start, int middle, int end); here the temporary variable temp is used As an intermediate variable in the two sorted sets, a set is synthesized.


void merge(int arr[], int *temp, int start, int middle, int end)
{
    
    
	int i=start, j=middle+1,k=start;
	
	while(i<=middle && j<=end)
	{
    
    
		if(arr[j] < arr[i])
		{
    
    
			temp[k++]=arr[j++];
		}
		else
		{
    
    
			temp[k++]=arr[i++]
		}
	}
/*
这里表示的是几集合2的数值已经先排序完成,集合1剩下有数据剩余,
这个时候直接把集合1剩余的数据直接插入到合集的后面,比如两个集合,
集合1:1 3 8 9,  集合2:2 4 6 7,集合1和集合2的合集到1 2 3 4 6 7,
此时集合2内的数据已经合并完毕,集合1还有8 9 数据,这个时候直接追加到合集中即可
*/
	while(i <= middle)
	{
    
    
		temp[k++]=arr[i++];
	}
	//与上while类似,只是这里剩余的是集合2的数据
	while(j<=end)
	{
    
    
		temp[k++]=arr[j++];
	}
	
	//把临时变量中的数值拷贝到数组中
	for(i=start; i <=end; i++)
	{
    
    
		arr[i]=temp[i];
	}
}

//start,end参数均为数值的下标值
void mergeSort(int arr[], int *temp, int start, int end)
{
    
    
	int middle=0;
	if(start< end)
	{
    
    
		middle=start+(end-start)/2;
		mergeSort(arr, temp, start, middle);
		mergeSort(arr, temp, middle+1, end);
		merge(arr, temp, start, middle, end);
	}
}

//调用函数
int main()
{
    
    
	int arr[9]={
    
    1,4,2,6,7,3,5,10,9};
	int temp[9]={
    
    0};
	int len=9;
	
	mergeSort(arr, temp, 0, len-1);
}

Guess you like

Origin blog.csdn.net/locahuang/article/details/110181499