public class DateSort {
public static void main(String args[]) {
String args2[] = { "1", "9", "6", "5", "3", "4", "2", "7", "8" };
int a[] = new int[args2.length];
for (int i = 0; i <args2.length-1; i++) {
a[i] = Integer.parseInt(args2[i]);
}
buddlesort(a);
print(a);
}
private static void print(int a[]) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "  ");
}
} private static void buddlesort(int a[]) {
try{
for (int j = 0; j < a.length; j++) {
for (int i = 0; i < a.length - j; i++) {
if (a[i] > a[i + 1]) {
int temp;
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array index out-of-bound: " + e);
}
}
}
心血来潮编写了这样一个冒泡法排序的程序,可是显示有异常,排序只能排一次执行一次,请教各位有何高招!同样对于这样的问题有什么敲门没有!谢谢了!

解决方案 »

  1.   


    public class DateSort {
        public static void main(String args[]) {
    String args2[] = { "1", "9", "6", "5", "3", "4", "2", "7", "8" };
    int a[] = new int[args2.length];
    for (int i = 0; i < args2.length; i++) {//注意上界
        a[i] = Integer.parseInt(args2[i]);
    }
    System.out.println("原始数据 = " + Arrays.toString(a));
    buddlesort(a);
    System.out.println("现在数据 = " + Arrays.toString(a));
    print(a);
        }    private static void print(int a[]) {
    for (int i = 0; i < a.length; i++) {
        System.out.print(a[i] + " ");
    }
        }    private static void buddlesort(int a[]) {
    try {
        for (int j = 0; j < a.length; j++) {
    for (int i = 0; i < a.length - j -1; i++) {//注意上界
        if (a[i] > a[i + 1]) {
    int temp = a[i];
    a[i] = a[i + 1];
    a[i + 1] = temp;
        }
    }
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("Array index out-of-bound: " + e);
    }
        }
    }
      

  2.   

    这样也行 看起来舒服点public class DateSort {
        public static void main(String args[]) {
    String sourceString = "196542378";
    int a[] = new int[sourceString.length()];
    for (int i = 0; i < sourceString.length(); i++)
        a[i] = sourceString.charAt(i) - '0';
    System.out.println("原始数据 = " + Arrays.toString(a));
    buddlesort(a);
    System.out.println("现在数据 = " + Arrays.toString(a));
        }    private static void buddlesort(int a[]) {
    for (int j = 0; j < a.length; j++)
        for (int i = a.length - 1; i > j; i--)
    if (a[i] < a[i - 1]) {
        int temp = a[i];
        a[i] = a[i - 1];
        a[i - 1] = temp;
    }    }
    }
      

  3.   

    还有 你的函数名英文写错了 应该是bubbleSort 而不是buddleSort 
      

  4.   

    下面四种都是冒泡排序 楼主你仔细对照一下public class BubbleSort {
        public static void main(String args[]) {
    String sourceString = "196542378";
    int a[] = new int[sourceString.length()];
    for (int i = 0; i < sourceString.length(); i++)
        a[i] = sourceString.charAt(i) - '0';
    System.out.println("原始数据 = " + Arrays.toString(a));
    bubbleSortOfBefore1(a);
    System.out.println("现在数据 = " + Arrays.toString(a));
        }    private static void bubbleSortOfBefore1(int a[]) {//先确定前面的
    for (int j = 0; j <= a.length-1; j++)//从前向后遍历
        for (int i = a.length - 1; i >= j+1; i--)//从len-1开始
    if (a[i] < a[i - 1]) {
        int temp = a[i];
        a[i] = a[i - 1];
        a[i - 1] = temp;
    }
        }
        
        private static void bubbleSortOfBefore2(int a[]) {//先确定前面的
    for (int j = a.length-1; j>=0; j--)//从后向前遍历
        for (int i = a.length - 1; i >= a.length  - j; i--)//从len-1开始
    if (a[i] < a[i - 1]) {
        int temp = a[i];
        a[i] = a[i - 1];
        a[i - 1] = temp;
    }
        }
        
        private static void bubbleSortOfAfter1(int a[]) {//先确定后面的
    for (int j = 0; j <= a.length - 1; j++)//从前向后遍历
        for (int i = 0; i <= a.length - j - 2; i++)//从0开始
    if (a[i] > a[i + 1]) {
        int temp = a[i];
        a[i] = a[i + 1];
        a[i + 1] = temp;
    }
        }
        
        private static void bubbleSortOfAfter2(int a[]) {//先确定后面的
    for (int j = a.length - 1 ; j >= 0; j--)//从后向前遍历
        for (int i = 0; i <= j - 1; i++)//从0开始
    if (a[i] > a[i + 1]) {
        int temp = a[i];
        a[i] = a[i + 1];
        a[i + 1] = temp;
    }
        }
     }