日期:2014-05-20  浏览次数:20798 次

冒泡问题
package com.wang;

/**
 * 演示冒泡降序排列
 * @author 
 *
 */
public class TestMaopao {
public static void main(String[] args) {
int array [] = {88, 52, 9, 100, 98, 65};
// 冒泡排序
System.out.println("************************升序排列**************************");
for (int i = 0; i < array.length; i ++) {
for (int j = 0; j < array.length - 1 - i; j ++) {
// 升序排列
if (array [i] > array [i + 1]) {
int temp = array [i];
array [i] = array [i + 1];
array [i + 1] = temp;
}
}
}
// 排序后输出数组
for (int i = 0; i < array.length; i ++) {
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println();
System.out.println("************************降序排列**************************");
for (int i = 0; i < array.length; i ++) {
for (int j = 0; j < array.length - 1 - i; j ++) {
// 降序排列
if (array [i] < array [i + 1]) {
int temp = array [i];
array [i] = array [i + 1];
array [i + 1] = temp;
}
}
}
// 排序后输出数组
for (int i = 0; i < array.length; i ++) {
System.out.print(array[i] + " ");
}
}

}


------解决方案--------------------
for (int i = 0; i < array.length; ++i) {
for (int j = 0; j < array.length - i - 1; ++j) {
// 升序排列
if (array [j] > array [j + 1]) {
temp = array [j];
array [j] = array [j + 1];
array [j + 1] = temp;
}
}
}
------解决方案--------------------
Java code

public static void main(String[] args) {
        int array [] = {88, 52, 9, 100, 98, 65};
        // 冒泡排序
        System.out.println("************************升序排列**************************");
        for (int i = 0; i < array.length; ++i) {
            for (int j = 0; j < array.length - 1 - i; ++j) {
            // 升序排列
                if (array [j] > array [j + 1]) {
                    int temp = array [j];//循环里面的条件应该是判断J,而不是I,降序也是如此
                    array [j] = array [j + 1];
                    array [j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < array.length; ++i) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
        System.out.println();
        System.out.println("************************降序排列**************************");
        for (int i = 0; i < array.length; i ++) {
            for (int j = 0; j < array.length - 1 - i; j ++) {
            // 降序排列
                if (array [j] < array [j + 1]) {
                int temp = array [j];
                array [j] = array [j + 1];
                array [j + 1] = temp;
                }
            }
        }
        // 排序后输出数组
        for (int i = 0; i < array.length; i ++) {
        System.out.print(array[i] + " ");
        }
    }

------解决方案--------------------
Java code

public static void main(String[] args) {
        int array [] = {88, 52, 9, 100, 98, 65};
        // 冒泡排序
        System.out.println("************************升序排列**************************");
        for (int i = 0; i < array.length; ++i) {
            for (int j = 0; j < array.length - 1 - i; ++j) {
            // 升序排列
                if (array [j] > array [j + 1]) {
                    int temp = array [j];
                    array [j] = array [j + 1];
                    array [j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < array.length; ++i) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
        System.out.println();
        System.out.println("************************降序排列**************************");
        for (int i = 0; i < array.length; i ++) {
            for (int j = 0; j < array.length - 1 - i; j ++) {
            // 降序排列
                if (array [j] < array [j + 1]) {
                int temp = array [j];
                array [j] = array [j + 1];
                array [j + 1] = temp;
                }
            }
        }
        // 排序后输出数组
        for (int i = 0; i < array.length; i ++) {
        System.out.print(array[i] + " ");
        }
    }