日撸代码300行学习笔记 Day 43

1.插入排序

基本思想:每一步将一个待排序的数据插入到前面已经排好序的有序序列中,直到插完所有元素为止。

2.代码

/**
	 * 
	 ***********************************
	 * @Title: insertionSort   
	 * @Description:  
	 * @param:       
	 * @return: void       
	 ***********************************
	 */
	public void insertionSort() {
		DataNode tempNode;
		int j;
		for (int i = 2; i < length; i++) {
			tempNode = data[i];
			
			//find the position to insert
			for (j = i - 1; data[j].key > tempNode.key; j--) {
				data[j+1] = data[j];
			}//Of for j
			
			data[j+1] = tempNode;
			System.out.println("Round " + (i - 1));
			System.out.println(this);
		}//Of for i
	}//Of insertionSort
	
	/**
	 *********************
	 * Test the method.
	 *********************
	 */
	public static void insertionSortTest() {
		int[] tempUnsortedKeys = { -100, 5, 3, 6, 10, 7, 1, 9 };
		String[] tempContents = { "null", "if", "then", "else", "switch", "case", "for", "while" };
		DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);

		System.out.println(tempDataArray);

		tempDataArray.insertionSort();
		System.out.println("Result\r\n" + tempDataArray);
	}// Of insertionSortTest

	/**
	 *********************
	 * The entrance of the program.
	 * 
	 * @param args Not used now.
	 *********************
	 */
	public static void main(String args[]) {
		System.out.println("\r\n-------insertionSortTest-------");
		insertionSortTest();
	}// Of main

3.总结

今日的代码部分很简单,顾今日打算对其他知识进行学习,例如以前学过的new,重写toString,this等等,之前只是大概知道是个什么意思,但是不知道具体很详细的用法等,今日想去再学习一下。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值