In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to achieve Java merging and sorting". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Merge sorting is an effective sorting algorithm based on merge operation. This algorithm is a very typical application of divide-and-conquer (Divide and Conquer). NlogN is stable because it requires pairwise comparisons.
First consider how to merge two ordered sequences. This is very simple, as long as you compare the first number of the two series, you will take the one who is small first, and then delete the number from the corresponding sequence. Then make a comparison, and if there is an empty column, you can simply take out the data of another sequence in turn.
/ merge ordered arrays a [] and b [] into c [] void MemeryArray (int a [], int n, int b [], int m, int c []) {int I, j, k; I = j = k = 0; while (I < n & & j < m) {if (a [I] < b [j]) c [k +] = a [I] Else c [KBG +] = b [JBG +];} while (I < n) c [KBG +] = a [iChu +]; while (j < m) c [KBG +] = b [Jake +];}
It can be seen that the efficiency of merging ordered sequences is relatively high, which can reach O (n).
After solving the above problem of merging ordered series, and then looking at merging and sorting, the basic idea is to divide the array into two groups of A & Magi B. if the data in these two groups are orderly, then it is very convenient to sort the two groups of data. How to make the data in these two groups orderly?
Group B can be subdivided into two groups. In turn, when the divided group has only one data, it can be considered that the group has reached order, and then merge the two adjacent groups. In this way, the merge sorting is completed by first recursively decomposing the sequence, and then merging the sequence.
The efficiency of merging and sorting is relatively high. It takes a total of logN steps to divide the sequence into decimal series, each step is a process of merging ordered series, and the time complexity can be recorded as O (N), so the total is O (N*logN). Because merge sorting operates in adjacent data every time, several sorting methods of merge sorting in O (N*logN) (quick sort, merge sort, Hill sort, heap sort) are also more efficient.
# include # include / / merge two ordered sequences a [first.mid] and a [mid + 1...last]. Void MerageArr (int a [], int first, int mid, int last, int temp []) {int I = first, j = mid + 1; int k = 0; 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.