Heap sorting instance (Java array implementation) and heap sorting instance java Array

Source: Internet
Author: User

Heap sorting instance (Java array implementation) and heap sorting instance java Array

Heap sorting: using a large heap

The array is all written into the heap, and then inserted back into the array from the back to the back. The array is sorted from small to large.

Public class MaxHeap <T extends Comparable <? Super T> {private T [] data; private int size; private int capacity; public MaxHeap (int capacity) {this. data = (T []) new Comparable [capacity + 1]; size = 0; this. capacity = capacity;} public int size () {return this. size;} public boolean isEmpty () {return size = 0;} public int getCapacity () {return this. capacity;}/*** @ return view the maximum root (only do not delete, compared with popMax) */public T seekMax () {return data [1] ;} Public void swap (int I, int j) {if (I! = J) {T temp = data [I]; data [I] = data [j]; data [j] = temp ;}} public void insert (T item) {size ++; data [size] = item; shiftUp (size) ;}/ *** @ return returns the maximum root (this parameter indicates deletion, compared with seekMax) */public T popMax () {swap (1, size --); shiftDown (1); return data [size + 1];} /*** @ param child node the parent node is child, and the parent node's parent table is child/2 */public void shiftUp (int child) {while (child> 1 & data [child]. compareTo (data [child/2])> 0) {swap (child, child/2); child = child/2 ;}} /*** @ param a cursor mark of an element in the data array * @ param B the cursor mark of an element in the data array * @ return the cursor mark of an element if it is large * /private int max (int, int B) {if (data [a]. compareTo (data [B]) <0) {// If data [B] Big return B; // return B} else {// If data [a] Big return; // return a}/*** @ param a data array. The cursor mark of an element * @ param B data array * @ param c data array the cursor mark * @ return of an element. If the element is large, the cursor mark */private int is returned. Max (int a, int B, int c) {int biggest = max (a, B); biggest = max (biggest, c); return biggest ;} /*** @ param father: the parent node is named father. The parent tables of the left and right child nodes are: father * 2 and father * 2 + 1 */public void shiftDown (int father) {while (true) {int lchild = father * 2; // The left child int rchild = father * 2 + 1; // The right child int newFather = father; // newFather is about to be updated, newFather is the alias if (lchild> size) {// if the father node has neither left nor right Child return;} else if (rchild> size) {// if this father node only has the left child, there is no right child newFather = max (father, lchild );} else {// if this father node has both left and right children newFather = max (father, lchild, rchild);} if (newFather = father) {// It indicates that father is larger than the two subnodes, and the table name is already a large root heap. You do not need to adjust return;} else {// otherwise, you need to continue to adjust the heap, swap (father, newFather) until the big root heap conditions are met; // The value is exchanged for father = newFather; // The value of father is updated, which is equivalent to continuing to adjust shiftDown (newFather )}}} public static <T extends Comparable <? Super T> void sort (T [] arr) {int len = arr. length; // MaxHeap <T> maxHeap = new MaxHeap <T> (len); for (int I = 0; I <len; I ++) {maxHeap. insert (arr [I]);} // output heap for (int I = len-1; I> = 0; I --) {arr [I] = maxHeap. popMax () ;}} public static void printArr (Object [] arr) {for (Object o: arr) {System. out. print (o); System. out. print ("\ t");} System. out. println ();} public static void main (String args []) {Integer [] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6}; printArr (arr); // 3 5 1 7 2 9 8 0 4 6 sort (arr); printArr (arr ); // 0 1 2 3 4 5 6 7 8 9 }}

Heap sorting: Construct a heap for arrays (max heap)

Public class MaxHeap <T extends Comparable <? Super T> {private T [] data; private int size; private int capacity; public MaxHeap (int capacity) {this. capacity = capacity; this. size = 0; this. data = (T []) new Comparable [capacity + 1];} public MaxHeap (T [] arr) {// heapify, number set heap capacity = arr. length; data = (T []) new Comparable [capacity + 1]; System. arraycopy (arr, 0, data, 1, arr. length); size = arr. length; for (int I = size/2; I> = 1; I --) {ShiftDown (I) ;}} public int size () {return this. size;} public int getCapacity () {return this. capacity;} public boolean isEmpty () {return size = 0;} public T seekMax () {return data [1];} public void swap (int I, int j) {if (I! = J) {T temp = data [I]; data [I] = data [j]; data [j] = temp ;}} public void insert (T item) {size ++; data [size] = item; shiftUp (size);} public T popMax () {swap (1, size --); shiftDown (1 ); return data [size + 1];} public void shiftUp (int child) {while (child> 1 & data [child]. compareTo (data [child/2])> 0) {swap (child, child/2); child/= 2 ;}} /*** @ param a data array the cursor mark of an element * @ param B data array * @ Return indicates the maximum value of an element. */private int max (int a, int B) {if (data [a]. compareTo (data [B]) <0) {// If data [B] Big return B; // return B} else {// If data [a] Big return; // return a}/*** @ param a data array. The cursor mark of an element * @ param B data array * @ param c data array * @ return indicates the maximum value of an element. */private int max (int, int B, int c) {int biggest = max (a, B); biggest = max (biggest, c); return biggest;} publ Ic void shiftDown (int father) {while (true) {int lchild = father * 2; int rchild = father * 2 + 1; int newFather = father; // It doesn't matter if the value is assigned here. if you change the return value to break, you must assign the value if (lchild> size) {// if there is no left or right child return ;} else if (rchild> size) {// if there is no right child newFather = max (father, lchild);} else {// if there is left or right child newFather = max (father, lchild, rchild);} if (newFather = father) {// if the original parent node is the maximum of the three nodes, you do not need to refresh the heap return;} el Se {// if the parent node is not the largest, swap the big child up and continue the heap adjustment until swap (newFather, father) is satisfied; father = newFather; // continue shiftDown (newFather ). If newFather is a left child of father, it is equivalent to shiftDown (2 * father) }}public static <T extends Comparable <? Super T> void sort (T [] arr) {int len = arr. length; MaxHeap <T> maxHeap = new MaxHeap <> (arr); for (int I = len-1; I> = 0; I --) {arr [I] = maxHeap. popMax () ;}} public static void printArr (Object [] arr) {for (Object o: arr) {System. out. print (o); System. out. print ("\ t");} System. out. println ();} public static void main (String args []) {Integer [] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6}; printArr (arr); // 3 5 1 7 2 9 8 0 4 6 sort (arr); printArr (arr ); // 0 1 2 3 4 5 6 7 8 9 }}

The above heap sorting instance (Java Array Implementation) is all the content shared by Alibaba Cloud xiaobian. I hope it can be used as a reference and support for the customer's home.

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.