2017-05-31 124 views
0

我们必须用随机数排序数组。对于这一点,我们需要使用代码(堆排序),这部分使用堆排序对数组进行排序

import java.util.ArrayList; 

public class HeapSort<E extends Comparable<E>> { 
    public ArrayList<E> h; 
    public int SIZE; 
    public int n; 

public HeapSort(int s){ 
    SIZE=s; 
    n=1; 
    h=new ArrayList<>(SIZE); 
    for(int i=0;i<SIZE;i++) 
     h.add(null); 
} 

public void Insert(E e){ 
    h.set(n,e); 
    n++; 
    // swapping up 
    if(n!=2){   // more than 1 element 
     int s=n-1;  // last added element 
     int p=s/2;  // its parent 
     while(s!=1 && LT(s,p)){ 
      swap(p,s); 
      s=p; 
      p=p/2; 
     } 
    } 
} 

public boolean Empty(){ 
    return n==1; 
} 

public E Lesser(){ 
    return h.get(1); 
} 

public void Delete(){ 
    n--; 
    h.set(1,h.get(n)); // last element to the root 
    h.set(n,null); 
    // swapping down 
    int ip=1; 
    int ils=2; 
    int irs=3; 
    while(ConditionSwapDown(ip,ils,irs)){ 
     if(Exists(ils) && Exists(irs)){ 
      // two sons 
      if(LT(ils,ip) || LT(irs,ip)){ 
       // at least one child is lesser than the parent -> swap 
       if(LT(ils,irs)){ 
        swap(ip,ils); // swapping down left 
        ip=ils; 
        ils=ip*2; 
        irs=ils+1; 
       } 
       else{ 
        swap(ip,irs); // swapping down right 
        ip=irs; 
        ils=ip*2; 
        irs=ils+1; 
       } 
      } 
     } 
     else 
      // only left son 
      if(Exists(ils) && !Exists(irs)){ 
       if(LT(ils,ip)){  // left son is lesser than the parent 
        swap(ip,ils); 
        ip=ils; 
        ils=ip*2; 
        irs=ils+1; 
       } 
      } 
      // else no children and end of swapping down 
    } 
} 

public boolean ConditionSwapDown(int p,int l,int r){ 
    // (has 2 children and (parent>left or parent>right)) or 
    // (has 1 children and parent>left) 
    return (((Exists(l) && Exists(r)) && (LT(l,p) || LT(r,p)))) || 
    (Exists(l) && !Exists(r) && LT(l,p)); 
} 

public boolean Exists(int p){ 
    if(p>=SIZE) 
     return false; 
    return h.get(p)!=null; 
} 

public void swap(int a,int b){ 
    E c=h.get(a); 
    h.set(a,h.get(b)); 
    h.set(b,c); 
} 

public boolean LT(int a,int b){  // h[a]<h[b] 
    return h.get(a).compareTo(h.get(b))<0; 
} 

@Override 
public String toString(){ 
    String r=""; 
    for(int i=1;i<n;i++) 
     r+=h.get(i)+" "; 
    return r; 
} 

public String Print(){ 
    return PrintR(1,""); 
} 
public String PrintR(int i,String p){ 
    String r=p+h.get(i)+"\n"; 
    if(h.get(i*2)!=null || h.get(i*2+1)!=null){ 
     if(h.get(i*2)!=null) 
      r+=PrintR(i*2,p+" "); 
     else 
      r+=p+" -\n"; 
     if(h.get(i*2+1)!=null) 
      r+=PrintR(i*2+1,p+" "); 
     else 
      r+=p+" -\n"; 
    } 
    return r; 
} 

我们如何“连接”两个代码?或者使用我们的阵列与HeapSort?对于数组我们已经做到了这一点:

public class P3 { 


public static void main(String[] args) { 

    Scanner reader = new Scanner(System.in); 
    System.out.println("Quina llargada tindrà la taula? "); 
    int n = reader.nextInt(); //n=llargada taula 
    int[] taula = new int[n]; 


    int fi = 100*n; 
    Random rand = new Random(); 
    //int al = rand.nextInt(fi) + 0; 

    for (int i = 0; i < taula.length; i++) 
     taula[i] = rand.nextInt(fi); 

System.out.println(Arrays.toString(taula)); }} 
+0

旁注:你应该坚持Java代码约定,例如方法名称以小写字母开头,以避免方法和类或其构造函数混淆。除此之外,将数组提供给'HeapSort'实例或者读取已排序的数组/列表(或者就地完成)会有什么问题? – Thomas

+0

您是否尝试过使用该类? – syntagma

回答

0

一切都在HeapSort类,等着你去使用它:

int[] taula = {4, 2, 8, 1, 2, 3}; 
    System.out.println(Arrays.toString(taula)); 

    // 'heapSort' variable will store ordered list 
    HeapSort<Integer> heapSort = new HeapSort<>(taula.length+1); 

    for (Integer i : taula) 
     heapSort.Insert(i); 

    System.out.println(heapSort);