日撸代码300行:第43天(插入排序)

代码来自闵老师”日撸 Java 三百行(41-50天)“,链接:https://blog.csdn.net/minfanphd/article/details/116975863

	public void insertionSort() {
		DataNode tempNode;
		int j;
		
		for (int i = 2; i < data.length; i++) {
			tempNode = data [i];
			for (j = i - 1;  data[j].key > tempNode.key; j--) {
				data[j + 1] = data[j];
			}//of for j
			data[j + 1] = tempNode;
		}//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
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值