首页 > 其他 > 详细

【数据结构与算法】直接插入排序

时间:2014-08-11 12:04:12      阅读:358      评论:0      收藏:0      [点我收藏+]

直接插入排序的时间复杂度的O(N^2),空间复杂度是O(1)。

下面是代码:

/**
 * 源码名称: InsertionSort.java 
 * 日期:2014-08-11 
 * 程序功能:直接插入排序 
 * 版权:CopyRight@A2BGeek
 * 作者:A2BGeek
 */
public class InsertionSort {
	public void insertionSort(int[] in) {
		int length = in.length;
		int i, j;
		for (i = 1; i < length; i++) {
			int tmp = in[i];
			for (j = i - 1; j >= 0 && tmp < in[j]; j--) {
				in[j + 1] = in[j];
			}
			in[j + 1] = tmp;
			printArray(in);
		}
	}

	public void printArray(int[] in) {
		for (int i : in) {
			System.out.print(i + " ");
		}
		System.out.println();
	}

	public static void main(String[] args) {
		int[] testCase = { 1, 3, 4, 10, 2, 5, 6, 7, 9, 11 };
		InsertionSort mInsertionSort = new InsertionSort();
		mInsertionSort.printArray(testCase);
		mInsertionSort.insertionSort(testCase);
		mInsertionSort.printArray(testCase);
	}
}


【数据结构与算法】直接插入排序,布布扣,bubuko.com

【数据结构与算法】直接插入排序

原文:http://blog.csdn.net/a2bgeek/article/details/38488749

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!