Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize merging and sorting by java

Shulou Source: shulou.com Published: 2022-06-02 05:02:25 10月02日 Update

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)

Tags: Sorting array learning two more jobless recursive help complex practical ordered next size elements only complexity articles methods conditions theory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Linux MariaDB vpn