In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to merge and sort C #". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to merge and sort C #" can help you solve the problem.
What is merger? Merge two ordered arrays into a larger ordered array.
What is merge sorting? First, the sorted array is recursively divided into two halves to sort separately, and then the results are merged.
Merge sorting ensures that the time required to sort an array of any size N is proportional to N logN; the disadvantage is that the extra space it requires is proportional to N.
1. The abstraction of merging in place
A straightforward way to achieve merging is to create an array of the appropriate size and then put the elements of the two input arrays into the array from small to large. Because it is merged multiple times, an array is prevented from being created at each merge, and the array is created on the outside of the recursion.
Merging in place can move elements in an array without using extra space, but the implementation is very complex. The following merging method is non-local merging:
Public static void Merge (IComparable [] a, int lo, int mid, int hi) {/ / Console.WriteLine (lo+ "," + mid+ "," + hi); int I = lo; / / left partial index int j = mid+ 1; / / right partial index / / copy an array for (var k = lo) K hi) a [k] = aux [ionization +]; else if (Less (aux [j], auxi]) a [k] = aux [jungle +]; else a [k] = aux [ionization +];}}
The Merge method first sets a [lo. Hi] is copied to aux [], which is the first loop. Then start merging (the second loop), compare the left part with the right part, which side is smaller, put the small value into the array an at once, and put the small index + 1. When one part is taken out, the other part is looped into the array.
two。 Top-down merging sort
The following algorithm uses the above Merge method to achieve top-down merge sorting, this algorithm design uses the divide-and-conquer idea. To a [lo... Hi] sort, first divide it into a [lo... Mid] and a [mid+1... Hi] two parts, respectively, sort them separately through recursive calls, and finally merge the ordered subarrays into the final result.
Public class MergeSort: BaseSort {public static IComparable [] aux = null; public static long usedTimes = 0; public MergeSort () {} public static void Sort (IComparable [] a) {Stopwatch timer = new Stopwatch (); timer.Start (); aux = new IComparable [a.Length]; Sort Timer.Stop (); usedTimes = timer.ElapsedMilliseconds;} / / set the array a [lo... Hi] sort public static void Sort (IComparable [] a, int lo, int hi) {/ / Recursive call Sort (IComparable [] a, int lo, int hi) if (hi)
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.