冒泡排序
2012-05-16 10:16:47 来源:WEB开发网核心提示:public class BubbleSort { /** 冒泡排序*/ public static void bubbleSort(int[] list) { for (int k = 1; k < list.length ; k++) { // 排序 for (int i
public class BubbleSort { /** 冒泡排序*/ public static void bubbleSort(int[] list) { for (int k = 1; k < list.length ; k++) { // 排序 for (int i = 0; i < list.length - k; i++) { if (list[i] > list[i + 1]) { //如果list[i] > list[i + 1],进行交换 int temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; } } } } /** 方法 */ public static void main(String[] args) { int[] list = {10,5,7,8,9,6,14}; bubbleSort(list); for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); }
更多精彩
赞助商链接