Heap sorting -- Java, spell more, three fiasco

mb61ab40ce80d9c 2022-01-15 03:09:49 阅读数:561

heap sorting java spell fiasco

Heap sort (Heapsort) It is a sort algorithm designed by using the data structure of heap . Stacking is the structure of a nearly complete binary tree , And at the same time satisfy the nature of accumulation : That is, the key or index of the child is always less than ( Or greater than ) Its parent node . Heap sorting can be said to be a kind of selective sorting by using the concept of heap . There are two ways : Sequencing of large top heap , Descending sort of small top heap .

Code implementation :

import java.util.Arrays;

public class HeapSort {

public int[] sort(int[] sourceArray) throws Exception {

// Yes arr Copy , Do not change the parameter content

int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

int len = arr.length;

buildMaxHeap(arr, len);// Build the big top heap

for (int i = len - 1; i > 0; i--) {

swap(arr, 0, i);// So that the last of the array must be the largest number

len--;

heapify(arr, 0, len);// Refresh large top heap

}

return arr;

}

/**

  • Build the big top heap

  • @param arr Data array

  • @param len The length of the array

*/

private void buildMaxHeap(int[] arr, int len) {

//(int) Math.floor(len / 2) Rounding down , however Math.floor() The type returned is double, So we have to turn it into int

for (int i = (int) Math.floor(len / 2); i >= 0; i--) {

heapify(arr, i, len);

}

}

/**

  • Make the parent node greater than or equal to the left and right child nodes

  • @param arr Array

  • @param i Parent node location

  • @param len The length of the array

*/

private void heapify(int[] arr, int i, int len) {

int left = 2 * i + 1; // Left child node location

int right = 2 * i + 2;// Right child node position

int largest = i;

if (left < len && arr[left] > arr[largest]) {

largest = left;// If the value of the left child node is greater than the value of the parent node position , The replacement largest

}

if (right < len && arr[right] > arr[largest]) {

largest = right;// If the value of the right child node is greater than the value of the parent node position , The replacement largest

}

if (largest != i) {

swap(arr, i, largest);// Switching makes the value of the parent node larger than that of the left and right child nodes .

heapify(arr, largest, len);

}

}

/**

  • Exchange function

*/

private void swap(int[] arr, int i, int j) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

Last

Already said spring cloud alibaba, That's for the whole microservice Architecture , If you want to further improve yourself , What core skills should we master ?

Personally , For the entire microservice Architecture , image RPC、Dubbo、Spring Boot、Spring Cloud Alibaba、Docker、kubernetes、Spring Cloud Netflix、Service Mesh These are the most core knowledge , The only way for architects ! The figure below , It is the self drawn route system outline of microservice architecture , If you have friends who don't know what skills you should master , You can make a reference according to the outline drawn by Xiaobian .

image

If you think the picture is not clear enough , You can also come to Xiaobian to share the original xmind file !

In addition to this micro service system outline , I also organize the strongest learning notes corresponding to the core knowledge points of each topic :

  • Reach the acme of perfection ——Spr

《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing

ingCloudAlibaba.pdf

  • SpringCloud Microservice architecture notes ( One ).pdf

  • SpringCloud Microservice architecture notes ( Two ).pdf

  • SpringCloud Microservice architecture notes ( 3、 ... and ).pdf

  • SpringCloud Microservice architecture notes ( Four ).pdf

  • Dubbo frame RPC Realization principle .pdf

  • Dubbo The latest comprehensive and in-depth interpretation .pdf

  • Spring Boot Study the tutorial .pdf

  • SpringBoo The core book .pdf

  • The first Docker book - Full version .pdf

  • Use SpringCloud and Docker Practical microservice .pdf

  • K8S(kubernetes) Learning Guide .pdf

image

in addition , If you don't know where to start learning , The editor also drew an outline of the corresponding knowledge architecture system for the core knowledge points of each micro service , But it's all exported xmind file , All the source files are here !

image

This article has been  CODING Open source project :【 A big factory Java Analysis of interview questions + Core summary learning notes + The latest explanation video + Actual project source code 】 Included

版权声明:本文为[mb61ab40ce80d9c]所创,转载请带上原文链接,感谢。 https://javamana.com/2021/12/202112122254390174.html