各位朋友大家好:
    我在书上及网上看到很多相同的用Java实现各种排序算法的代码,如下所示,但是我的代码不能编译,总是找不到包,org.rut.util.algorithm.SortUtil里需要
import org.rut.util.algorithm.support.BubbleSort;   
import org.rut.util.algorithm.support.HeapSort;   
import org.rut.util.algorithm.support.ImprovedMergeSort;   
import org.rut.util.algorithm.support.ImprovedQuickSort;   
import org.rut.util.algorithm.support.InsertSort;   
import org.rut.util.algorithm.support.MergeSort;   
import org.rut.util.algorithm.support.QuickSort;   
import org.rut.util.algorithm.support.SelectionSort;   
import org.rut.util.algorithm.support.ShellSort;  

但是在各种排序算法里又有:
  import org.rut.util.algorithm.SortUtil;   
请问这不矛盾吗?就是甲类需要导入乙类,而同时乙类又需要导入甲类,我怎么感觉这像个循环啊!我的代码编译不过去,在线急等知道的朋友指点!谢谢!
   以下是代码,红色部分表示我认为循环引用包的import语句!请大家帮助看下!插入排序:
package org.rut.util.algorithm.support;   
  
import org.rut.util.algorithm.SortUtil;   
/**  
 * @author treeroot  
 * @since 2006-2-2  
 * @version 1.0  
 */  
public class InsertSort implements SortUtil.Sort{   
  
    /* (non-Javadoc)  
     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])  
     */  
    public void sort(int[] data) {   
        int temp;   
        for(int i=1;i<data.length;i++){   
            for(int j=i;(j>0)&&(data[j]<data[j-1]);j--){   
                SortUtil.swap(data,j,j-1);   
            }   
        }           
    }   
  
}  } 堆排序:Java代码 
package org.rut.util.algorithm.support;   
  
import org.rut.util.algorithm.SortUtil;   
  
/**  
 * @author treeroot  
 * @since 2006-2-2  
 * @version 1.0  
 */  
public class HeapSort implements SortUtil.Sort{   
  
    /* (non-Javadoc)  
     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])  
     */  
    public void sort(int[] data) {   
        MaxHeap h=new MaxHeap();   
        h.init(data);   
        for(int i=0;i<data.length;i++)   
            h.remove();   
        System.arraycopy(h.queue,1,data,0,data.length);   
    }   
  
     private static class MaxHeap{            
           
        void init(int[] data){   
            this.queue=new int[data.length+1];   
            for(int i=0;i<data.length;i++){   
                queue[++size]=data[i];   
                fixUp(size);   
            }   
        }   
            
        private int size=0;   
  
        private int[] queue;   
                   
        public int get() {   
            return queue[1];   
        }   
  
        public void remove() {   
            SortUtil.swap(queue,1,size--);   
            fixDown(1);   
        }   
        //fixdown   
        private void fixDown(int k) {   
            int j;   
            while ((j = k << 1) <= size) {   
                if (j < size && queue[j]<queue[j+1])   
                    j++;    
                if (queue[k]>queue[j]) //不用交换   
                    break;   
                SortUtil.swap(queue,j,k);   
                k = j;   
            }   
        }   
        private void fixUp(int k) {   
            while (k > 1) {   
                int j = k >> 1;   
                if (queue[j]>queue[k])   
                    break;   
                SortUtil.swap(queue,j,k);   
                k = j;   
            }   
        }   
  
    }   
  
}   
SortUtil:   
  
package org.rut.util.algorithm;   
  
import org.rut.util.algorithm.support.BubbleSort;   
import org.rut.util.algorithm.support.HeapSort;   
import org.rut.util.algorithm.support.ImprovedMergeSort;   
import org.rut.util.algorithm.support.ImprovedQuickSort;   
import org.rut.util.algorithm.support.InsertSort;   
import org.rut.util.algorithm.support.MergeSort;   
import org.rut.util.algorithm.support.QuickSort;   
import org.rut.util.algorithm.support.SelectionSort;   
import org.rut.util.algorithm.support.ShellSort;
   
  
/**  
 * @author treeroot  
 * @since 2006-2-2  
 * @version 1.0  
 */  
