In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 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.
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.