插入排序调试

时间:2014-02-18 03:18:54

标签: java debugging sorting

我尝试使用插入排序对给定的数据集进行排序。但是当我尝试打印输出时没有打印,正确的输出是:

Albatross 1
mockingbird 2
vultures 3
redwoodpeckers 6
pigeons 7
crows 10
condos 12
bluejays 15
dodos 15
baldeagles 25
cardinals 40
hummung birds 88

String[]bn={"Cardinals","BlueJays","Albatross","Vultures","Crows","Mockingbirds","Condors","BaldEagles","Pigeons","RedHeadWoodPecker","Hummingbirds","Dodos"};
int[]bq={40,15,1,3,10,2,12,25,7,6,88,15};   
public static void SortNumericalOrdernsert (String[] bn,int[] bq){
    for(int i=1;i<bq.length;i++){
        int next=bq[i];
        String y=bn[i];
        //find all the insertion location 
        //Move all the larger elements up
        int j=i;
        while(j>0 && bq[j-1]>next){
            bn[j]=bn[j-1];
            bq[j]=bq[j-1];
               j--;
        }
        //insert the element
        bq[j]=next;
        bn[j]=y;
    }

有人可以帮助

3 个答案:

答案 0 :(得分:0)

我复制并粘贴了你的代码并且它正常运行我相信问题在于打印方法。请提供,以便我们找出问题。

我已经把你的运行代码也放了,它没有任何问题。我相信你应该提供全班,这样我们才能确定问题所在。这就是我添加代码的方式,并使输出正常运行。

public class Sort {

    static String[]bn={"Cardinals","BlueJays","Albatross","Vultures","Crows","Mockingbirds","Condors","BaldEagles","Pigeons","RedHeadWoodPecker","Hummingbirds","Dodos"};
    static int[]bq={40,15,1,3,10,2,12,25,7,6,88,15};

    public static void SortNumericalOrdernsert (String[] bn,int[] bq) {
         for(int i=1;i<bq.length;i++){
             int next=bq[i];
             String y=bn[i];
            //find all the insertion location 
            //Move all the larger elements up
            int j=i;
            while(j>0 && bq[j-1]>next){
               bn[j]=bn[j-1];
               bq[j]=bq[j-1];
               j--;
            }
            //insert the element
            bq[j]=next;
            bn[j]=y;
        }
    }

    public static void ShowAllBirds(String[]bn,int[]bq) { 
        for(int a=0;a<=bn.length-1;a++) { 
            System.out.println(bn[a]+" : "+bq[a]);
        }
    }
    public static void main(String[] args) {
        SortNumericalOrdernsert(bn, bq);
        ShowAllBirds(bn, bq);
   }
}

答案 1 :(得分:0)

您需要添加一个循环来遍历并打印数组的各个元素:

 for(String name: bn){ // this needs to be included at the end of your sort method AFTER the for loop
     System.out.println(name);
 }

建议; Java是按值传递的,而不是通过引用传递,如果您打算之后使用已排序的数组,则必须返回带有修改值的数组。

注意到你需要名字和价值;将无法使用增强的for循环。你需要按照以下方式去做:

 for(int index = 0; index < bn.length; index++){
     System.out.println(bn[index] + " " + bq[index]);
 }

有关for循环和数组的更多信息,请参阅Java教程和文档(仅限Google)。

答案 2 :(得分:0)

它工作,从数组b和bs作为静态

public class test {

    /**
     * @param args
     */
    static String[]bn={"Cardinals","BlueJays","Albatross","Vultures","Crows","Mockingbirds","Condors","BaldEagles","Pigeons","RedHeadWoodPecker","Hummingbirds","Dodos"};
    static int[]bq={40,15,1,3,10,2,12,25,7,6,88,15};   
    public static void SortNumericalOrdernsert (String[] bn,int[] bq){
        for(int i=1;i<bq.length;i++){
            int next=bq[i];
            String y=bn[i];
            //find all the insertion location 
            //Move all the larger elements up
            int j=i;
            while(j>0 && bq[j-1]>next){
                bn[j]=bn[j-1];
                bq[j]=bq[j-1];
                   j--;
            }
            //insert the element
            bq[j]=next;
            bn[j]=y;
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SortNumericalOrdernsert(bn,bq);
        for(int i = 0; i< bn.length; i++)
        {
            System.out.println(bn[i] + "\t" + bq[i]);
        }
    }
}