Heap-row Java code implementation

Source: Internet
Author: User
Tags array length

Heap review:

Conclusion: The time complexity of the heap algorithm is O (NLGN), and the extra space complexity is O (1).

Familiarize yourself with two structures before you begin the heap sequencing process

1, complete binary tree: If the depth of the two-fork tree is H, in addition to the H layer, the other layers (1~h-1) of the nodes are the maximum number, the H layer all the nodes are continuously concentrated on the leftmost, this is a complete binary tree. (Excerpt from Baidu Encyclopedia)

Plain English: Plainly is the establishment of two fork tree in the process, the two-tree each layer is full of nodes, the last layer of the node is not allowed to have vacancies in the middle;

2, Dagen: Dagen requires that the root node's keyword is greater than or equal to the key value of the left subtree, and is greater than or equal to the key value of the right subtree. (Excerpt from Baidu Encyclopedia)

Plain English: In this heap, the root node of any subtree is the maximum value of this subtree;

Heap sorting core idea: First the unordered array to build a large heap, and then the root node and the last node in the two-tree interchange, the binary tree size--, and then re-establish the large heap, and then the root node and the two-fork tree of the last node interchange, and then size--, so, until the two fork tree size=0, At this point the array is naturally ordered;

The heap sort Java implementation is as follows:

1  PackageCom.cmbc.test1;2 3 Importjava.util.Arrays;4 5  Public classheapsortion {6     7     8      Public Static voidHeapsort (int[] arr) {9         if(arr==NULL|| Arr.length<2){Ten             return; One         } A         //build a large root heap -          for(inti = 0;i<arr.length;i++){ - Heapinsert (arr,i); the         } -         //after the Dagen is established, the size and array length of the binary tree are consistent; -         intSize =arr.length; -Swap (arr,0,--size); +          while(size>0){ -Heapify (arr,0, size); +Swap (arr,0,--size); A         } at     } -      -      Public Static voidHeapinsert (int[] arr,intindex) { -          while(arr[index]>arr[(index-1)/2]){ -Swap (Arr,index, (index-1)/2); -index = (index-1)/2; in         } -     } to      +      Public Static voidSwapint[] arr,intIintj) { -         intTMP =Arr[i]; theArr[i] =Arr[j]; *ARR[J] =tmp; $     }Panax Notoginseng      -      the      Public Static voidPrintArray (int[] arr) { +         if(arr = =NULL) { A             return; the         } +          for(inti = 0; i < arr.length; i++) { -System.out.print (Arr[i] + ""); $         } $ System.out.println (); -     } -      the      Public Static voidHeapify (int[] arr,intIndexintsize) { -         intleft = 2*index+1;Wuyi          while(left<size) { the             intlargest = Left+1<size&&arr[left+1]>arr[left]?left+1: Left; -largest = Arr[largest]>arr[index]?Largest:index; Wu             if(largest==index) { -                  Break; About             } $ swap (arr,largest,index); -index =largest; -left = largest*2+1; -         } A     } +      the      Public Static BooleanIsEqual (int[] arr1,int[] arr2) { -         if(arr1 = =NULL&& ARR2! =NULL) || (Arr1! =NULL&& ARR2 = =NULL)) { $             return false; the         } the         if(arr1 = =NULL&& ARR2 = =NULL) { the             return true; the         } -         if(Arr1.length! =arr2.length) { in             return false; the         } the          for(inti = 0; i < arr1.length; i++) { About             if(Arr1[i]! =Arr2[i]) { the                 return false; the             } the         } +         return true; -     } the     Bayi      Public Static int[] Generaterandomarray (intMaxSize,intmaxValue) { the         int[] arr =New int[(int) ((maxSize + 1) *Math.random ())]; the          for(inti = 0; i < arr.length; i++) { -Arr[i] = (int) ((MaxValue + 1) * Math.random ())-(int) (MaxValue *math.random ()); -         } the         returnarr; the     } the      the      Public Static int[] Copyarray (int[] arr) { -         if(arr = =NULL) { the             return NULL; the         } the         int[] res =New int[arr.length];94          for(inti = 0; i < arr.length; i++) { theRes[i] =Arr[i]; the         } the         returnRes;98     } About      -      Public Static voidMain (string[] args) {101         intTesttime = 500000;102         intmaxSize = 100;103         intMaxValue = 100;104         Booleansucceed =true; the          for(inti = 0; i < testtime; i++) {106             int[] arr1 =Generaterandomarray (maxSize, maxValue);107             int[] arr2 =Copyarray (arr1);108 Heapsort (arr1);109 Arrays.sort (ARR2); the             if(!isequal (arr1, ARR2)) {111succeed =false; the                  Break;113             } the         } theSystem.out.println (succeed? "Right!": "Please check carefully"); the 117         int[] arr =Generaterandomarray (maxSize, maxValue);118 PrintArray (arr);119 Heapsort (arr); - PrintArray (arr);121     }122 123}

Heap-row Java code implementation

Related Article

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.