In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to program heap sorting technology with Java". 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!
Basic introduction to heap sorting
Heap sorting is a sort algorithm designed by using the data structure of heap. Heap sorting is a selective sort, its best, worst and average time complexity are all O (nlogn), it is unstable sorting.
Heap is a complete binary tree with the following properties: the value of each node is greater than or equal to the value of its left and right child nodes, which is called large top heap. Note: there is no requirement that the most child nodes are worth the size relationship.
The value of each node is less than or equal to the value of the left and right child nodes, which is called small top heap.
The characteristics of large top heap: arr [I] > = arr [2i+1] & & arr [I] > = arr [2i+2], I corresponds to the node, I starts with the number 0.
Characteristics of small top reactor: arr [I] 0; j muri -) {/ / exchange temp = arr [j]; arr [j] = arr [0]; arr [0] = temp; adjustHeap (arr, 0, j) }} / * * put an array (binary tree) Adjust to a large top heap * function: complete the adjustment of the non-leaf node tree corresponding to I into a large top heap * * @ param arr array to be adjusted * @ param I represents the index of the non-leaf node in the array * @ param length indicates how many elements to adjust, length is gradually decreasing * / public static void adjustHeap (int [] arr, int I Int length) {/ / fetch the value of the current element first Saved in the temporary variable int temp = arr [I] / / k = 2 * I + 1 is the left child of node I for (int k = 2 * I + 1; k
< length; k = k * 2 + 1) { //当左子节点值小于右子节点值 if (k + 1 < length && arr[k] < arr[k + 1]) { k++;//k指向右子节点 } //如果子节点值大于父节点值 if (arr[k] >Temp) {/ / assign a larger value to the current node arr [I] = arr [k]; / /!! I point to k to continue loop comparison I = k;} else {break;}} / / when the for loop ends, we have put the maximum value of the tree with I as the parent node at the top. / / put the temp value into the adjusted position arr [I] = temp;}} "how to program heap sorting technology with Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.