Java sorting Algorithm (v): Heap Ordering

Source: Internet
Author: User

[Algorithm description]

Heap sorting is an improvement on simple selection sorting

Simple selection sorting is to find a minimum record from N records, which needs to be compared n-1 times. However, this operation did not save the comparison of each trip, in the comparison of the next trip, there are many comparisons have been done in the previous trip, but because the previous trip did not save these comparison results, so the next trip sort and then repeated the execution of these comparison operations, so the record more times.

A 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, called the Big Top heap, or the value of each node is less than or equal to the value of its left and right child nodes, called the small top heap.



[Algorithmic thinking]

Constructs the sequence to be sorted into a large top heap. At this point, the maximum value of the entire sequence is the root node of the heap top. Remove it (actually swap it with the end element of the heap array, at which point the element at the end is the maximum), and then re-construct the remaining n-1 sequence into a heap, resulting in a secondary maximum of n elements. With this repeated execution, an ordered sequence can be obtained.

[Java Implementation]

public class Heapsort {public static void main (string[] args) {int[] arr = {6, 5, 3, 1, 8, 7, 2, 4}; System.out.println ("Before sorting:"); for (int i = 0; i < arr.length; i++) {System.out.print (Arr[i] + "");} Heap sort heapsort (arr); System.out.println (); System.out.println ("After sorting:"); for (int i = 0; i < arr.length; i++) {System.out.print (Arr[i] + "");}} /** * Heap sort */private static void Heapsort (int[] arr) {for (int i = ARR.LENGTH/2; I >= 0; i--) {//to build the sequence to be sorted into a large top heap Adjust (arr, I, arr.length); }for (int i = arr.length-1; i > 0; i--) {//gradually swap the root node of each maximum value with the end element and then resize it to become a large top heap swap (arr, 0, I);//The last record of the heap top records and the current unsorted subsequence Record Exchange Heapadjust (arr, 0, i);}} private static void Heapadjust (int[] arr, int i, int n) {int Child;int tmp;for (tmp = arr[i]; leftchild (i) < n; i = Chi LD) {child = Leftchild (i), if (Child! = n-1 && Arr[child] < Arr[child + 1]) {child++;} if (TMP < Arr[child]) {arr[i] = Arr[child];} else {break;}} Arr[i] = tmp;} private static int leftchild (int i) {return 2 * i + 1;} Private static void Swap (int[] arr, int index1, int index2) {int tmp = ARR[INDEX1];ARR[INDEX1] = arr[index2];arr[index2] = tmp ;}}
[Algorithm summary]

Heap sequencing Time complexity: O (NLOGN)

Heap sorting is not sensitive to the sequencing state of the original records, which is far better in performance than bubbling, simple selection, direct insertion and sorting.

Java sorting Algorithm (v): Heap Ordering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.