Merge sort is a process of combining two ordered sequences into one sequence. When merging the two sequences, the more ordered the merge, the faster the merge. There are many applications of merge sort. Put the two questions I have done by myself using merge sort AC. One is (reverse ordinal number) and the other is (the title of the manuscript organized by the 2017ICPC Inner Mongolia Autonomous Region Provincial Competition was still a freshman and newbie would not be able to do it, put it on Now that I'm a sophomore and a junior, it's not easy for me, let's not talk about continuing our merge sort idea)
Its principle is to first split the elements in the array:
For example 6, 5, 2, 1, 3, 8, 9;
first column: 6 5 2;
Second column: 1 3 8 9;
Then the first column continues to split until 6 5 2 and then starts to merge
Two arrays are merged and compared 6 and 5. 6 is larger than 5, then temp[] stores 5 6 (temp is a temporary array, stored in order from small to large)
Then 5 6 and 2 are compared and finally 2 5 6
The same is true for the second column, because 1 3 8 is ordered, so there is no need to shoot the last one is 1 3 8
Finally, the first and second columns are merged
2 5 6
1 3 8 9
First_a is the starting position of the first column
Second_a is the starting position of the second column
The two arrays first compare 2 to be bigger than 1 so the Second_a pointer is moved backward and 1 is placed in the temporary array then 2 is compared with 3 then 2 is compared then 5 is compared with 3 then 3 is placed and so on
Finally, the first column is finished, but the 9 in the second column is not saved, so I need to deal with it and save it (here, let me talk about why I need to save it directly because each of my arrays is an ordered array, so the latter is bigger than the former, so don't worry It’s better to save it directly
Not much to say about the code:
//Merge algorithm from small to large ////The parameters inside are the array of data storage, start position, middle position, end position, temporary array void Merge(int arr[], int start, int mid, int end, int temp[]) { int First_a = start; int Second_a = mid + 1; //Indicate how many elements there are in the auxiliary space int length = 0; //Merge two sorted sequences //If one of the two arrays reaches the end, it ends because while(First_a <= mid && Second_a <= end) { //If the first array is smaller than the second, then I put the first array if(arr[First_a] < arr[Second_a]) { temp[length++] = arr[First_a++]; } else { temp[length++] = arr[Second_a++]; } } //Because there may be an end and another array is not finished, so we have to deal with it while(First_a <= mid) { temp[length++] = arr[First_a++]; } while(Second_a <= mid) { temp[length++] = arr[Second_a++]; } //Finally, assign the value in the auxiliary space to the original space for(int i = 0; i < length; i++) { //Because I started from start, start + i to start arr[start + i] = temp[i]; } } // merge sort //The parameters inside are the array to store the data, start position, end position, temporary array void Merge_Sort(int arr[], int start, int end, int temp[]) { if(start >= end) { return ; } int mid = (start + end) / 2; // merge left half Merge_Sort(arr, start, mid, temp); // merge right half Merge_Sort(arr, mid + 1, end, temp); //merge Merge(arr, start, mid, end, temp); }
//The complete code is as follows:
# include <iostream> # include <numeric> # include <algorithm> # include <functional> # include <list> # include <map> # include <set> # include <stack> # include <deque> # include <queue> # include <vector> # include <ctime> # include <cstdlib> # include <cmath> # include <string> # include <cstring> using namespace std; //The parameter in the creation array is to create an array of how many data can be stored int* Creat_Arr(int length) { int *arr = (int *)malloc(sizeof(int) * length); // int *arr = new int [length]; srand((unsigned int)time(NULL)); for(int i = 0; i < length; i++) { arr[i] = rand() % 10; } return arr; } //The parameter passed to print the array is an array, and the other is the length of the array void Print(int arr[], int length) { for(int i = 0; i < length; i++) { i != length - 1 ? cout << arr[i] << " " : cout << arr[i] << endl; } } //Merge algorithm from small to large ////The parameters inside are the array of data storage, start position, middle position, end position, temporary array void Merge(int arr[], int start, int mid, int end, int temp[]) { int First_a = start; int Second_a = mid + 1; //Indicate how many elements there are in the auxiliary space int length = 0; //Merge two sorted sequences //If one of the two arrays reaches the end, it ends because while(First_a <= mid && Second_a <= end) { //If the first array is smaller than the second, then I put the first array if(arr[First_a] < arr[Second_a]) { temp[length++] = arr[First_a++]; } else { temp[length++] = arr[Second_a++]; } } //Because there may be an end and another array is not finished, so we have to deal with it while(First_a <= mid) { temp[length++] = arr[First_a++]; } while(Second_a <= mid) { temp[length++] = arr[Second_a++]; } //Finally, assign the value in the auxiliary space to the original space for(int i = 0; i < length; i++) { //Because I started from start, start + i to start arr[start + i] = temp[i]; } } // merge sort //The parameters inside are the array to store the data, start position, end position, temporary array void Merge_Sort(int arr[], int start, int end, int temp[]) { if(start >= end) { return ; } int mid = (start + end) / 2; // merge left half Merge_Sort(arr, start, mid, temp); // merge right half Merge_Sort(arr, mid + 1, end, temp); //merge Merge(arr, start, mid, end, temp); } int main(int argc, char *argv[]) { int n; cout << "Please enter the array you want to create:"; cin >> n; int *temp = new int [n]; int *arr = Creat_Arr(n); cout << endl << "原数组:" << endl; Print(arr, n); cout << "After sorting:" << endl; Merge_Sort(arr, 0, n - 1, temp); Print(arr, n); return 0; }
The result is as follows: