How to realize merging and sorting by java
This article mainly introduces "how to merge and sort java". In the daily operation, I believe that many people have doubts about how to achieve merger and sorting in java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve merger and sorting in java". Next, please follow the editor to study!
Public class MergeSort {
Public static int arr [] = {2, 4, 4, 7, 7, 8, 9, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 5, 4, 4, 5, 4, 6, 4, 4, 4, 6, 4, 4, 6, 4, 4, 6, 4, 4, 6, 4, 6, 7, 5, 4, 4, 6, 5, 4, 4, 5, 4, 5, 4, 5, 4, 5, 1, 4, 6, 4, 5, 4, 4, 4, 4, 4, 4, 5, 4, 1, 4, 4, 4, 4, 1, 4, 4, 4, 4, 4, 4, 1, 4, 4, 4, 1, 4, 1, 4, 4, 4, 5
Public static void main (String [] args) {
Sort (0memarr.futhmur1); / / sort.
Print (); / / print
}
/ / split
Public static void sort (int left,int right) {
If (left > = right)
Return
/ / Recursive termination condition, a subarray has only one or two elements
If (right-left==1) {
If (Arrleft] > arr [right]) {
Int t = arr [left]
Arr [left] = arr [right]
Arr [right] = t
}
Return
}
/ / Recursive decomposition
Int mid = (left + right) / 2
Sort (left,mid)
Sort (mid+1,right)
/ / merge
Merge (left,mid,right)
}
/ / merge
Public static void merge (int left,int mid,int right) {
Int I = left
Int j = mid+1
Int k = 0
/ / Open up temporary arrays and merge two ordered subarrays
Int temp [] = new int [right-left+1]
While (I)