@@ -0,0 +1,1886 @@
+package org.speedfirst.leetcode.heap;%0A%0A/**%0A * Created by jiankuan on 1/21/15.%0A */%0Apublic class Heapify %7B%0A%0A    /**%0A     * @param A: Given an integer array%0A     * @return: void%0A     */%0A    public void heapify(int%5B%5D A) %7B%0A        int end = 0;%0A        while (end %3C A.length) %7B%0A            int pos = end;%0A            int parentPos = (pos - 1) / 2;%0A            while (pos %3E 0) %7B%0A                if (parentPos %3C 0) %7B%0A                    break;%0A                %7D%0A%0A                if (A%5BparentPos%5D %3C= A%5Bpos%5D) %7B%0A                    break;%0A                %7D%0A%0A                swap(A, parentPos, pos);%0A                pos = parentPos;%0A                parentPos = (pos - 1) / 2;%0A            %7D%0A%0A            end++;%0A        %7D%0A    %7D%0A%0A    private void swap(int%5B%5D A, int i, int j) %7B%0A        int tmp = A%5Bi%5D;%0A        A%5Bi%5D = A%5Bj%5D;%0A        A%5Bj%5D = tmp;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        int%5B%5D A = %7B42,30,27,93,8,34,47,64,82,76,70,79,23,5,67,9,97,29,7,61,73,3,44,85,51,49,90,59,38,55,39,62,54,81,26,50,22,71,52,41,77,32,2,96,84,20,48,17,87,94,12,13,89,24,6,74,69,78,65,35,15,28,25,16,33,63%7D;%0A        Heapify heapify = new Heapify();%0A        heapify.heapify(A);%0A%0A        for (int i = 0; i %3C A.length / 2; i++) %7B%0A            int left = i * 2 + 1;%0A            int right = i * 2 + 2;%0A            if (left %3C A.length && A%5Bi%5D %3E= A%5Bleft%5D) %7B%0A                System.out.println(%22invalid heap pos %22 + i + %22:%22 + A%5Bi%5D + %22, left %22 + left + %22:%22 + A%5Bleft%5D);%0A            %7D%0A            if (right %3C A.length && A%5Bi%5D %3E= A%5Bright%5D) %7B%0A                System.out.println(%22invalid heap pos %22 + i + %22:%22 + A%5Bi%5D + %22, right %22 + right + %22:%22 + A%5Bright%5D);%0A            %7D%0A        %7D%0A%0A        for (int i = 0; i %3C A.length; i++) %7B%0A            System.out.printf(%22%25d%5Ct%22, i);%0A        %7D%0A%0A        System.out.println();%0A%0A        for (int i = 0; i %3C A.length; i++) %7B%0A            System.out.printf(%22%25d%5Ct%22, A%5Bi%5D);%0A        %7D%0A    %7D%0A%7D%0A

add heapify solution