In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the Java sorting algorithm heap sorting how to achieve, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
A binary heap is a complete binary tree or a nearly complete binary tree.
The binary heap satisfies two characteristics:
1. The key value of the parent node is always greater than or equal to (less than or equal to) the key value of any child node.
2. The left and right subtrees of each node are a binary heap (the largest or smallest heap).
The value of any node is greater than the value of its child node-large top heap (the final output is from small to large row)
The value of any node is less than the value of its child node-small top heap (final output from big to small row)
Heap sort step
1. Stacking, reverse adjustment so that each subtree is a large-top or small-top heap (heap)
two。 Output elements in order ∶ swaps the heap top with the last element, and then adjusts the heap top element (sort)
The heap sorting code implements (large top heap) public class HeapSort {private static void heapSort (int [] arr) {/ / constructs the initial heap (big top heap), starting with the first non-leaf node, and swapping the larger left and right child nodes to the parent node for (int I = arr.length / 2-1; I > = 0; imurf -) {heapAdjust (arr, I, arr.length) } / / adjust the heap structure, swap the top element with the end element for (int j = arr.length-1; j > 0; j color -) {swap (arr, 0, j); / / swap the top element with the end element heapAdjust (arr, 0, j) / / re-adjust the heap} private static void swap (int [] arr, int a, int b) {int temp = arr [a]; arr [a] = arr [b]; arr [b] = temp;} / / adjust large top heap private static void heapAdjust (int [] arr, int I, int len) {int temp = arr [I], index = 2 * I + 1 While (index
< len) { if (index + 1 < len && arr[index] < arr[index + 1]) {// 如果左子结点小于右子结点,index指向右子结点 index += 1; } if (arr[index] >Temp) {/ / if the child node is larger than the parent node, assign the child node value to the parent node arr [I] = arr [index]; I = index; index = 2 * I + 1;} else {break;}} arr [I] = temp } public static void main (String [] args) {int [] arr = {1 arr 28 arr 3 21, 11 11, 7 6 18}; heapSort (arr); System.out.println (Arrays.toString (arr));}}
Time complexity: O (nlogN)
Thank you for reading this article carefully. I hope the article "how to realize the heap sorting of Java sorting algorithm" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.