1 package com.cn.sortt; 2 3 public class BubbleSort { 4 //冒泡排序1 5 public static void bubble1(int[] array){ 6 for (int i = 0 ;i < array.length;i++){ 7 for(int j = 0;j < array.length-1-i;j++ ){ 8 if(array[j] < array[j+1]){ 9 int temp = array[j]; 10 array[j] = array[j+1]; 11 array[j+1] = temp; 12 } 13 } 14 } 15 for (int i = 0 ;i < array.length;i++){ 16 System.out.print(array[i] + " "); 17 } 18 } 19 20 public static void main(String[] args) { 21 // TODO Auto-generated method stub 22 int[] array = {1,4,3,22,46,7}; 23 //bubble1(array); 24 bubble2(array); 25 26 27 } 28 29 // 冒泡排序2 30 public static void bubble2(int[] array){ 31 for (int i = 0 ;i < array.length;i++){ 32 for(int j = array.length-1;j > i;j-- ){ 33 if(array[j] > array[j-1]){ 34 int temp = array[j]; 35 array[j] = array[j-1]; 36 array[j-1] = temp; 37 } 38 } 39 } 40 for (int i = 0 ;i < array.length;i++){ 41 System.out.print(array[i] + " "); 42 } 43 } 44 45 }
1 package com.cn.sortt; 2 3 public class SelectionSort { 4 //选择排序 5 public static void select(int[] array){ 6 for (int i = 0 ;i < array.length-1;i++){ 7 for(int j = i+1;j < array.length;j++ ){ 8 if(array[i] < array[j]){ 9 int temp = array[j]; 10 array[j] = array[i]; 11 array[i] = temp; 12 } 13 } 14 } 15 for (int i = 0 ;i < array.length;i++){ 16 System.out.print(array[i] + " "); 17 } 18 } 19 20 public static void main(String[] args) { 21 // TODO Auto-generated method stub 22 int[] array = {1,4,3,22,46,7}; 23 select(array); 24 } 25 }
原文:http://www.cnblogs.com/ailsalin/p/6366036.html