InsertionSort
插入排序
import java.util.Arrays; public class InsertionSort { public static void main(String[] args) { int[] a = new int[10]; populateArray(a); printArray(a); insertionSort(a); System.out.println(); printArray(a); } private static void insertionSort(int[] a) { // TODO Auto-generated method stub for (int i = 1; i < a.length; i++) { int value = a[i]; int j = i; while (j > 0 && a[j - 1] > value) { a[j] = a[j - 1]; j--; } a[j] = value; } } private static void printArray(int[] a) { // TODO Auto-generated method stub System.out.println(Arrays.toString(a)); } private static void populateArray(int[] arr) { // TODO Auto-generated method stub for (int i = 0; i < arr.length; i++) { arr[i] = (int) (Math.random() * 100); } } }
剩下的几种排序算法的实现可以参考:https://github.com/jinfenglee/algorithm/tree/master/algorithm/src/com/jinfenglee/sort
2014-10-16 23:41:28