Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize heap sorting of Java sorting algorithm

Shulou Source: shulou.com Published: 2022-06-02 11:34:51 09月29日 Update

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!

Tags: Node sort element adjustment article subtree output algorithm end node complex minimum large small to large size small code value interest leaf Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi macOS vpn Xiaomi