标签:
package cn.aust.zyw.demo; /** * Created by zyw on 2016/2/9. * insert-sort */ public class Insertion { public static void sort(int [] a){ int N=a.length; for(int i=1;i<N;i++){ for(int j=i;j>0&&less(a[j],a[j-1]);j--) exch(a,j,j-1); } } private static boolean less(int v, int w){ if(v<w) return true; return false; } private static void exch(int[] a,int i,int j){ int t=a[i];a[i]=a[j];a[j]=t; } private static void show(int[] a){ for(int i=0;i<a.length;i++){ System.out.printf(a[i]+" "); } } public static boolean isSorted(int[] a){ for(int i=1;i<a.length;i++) if(less(a[i],a[i-1])) return false; return true; } public static void main(String args[]){ int a[]={20,5,14,3,63,1,55,11,0}; sort(a); show(a); } }
标签:
原文地址:http://www.cnblogs.com/yunwuzhan/p/5191415.html