public class SortUtil {   
    public final static int INSERT = 1;   
    public final static int BUBBLE = 2;   
    public final static int SELECTION = 3;   
    public final static int SHELL = 4;   
    public final static int QUICK = 5;   
    public final static int IMPROVED_QUICK = 6;   
    public final static int MERGE = 7;   
    public final static int IMPROVED_MERGE = 8;   
    public final static int HEAP = 9;   
  
    public static void sort(int[] data) {   
        sort(data, IMPROVED_QUICK);   
    }   
    private static String[] name={   
            "insert", "bubble", "selection", "shell", "quick", "improved_quick", "merge", "improved_merge", "heap"  
    };   
       
    private static Sort[] impl=new Sort[]{   
            new InsertSort(),   
            new BubbleSort(),   
            new SelectionSort(),   
            new ShellSort(),   
            new QuickSort(),   
            new ImprovedQuickSort(),   
            new MergeSort(),   
            new ImprovedMergeSort(),   
            new HeapSort()   
    };   
  
    public static String toString(int algorithm){   
        return name[algorithm-1];   
    }   
       
    public static void sort(int[] data, int algorithm) {   
        impl[algorithm-1].sort(data);   
    }   
  
    public static interface Sort {   
        public void sort(int[] data);   
    }   
  
    public static void swap(int[] data, int i, int j) {   
        int temp = data[i];   
        data[i] = data[j];   
        data[j] = temp;   
    }   
}  

解决方案 »

  1.   

    import org.rut.util.algorithm.support.InsertSort;
    表示的意思就是后面代码里写的类型InsertSort是org.rut.util.algorithm.support这个包下的InsertSort类
    让程序能认识。
      

  2.   

    谢谢这位朋友的回复,但你说的这个我知道。import的意思就是当前类要用到import后面导入的类。但正如上面所说的:SortUtil类里需要导入InsertSort类,而在InsertSort类中又需要导入SortUtil类,难道这不矛盾吗?这样的话该怎么编译?我看到网上所有的JAVA排序算法都是这个样子的。我编译不过去!
    InsertSort排序算法后面多了两个}。
      

  3.   

    谢谢这位朋友的回复!你说的我明白,import导入的类是需要在当前类中使用到的,我的意思是SortUtil类里需要导入InsertSort类,而同时InsertSort类里又需要导入SortUtil类,难道这不矛盾吗?我觉得像个循环似的,我编译不过去,两个类都编译不过去。请朋友们帮助!
    InsertSort排序算法后面多了两个}号
    我这里只列出了一个InsertSort算法,其他的算法里面也都有import org.rut.util.algorithm.SortUtil;而在SortUtil里也都有ort org.rut.util.algorithm.support.算法名的语句。请问如何编译,如何运行?
    谢谢了。
      

  4.   

    编译和运行是两个过程
    运行的时候只需用已经编译好的class文件,所以没有所谓矛盾,冲突的问题
      

  5.   

    编译A.java时如果需要用到B.java则把B.java拿来一起编译,然后生成A.class和B.class
      

  6.   

    javac *.java
    注意路径!看看行吗?先编译一个SortUtil.Sort接口,注意不要引用到子类,然后编译子类,最后把SortUtil.Sort内容放上去再编译不行的话用eclipse就可以了
      

  7.   

    我觉得首先你要能肯定某些东西你才能正确排除错误
    可以肯定A类调用B类,B类也调用A类,运行class文件是没有问题的
    最好是用Myeclipse等IDE调试起来也方便。
      

  8.   

    我在自己机器上试的都不出错的
    你用的什么IDE,报的错是什么?说出来可能更容易找到原因