B1035 inserting and merging ideas (code + detailed explanation)

subject

Ideas:

Class B questions will not directly test the data structure, and several sorting algorithms will not let you directly.
In fact, the insertion and merging sorting of this test can be done without fully mastering and writing code (of course, mastering is good). As long as we understand the idea of insertion sort and merge sort. No, I still have to, otherwise I can't ask for the next round of merging.

Insert sorting: the first number of an array is considered as an ordered array, and the remaining n-1 numbers in the array are inserted into the ordered array until all the numbers in the array are ordered.
For this problem, the first m items in the insertion sorting process are ordered. (from the point of view of the topic, it is scanning from the back to the front)

		Code implementation sorting from small to large:
		#include<stdio.h>
		  void InsertionSort(int *num,int n) 
		  {
		  	int i = 0;
		  	int j = 0;
		  	int tmp = 0;
		  	for(i = 1;i<n;i++)//n-1 times for n numbers
		  	{
		          tmp = num[i];//Takes the first element from the group to be inserted. 
			  j = i-1; //i-1 is the subscript of the last element of the ordered group (adjacent to the element to be inserted). At the beginning, j=i-1=0.
			  while(j>=0&&tmp<num[j])  //Note that there are two judging conditions, and j > = 0 limits them. The second is the insertion judgment condition 
			  {
			    num[j+1] = num[j];//If not, the ordered group element moves backward 
				j--; 
			  }
			  num[j+1] = tmp;//Find the right place to insert the element. 
			}
		  }
		  int main() 
		  {
		  	int i = 0;
		  	int num[8]={9,3,4,2,6,7,5,1};
		  	InsertionSort(num,8); 
		  	for(i=0;i<8;i++)
		  	{
		     	printf("%d ",num[i]);
			}
		  	return 0;
		  }

Merge order:
There are two ways to merge and sort
1. Top down
Directly merge and sort the original sequence, and merge and sort the left and right sides respectively in each merging and sorting until they are subdivided into two groups.
2. Bottom up
Suppose the sequence has n elements:
1. Merge and sort two adjacent groups
2. Merge and sort four adjacent groups
3. Merge and sort the adjacent eight eight groups
4. Repeatedly expand the grouping scale until all elements are sorted
5. ...
As for this question, it is obvious that the second kind is used, from the bottom to the top, in two groups.
Code source: https://blog.csdn.net/Silence_R/article/details/86524975

#include<stdio.h>

/*Merge sort*/
void Merge_Sort(int *arr, int *temparr,int start,int mid,int end)
{
    int left_start = start ;
    int left_end   = mid ;

    int right_start = mid+1 ;
    int right_end   = end ;

    int index = start ;

    while(left_start<=left_end&&right_start<=right_end)
    {
        if(arr[left_start]>arr[right_start])
            temparr[index++] = arr[right_start++] ;
        else
            temparr[index++] = arr[left_start++] ;
    }

    while(left_start<=left_end)
        temparr[index++] = arr[left_start++] ;

    while(right_start<=right_end)
        temparr[index++] = arr[right_start++] ;

    for(index = start ;index<=end ;++index)
        arr[index] = temparr[index] ;
}
void Sort_Message(int *arr, int *temparr,int start,int end)
{
    if(start<end)
    {
        int mid = (start+end)/2 ;
        Sort_Message(arr,temparr,start,mid) ;
        Sort_Message(arr,temparr,mid+1,end) ;
        Merge_Sort(arr,temparr,start,mid,end) ;
    }
}

int main(void)
{
    int a[] = {9,2,5,3,7,4,8,0} ;
    int n = sizeof(a)/sizeof(a[0]) ;
    int i, temp[8] ;

    printf("The original sequence is:") ;
    for(i=0;i<n;++i)
        printf("%d ",a[i]) ;
    printf("\n") ;

    Sort_Message(a,temp,0,n-1) ;

    printf("\n Posterior sequence:") ;
    for(i=0;i<n;++i)
        printf("%d ",a[i]) ;
    printf("\n") ;
    return 0 ;
}


After understanding the two sorting methods, the requirements of this question are obvious:
Give the original array, judge whether the first n numbers are in order -- insert sort; else -- merge sort.

Or:
Determine whether the array is in order of two or four -- merge sort; else -- insert sort.
Next round of sorting:
For insertion sorting, sort directly to the next bit with sequence.
For merging and sorting, find the merging and sorting again.

AC code

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int n, a[100], b[100], i, j;
	cin >> n;
	for(int i = 0; i < n; i++)//Original array 
		cin >> a[i];
	for(int i = 0; i < n; i++)//Sort array 
		cin >> b[i];
		
	for(i = 0; i < n - 1 && b[i] <= b[i + 1]; i++);//Judge insertion order: first i order, jump out of loop when disorder is found 
	for(j = i + 1; a[j] == b[j] && j < n; j++);  
	if(j == n){                                //After the above loop is traversed, if j==n, the first i are in order, and the last i+1 to n-1 are not. It's obviously an insert sort. 
		cout << "Insertion Sort" << endl;     
		sort(a, a + i + 2);                   //Insert sort and iterate for another round, that is, sort in the i+1. a[i+1] in the sort function, the address is a+i+2 
	} 
	
	 
	else{                                   //On the contrary, it is a merge sort 
		cout << "Merge Sort" << endl;         //Output the array for another round of sorting 
		int k = 1, flag = 1;                  // 
		while(flag){
			flag = 0;
			for(int i = 0; i < n; i++){       //To traverse array a, as long as there is something different from array b, continue. 
				if(a[i] != b[i])              //If the two arrays are the same, it indicates the round given by the title. Jump out of the loop. 
					flag = 1;
			}
			k = k * 2;                         //k = 2 4 8...n.  Start in pairs and sort each group. 
			for(int i = 0; i < n / k; i++)    //Divide n / k groups and sort them.
				sort(a + i * k, a + (i + 1) * k);
			sort(a + n / k * k, a + n);    //Sort the last group of elements in the sequence.
		}
	}
	for(j = 0; j < n; j++){
		if(j != 0) printf(" ");
		printf("%d", a[j]);
	}
	return 0;
}

Posted on Tue, 09 Jun 2020 03:31:43 -0400 by frosero