使用扩展集合进行排序

时间:2014-02-27 17:20:40

标签: java sorting

您好我想知道如何使用堆排序算法并使其能够处理整数和浮点数的数组到目前为止我有以下代码:

   private static int[] a; //variable that stores the value of the array
   private static String[] names; //used for the names of the array
   private static int lengtha;// used for determing the size of the array
   private static int left;
   private static int right;
   private static int largest;


   private static void buildheap(int []a){
        lengtha=a.length-1; //gets the size of the array
        for(int i=lengtha/2;i>=0;i--){
            maxheap(a,i);
        }
    }

   private static void maxheap(int[] a, int i){ 
        left=2*i;
        right=2*i+1;
        if(left <= lengtha && a[left] > a[i]){
            largest=left;
        }
        else{
            largest=i;
        }

        if(right <= lengtha && a[right] > a[largest]){
            largest=right;
        }
        if(largest!=i){
            exchange(i,largest);
            maxheap(a, largest);
        }
    }

   private static void exchange(int i, int j){
        int t=a[i];
        String temp = names[i];

        a[i]=a[j];

        a[j]=t; 

        names[i]= names[j];
        names[j]=temp;
        }

    public Heap(int []a0, String[] arr){
        a=a0;
        names=arr;
        buildheap(a);

        for(int i=lengtha;i>0;i--){
            exchange(0, i);
            lengtha=lengtha-1;
            maxheap(a, 0);
        }

0 个答案:

没有答案