Java插入排序递归算法(insert sorting recursively)

import java.util.*; public class InsertSort { public static void sort(int[] a, int m) { if(m==0) { return; } sort(a, m-1); int k = a[m]; while(m>0 && k<a[m-1]) { a[m]=a[m-1]; m--; } a[m]=k; } public static void main(String[] args) { int[] a = {5,2,4,7,1,3,2,6}; System.out.println("Origin: " + Arrays.toString(a)); sort(a, a.length-1); System.out.println("Sorted: " + Arrays.toString(a)); } }

你可能感兴趣的:(Java插入排序递归算法(insert sorting recursively))