The merging sorting algorithm is a typical application of the idea of divide and conquer, and the algorithm is described in detail below. Algorithm Description
Merge sort algorithm is a kind of effective sorting algorithm based on merging operation, the whole idea is for a sort of array a[0...n-1], merging sort divides it into one first: A[0 ..., (N/2)-1] and a[n/2...,n-1], and then recursively sorts each child array, Finally, the two sorted arrays are merged into an ordered array. Algorithm Analysis
Algorithm Time complexity Analysis:
Best case: O (NLOGN);
Worst case: O (NLOGN);
Average situation: O (NLOGN); algorithm Java code implementation
Import java.util.*; public class MergeSort {public int[] mergesort (int[] A, int n) {//array boundary conditions are judged if (a==null| |
a.length==0) {return null;
Sort (a,0,n-1);
return A;
///recursive sort stage public void sort (int[] data,int left,int right) {//split the array into two parts, sort the merge if (left<right) {
int middle = (left+right)/2;
Sort (data,left,middle);
Sort (data,middle+1,right);
Merge (Data,left,middle,right); }///Two ordered array a,b merge algorithm public void merge (int[] Data,int left,int middle,int right) {//define a temporary array for later storage two
The result of merging an array int[] temp = new int[right-left+1];
The pointer to the array A to be merged, pointing to the first element int leftindex = left;
The pointer to the array b to be merged, pointing to the first element int rightindex = middle+1;
The pointer int tempindex = 0 of the temporary array; Loops compare the size of elements in two arrays and store them in a temporary array by sort (leftindex<=middle&&rightindex<=right) {//The size of each element in the two array is compared, and the smaller is placed in the temporary array, if (Data[leftindex]<=data[rightindex]) {temp[tempindex++] = da
Ta[leftindex++];
}else{temp[tempindex++] = data[rightindex++];
}///When all of the elements in an array are complete, the pointer determines if there are any elements in the other array that are not compared, and, if so, they are all stored in the back of the existing elements of the temporary array in order (Leftindex<=middle) {
temp[tempindex++] = data[leftindex++];
///When all of the elements in an array are complete, the pointer determines if there are any elements in the other array that are not compared, and, if so, they are all stored in the back of the existing elements of the temporary array in order (Rightindex<=right) {
temp[tempindex++] = data[rightindex++];
int temp1 = 0;
Copies the elements in the temporary array to the original array sequentially, completing a merge sort while ((Temp1+left) <=right) {Data[temp1+left] = temp[temp1++]; }
}
}
Merge Method Analysis: In the initial state, the definition of two pointers (the subscript of the sorted array to be merged) points to the first element of the two arrays to be merged, then compare the size of these two elements and add the smaller elements to the newly created temporary array; then the pointer in the copied array moves back one bit, Point to the successor element of the smaller element and continue the comparison process until one of the data is processed. Then, in an unhandled array, the remaining elements are copied to the end of the temporary array, which completes the entire merge process.