使用java实现冒泡排序,不适用Arrays工具类:
public class BubbSort { public static void main(String[] args) { int []array = {1,23,48,89,199,588,22,33}; maopaoSort(array); for (int i: array) { System.out.println(i); } } private static void maopaoSort(int[] array) { for (int i = array.length-1; i > 0; i--){ for (int j = 0; j < i; j++){ if (array[j] > array[j + 1]){ int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } }
选择排序:
public class SelectSort { public static void main(String[] args) { int []array = {111,223,48,89,199,588,22,33,7,89}; selectSort(array); for (int i: array){ System.out.println(i); } } //选择排序 //每次选出一个最小的放在最前面 private static void selectSort(int[] array) { for (int i = 0; i < array.length; i++){ //找出最小数的下标并且和i保持一致 int min = i; for (int j = i + 1; j < array.length; j++){ if(array[min] > array[j]){ //交换下标 int temp = min; min = j; j = temp; } } //下标不一致,便交互其中的数据 if (min != i){ int temp = array[min]; array[min] = array[i]; array[i] = temp; } } } }