In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
C# sorting algorithm in the heap sort is what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Code:
/ / heap sort method. / / the array to be sorted. / / private void Heapsort (int [] a) {HeapSort_BuildMaxHeap (a); / / build a large root heap. Console.WriteLine ("Build max heap:"); foreach (int i in a) {Console.Write (I + "); / / print the large root heap. } Console.WriteLine ("\ r\ nMax heap in each iteration:"); for (int I = a.Length-1; I > 0; iMub -) {HeapSort_Swap (ref a [0], ref a [I]); / / swaps the top element of the heap with the last element of the disordered area. HeapSort_MaxHeaping (a, 0, I); / / adjust the new disorder area to the big root heap. / / print the large root heap after each iteration of heap sorting. For (int j = 0; j
< i; j++) { Console.Write(a[j] + " "); } Console.WriteLine(string.Empty); } } /// /// 由底向上建堆。由完全二叉树的性质可知,叶子结点是从index=a.Length/2开始,所以从index=(a.Length/2)-1结点开始由底向上进行大根堆的调整。 /// /// /// 待排序数组。 /// private static void HeapSort_BuildMaxHeap(int[] a) { for (int i = (a.Length / 2) - 1; i >= 0; iMel -) {HeapSort_MaxHeaping (a, I, a.Length);}} / adjusts the specified node to heap. / / the array to be sorted. The node that needs to be adjusted. The size of the heap is also the length of the disorder region in the index group. / / private static void HeapSort_MaxHeaping (int [] a, int I, int heapSize) {int left = (2 * I) + 1; / / left child node. Int right = 2 * (I + 1); / / right child node. Int large = I; / / temporary variable, which holds large node values. / / compare the left child node. If (left
< heapSize && a[left] >A [large]) {large = left;} / / compare the right child node. If (right
< heapSize && a[right] >A [large]) {large = right;} / / swap if the child node is larger than itself, moving the large element up, and adjust the large element to the heap to ensure the nature of the heap. If (I! = large) {HeapSort_Swap (ref a [I], ref a [large]); HeapSort_MaxHeaping (a, large, heapSize);}} / exchange the values of two integers. / / the integer a. / / Integer b. Private static void HeapSort_Swap (ref int a, ref int b) {int tmp = a; a = b; b = tmp;}
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.