`
jsczxy2
  • 浏览: 1260256 次
  • 性别: Icon_minigender_1
  • 来自: 常州
文章分类
社区版块
存档分类
最新评论

数据结构与算法:插入排序

阅读更多

 

import java.util.Arrays;

import junit.framework.TestCase;

/**
 * 插入排序
 * 
 * @author jsczxy2
 * 
 */
public class InsertSort extends TestCase {

	@Override
	protected void setUp() throws Exception {
		super.setUp();
	}

	private void insertSort(int[] a) {
		int i, j, temp;
		for (i = 1; i < a.length; i++) {
			temp = a[i];
			for (j = i; j > 0; j--) {
				if (a[j - 1] > temp) {
					a[j] = a[j - 1];
				} else
					break;
			}
			a[j] = temp;
		}
	}

	public void test() {
		int a[] = { 4, 2, 6, 12, 7, 9, 1 };
		insertSort(a);
		System.out.println(Arrays.toString(a));
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics