花间一壶酒

导航

java 实现堆排序

package com.wrh.study.dataStructure.heap;
/**
 * The operation of the heap sift up
 * @author wrh
 *
 */
public class Heap {
//    private int[] a;
    //private int size;
    
    /**
     * sift the element up 
     * @param b the heap
     * @param i the index of the sifted element
     */
    public  int[] siftUp(int[] b, int i) {
        boolean done = false;
        if (i == 0) {
            System.out.println("the node is the root element");
        } 
         while ((i != 0) && (done != true)){
            if (b[i] > b[((i + 1) / 2) - 1]) {
                int temp = b[i];
                b[i] = b[(i + 1) / 2 - 1];
                b[(i + 1) / 2 - 1] = temp;
                i = (i + 1) / 2 - 1;
            } else {
                done = true;
            }
        }
         return b;
    }
    
    /**
     * sift the element down
     * @param b
     * @param i
     */
    public int[] siftDown(int[] b, int i) {
        boolean done = false;
        if ((2* i + 1) > (b.length - 1)) {
            System.out.println("i is the leaf node");
        }
        
        while ((2 * i + 1) < (b.length) && (done != true)) {
         i = 2 * i + 1;
         //find the bigger brother index
         if ((i + 1 < b.length) && (b[i + 1] > b[i])) {
             i = i + 1;
         }
         if (b[(i + 1) / 2 - 1] < b[i]) {
             int temp = b[(i + 1) / 2 - 1];
             b[(i + 1) / 2 - 1] = b[i];
             b[i] = temp;
         } else {
             done = true;
         }
        }
        return b;
    }
    
    /**
     * insert the elememt to the heap b
     * @param b
     * @param elememt
     * @param number insert number
     */
    public  int[] insert(int[] b, int elememt, int number) {
        b = resizeArray(b, number, true);
        System.out.println("b length: " + b.length);
        b[b.length - 1] = elememt;
        b = siftUp(b, b.length - 1);
        
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }
        return b;
    }
    
    /**
     * delete the element whose index is i
     * 
     * use b[b.length -1] to replace i and then adjust the heap
     * @param b the heap
     * @param i the element index of the deleted
     */
    public int[] delete(int[] b, int i) {
        if (i == b.length - 1) {
            b = resizeArray(b,1, false);
        } else if (i == 0){    //if deleted one is the root element 
            b[i] = b[b.length - 1];
            b = resizeArray(b,1, false);
            b = siftDown(b, i);
        } else {
            b[i] = b[b.length - 1];
            b = resizeArray(b,1, false);
            //if b[i] > its root element ,then sift up
            if (b[i] > b[(i + 1)/ 2 - 1]) {
                b = siftUp(b, i);
            } else {
                b = siftDown(b, i);
            }
        }
        return b;
        
    }
    
    /**
     * delete the max element in the heap
     * if the heap is the bigger then delete the root
     * 
     * @param b
     * @return the new heap
     */
    public int[] deleteMax(int[] b) {
        b = delete(b,0);
        return b;
    }
    
    /**
     * make the heap from the int[] b
     * 
     * @param b
     * @return
     */
    public int[] makeHeap(int[] b) {
        int startIndex = b.length / 2 - 1;
        for (int i = startIndex; i > -1; i--) {
            b = siftDown(b, i);
        }
        return b;
    }
    
    public int[] heapSort(int[] b) {
        b = makeHeap(b);
        int end = b.length;
        int[] a = new int[b.length];
        for (int i = 0; i < end; i++) {
            System.out.println(b[0]);
            a[i] = b[0];
            b = delete(b, 0);
        }
        return a;
    }
     
    /**
     * resize the b into b.length + number
     * @param b
     * @param is add to identify to add the array or cut down the array
     * @param number
     */
    public int[] resizeArray(int[] b, int number, boolean isAdd) {
        int resize = 0;
        int[] seqList_upp;
        if (isAdd) {
        resize = b.length + number;
        seqList_upp = new int[resize];
        
        System.arraycopy(b,0,seqList_upp,0,b.length);
        } else {
            resize = b.length - number;
            seqList_upp = new int[resize];
            
            System.arraycopy(b,0,seqList_upp,0,b.length - number);
        }
        return seqList_upp;
        
    }
    
    public static void main(String[] args) {
        int[] a = {20,17,9,10,11,4,5,3,7,5};
        Heap s = new Heap();
        
        s.siftUp(a, 9);
        int[] ni = s.delete(a, 0);
        System.out.println(ni.length);
        System.out.println("_________________");
        
        int[] b = {4,3,8,10,11,13,7,30,17,26};
        b = s.heapSort(b);
        /*for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }*/
        
    }
}

posted on 2012-04-16 16:03  wrh526  阅读(253)  评论(0编辑  收藏  举报