基本排序算法

冒泡排序:

for(int i = 0; i < testNumber.length; i++) {	//确定当前需要比较的值
			
			int temp = testNumber[i];	//临时变量
			
			for(int j = (i+1); j < testNumber.length; j++ ) {		//需要被依次比较的数值
				if(testNumber[i] > testNumber[j]) {
					testNumber[i] = testNumber[j];
					testNumber[j] = temp;
					temp = testNumber[i];
				}
			}
		}

选择排序:

for(int i = 0; i < testNumber.length; i++) {
			int index = i;	//临时索引值
			int currentValue = testNumber[i];	//当前比较值
			int temp = testNumber[i];
			
			for(int j = (i+1); j < testNumber.length; j++) {
				if(currentValue > testNumber[j]) {
					currentValue = testNumber[j];
					index = j;
				}
			}
			
			//确定交换,每轮只交换一次
			testNumber[i] = testNumber[index];
			testNumber[index] = temp;
		}

插入排序:

int in; 	//已排序区
		int out;	//待排序区
		
		for(out = 1; out < testNumber.length; out++) {
			int temp = testNumber[out];		//当前待排序区最左边数据
			in = out;		//已排序区的数组界限
			
			//把待排序区的数据和已排序区的数据进行,并且比较的方式是从一已排序的数据的右边向左边来跟待排序的数据进行比较,并右移
			while(in > 0 && testNumber[in-1] >= temp) {					
				testNumber[in] = testNumber[in-1];	//依次右移
				--in;	//右移后,接下来要跟当前待排序去数据比较的索引值
			}
			testNumber[in] = temp;	


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值