`
gmleegmlee
  • 浏览: 116637 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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] + " , ");
	}

}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics