标签:
package sort; import java.util.Scanner; public class InsertSort { public static int arr[]; static{ System.out.println("Please enter how many figure do you want to sort:"); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); arr=new int[n]; System.out.println("Before sort:"); for(int i=0;i<arr.length;i++){ arr[i]=(int)(Math.random()*100); System.out.print(arr[i]+" "); } System.out.println(); } public static void main(String args[]){ int m=arr.length-1; insertSort(m); System.out.println("After sort:"); for(int i=0;i<arr.length;i++) System.out.print(arr[i]+" "); System.out.println(); } public static void insertSort(int m){ int j=0; int insertValue=0; for(int i=1;i<=m;i++){ insertValue=arr[i]; j=i-1; while(j>=0 && insertValue<arr[j]){ arr[j+1]=arr[j]; j--; } arr[j+1]=insertValue; System.out.print("Current sorting:"); for(int k=0;k<arr.length;k++) System.out.print(arr[k]+" "); System.out.println(); } } }
标签:
原文地址:http://www.cnblogs.com/lvbubu/p/4684398.html