Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle and function of java heap sorting algorithm

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the principle and function of java heap sorting algorithm". In daily operation, I believe that many people have doubts about the principle and function of java heap sorting algorithm. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the principle and function of java heap sorting algorithm". Next, please follow the editor to study!

1. Definition

From the following figure (found on the Internet), the heap sort is similar, putting the largest number in the lowest and rightmost position, and then the second largest number in the penultimate position on the bottom right, and so on:

2. Code public class TestController {public static void main (String [] args) {int [] nums = {16Power7, 3d3, 20pr 17, 8}; headSort (nums); System.out.println ("result:" + Arrays.toString (nums) } / * heap sort * / public static void headSort (int [] list) {/ / construct the initial heap, starting with the first non-leaf node, and swapping the larger left and right child nodes to the parent node for (int I = (list.length) / 2-1; I > = 0; iMuk -) {headAdjust (list, list.length, I) } / / sort, put the largest node at the end of the heap, and then re-adjust the for from the root node (int I = list.length-1; I > = 1; Imuri -) {int temp = list [0]; list [0] = list [I]; list [I] = temp; headAdjust (list, I, 0) }} private static void headAdjust (int [] list, int len, int I) {int k = I, temp = list [I], index = 2 * k + 1; while (index

< len) { if (index + 1 < len) { if (list[index] < list[index + 1]) { index = index + 1; } } if (list[index] >

Temp) {list [k] = list [index]; k = index; index = 2 * k + 1;} else {break;}} list [k] = temp }} 3, the result is: [3,7,8,16,17,20] so far, the study on "the principle and function of java heap sorting algorithm" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report