算法导论插入算法源伪代码如下
JAVA实现代码如下
package com.bird.math; /** * @deprecated 算法导论中的插入算法实现 * @author bird * */ public class Insert_sort { public int[] Insertsort(int[] A){//插入排序算法 for(int j = 1; j < A.length; j++){ int key = A[j]; int i = j-1; while(i>=0 && A[i]>key){ A[i+1] = A[i]; i = i-1; } A[i+1] = key; } return A; } public static void main(String [] args){ Insert_sort sort = new Insert_sort(); int [] a = {7,4,9,13,5,1,34,24,235,29}; a = sort.Insertsort(a); for(int i = 0; i < a.length; i++){ System.out.print(a[i]+" "); } } }
运行结果如下
1 4 5 7 9 13 24 29 34 235