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

What is the merge sort and Master Formula in java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What is the merge sort and Master formula in java? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Basic thought

Merge and sort with the idea of divide and conquer. Borrow a picture to illustrate it.

The n elements are cut from the middle and divided into two parts. (there may be one more number on the left than on the right.) divide step 1 into two parts, and then decompose them recursively. Until the number of elements in all parts is 1. Gradually merge two sequenced sequences from the bottom.

The advantage is that after divide and conquer, the time complexity of the merge sorting process is O (N) (only one scan is needed to combine two ordered numbers into an ordered array)

Implement public static void MergeSort (int [] arr,int l, int r) {if (l = = r | | r < 0) {return;} int middle = 1 + (rmerl) / 2; / / take the middle value to prevent Integer.MaxValue overflow MergeSort (arr,l,middle); MergeSort (arr,middle+1,r); sort (arr,l,middle,r) } / * * @ param arr Array waiting to be sorted * @ first pointer to param l left Array * @ param middle split left Array * @ param r Last pointer to the right Array * / private static void sort (int [] arr, int l, int middle, int r) {int [] temp = new int [arr.length] System.arraycopy (arr, 0, temp, 0, arr.length); int right_first = middle+1; int tempIndex = l; while (l

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

Development

Wechat

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

12
Report