BVB Source Codes

Algorithm-Implementations Show Heap.java Source code

Return Download Algorithm-Implementations: download Heap.java Source code - Download Algorithm-Implementations Source code - Type:.java
  1. /*
  2.  * Written by: Rafael A. Rivera Soto
  3.  * Last Updated: January 2, 2014
  4.  *
  5.  * This class only contains the necessary methods needed to build a maxHeap and sort
  6.  * our array. It is in no way representative of a real heap implementation.
  7.  */
  8. public class Heap {
  9.         int [] array;
  10.         int size;
  11.        
  12.         public Heap(int[] array) {
  13.                 this.array = array;
  14.                 this.size = array.length;
  15.                 buildMaxHeap();
  16.         }
  17.        
  18.         public int leftIndex(int i){
  19.                 return 2*i + 1;
  20.         }
  21.        
  22.         public int rightIndex(int i){
  23.                 return 2*i + 2;
  24.         }
  25.        
  26.         public void swap(int index1, int index2){
  27.                 int temp = array[index1];
  28.                 array[index1] = array[index2];
  29.                 array[index2] = temp;
  30.         }
  31.        
  32.         public void maxHeapify(int i){
  33.                 int l = leftIndex(i);
  34.                 int r = rightIndex(i);
  35.                 int largest;
  36.                
  37.                 if(l < size && array[l] > array[i]){
  38.                         largest = l;
  39.                 }else{
  40.                         largest = i;
  41.                 }
  42.                
  43.                 if(r < size && array[r] > array[largest]){
  44.                         largest = r;
  45.                 }
  46.                
  47.                 if(largest != i){
  48.                         swap(i,largest);
  49.                         maxHeapify(largest);
  50.                 }
  51.         }
  52.        
  53.         public void buildMaxHeap(){
  54.                 for(int i = size/2 - 1; i >= 0; i--){
  55.                         maxHeapify(i);
  56.                 }
  57.         }
  58.        
  59.         public int[] getArray(){
  60.                 return array;
  61.         }
  62. }
  63.  
downloadHeap.java Source code - Download Algorithm-Implementations Source code
Related Source Codes/Software:
raty - 2017-04-22
RDVTabBarController - Highly customizable tabBar and tabBarController fo... 2017-04-22
material-icon-lib - Library containing over 1500 material vector icons... 2017-04-21
httpdiff - Perform the same request against two HTTP servers ... 2017-04-21
jquerytools - The missing UI library for the Web ... 2017-04-21
mcrouter - Mcrouter is a memcached protocol router for scalin... 2017-04-22
dynomite - A generic dynamo implementation for different k-v ... 2017-04-22
kityminder - Baidu brain figure 2017-04-22
llvm - Mirror of official llvm git repository located at ... 2017-04-22
RBBAnimation - Block-based animations made easy, comes with easin... 2017-04-22
ied - 2017-04-29
Nimble - A Matcher Framework for Swift and Objective-C 2017-04-29
MHVideoPhotoGallery - A Photo and Video Gallery 2017-04-29
shoulda-matchers - Collection of testing matchers extracted from Shou... 2017-04-29
Android-SlideExpandableListView - A better ExpandableListView, with animated expanda... 2017-04-29
AppSales-Mobile - App Sales allows iPhone and Mac App Store develope... 2017-04-29
react-templates - Light weight templates for react ... 2017-04-28
afterglow-theme - A minimal dark Theme for Sublime Text 2 and 3 2017-04-28
jwt-go - Golang implementation of JSON Web Tokens (JWT) 2017-04-28
DeerResume - Tool MarkDown online resume, online preview, edit,... 2017-04-28

 Back to top