Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize merging and sorting by java

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report