java插入排序算法

public class InsertionSort2 {
	
	public static void insertSort(Comparable[] data){
		int j;
		for(int p = 1; p < data.length; p++){
			Comparable key = data[p];
			for(j = p; j > 0 && (key.compareTo(data[j-1]) < 0); j--)
				data[j] = data[j-1];
			data[j] = key;
		}
	}
	
	public static void main(String[] args){
		Comparable[] b = {6,3,1,8,22,34,2,12,15,7,99,11};
		insertSort(b);
		for(int i = 0; i < b.length; i++)
			System.out.print(b[i] + ",");
	}

}












public class InsertionSort {
	
	public static void insertSort(int[] a){
		int j;
		
		for(int p = 1; p < a.length; p++){
			
			int temp = a[p];
			for(j = p; (j > 0) && (temp < a[j-1]); j--){
				a[j] = a[j-1];
			}
			a[j] = temp;
		}
		
	}
	
	public static void main(String[] args){
		int[] b = {9,8,5,2,1,7,3,4,8888,43,23525,232,12,34,53,75,13,15,17};
		insertSort(b);
		for(int i = 0; i < b.length; i++)
			System.out.print(b[i] + " , ");
	}

}



你可能感兴趣的:(java,算法,J#)