排序之堆排序java版

来源:互联网 发布:处理器优化 编辑:程序博客网 时间:2024/06/02 02:08
//数组中第0个结点存留不使用,从第一个结点开始public class HeapSort {    public static void percolate_down(int data[],int r,int n){        int c=2*r;        while(c<=n){            if(c<n&&data[c]<data[c+1]){                c++;            }            if(data[r]<data[c]){                int temp=data[c];                data[c]=data[r];                data[r]=temp;                r=c;                c=2*c;            }else{                break;            }        }    }    public static void create_heap(int data[],int n){        for(int i=n/2;i>=1;i--){            percolate_down(data,i,n);        }    }    public static void heap_sort(int data[],int n){        create_heap(data, n);        for(int i=n;i>=1;i--){            int temp=data[1];            data[1]=data[i];            data[i]=temp;            percolate_down(data, 1, i-1);        }    }    public static void main(String[] args) {        int []data={0,1,2,7,45,8,21,81,-1,0,9,41,98,26,17,67,89,34,23,12,20};        heap_sort(data,20);        for(int i=1;i<=20;i++){        System.out.println(data[i]);        }    }}//0不作为留存 public static void percolate_down(int data[],int r,int n){        int c=2*r+1;        while(c<n){            if(c<n-1&&data[c]<data[c+1]){                c++;            }            if(data[r]<data[c]){                int temp=data[c];                data[c]=data[r];                data[r]=temp;                r=c;                c=2*c+1;            }else{                break;            }        }    }    public static void create_heap(int data[],int n){        for(int i=n/2;i>=0;i--){            percolate_down(data,i,n);        }    }    public static void heap_sort(int data[],int n){        create_heap(data, n);        for(int i=n-1;i>=0;i--){            int temp=data[0];            data[0]=data[i];            data[i]=temp;            percolate_down(data, 0, i);        }    }    public static void main(String[] args) {        int []data={1,2,7,45,8,21,81,-1,0,9,41,98,26,17,67,89,34,23,12,20};        heap_sort(data,20);        for(int i=0;i<20;i++){        System.out.println(data[i]);        }    }//比较好理解的方法 public static void percolate_down(int[] A,int index,int length){         int childLeft;         int temp = A[index];         for( ;index*2+1 < length;index = childLeft){             childLeft = index*2+1;             if(childLeft !=length-1 && A[childLeft] < A[childLeft+1]){                  childLeft++;             }             if(temp > A[childLeft]){                 break;                                }                           else {                 A[index] = A[childLeft];                 index = childLeft;             }                  }          A[index] = temp;    }    public static void createHeap(int[] A,int n){        for(int i=n/2;i>=0;i--){            percolate_down(A,i,n);        }    }    public static int[] heapSort(int[] A, int n) {                createHeap(A,n);        int temp;        for(int i=n-1;i>0;i--){            temp=A[0];            A[0]=A[i];            A[i]=temp;            percolate_down(A,0,i);        }               return A;    }
0 0
原创粉丝点击