在无序数组中如何查找指定值的位置(索引)及算法优化

1.基本思路

查找一个值的常规方法是:在数组中依次查找遍历,找到这个值则退出循环,如果遍历到最后一个元素,仍没有找到这个指定值就表明,数组中没有这个值。

2.代码实现

// 线性查找(优化前)
	public int indexOfValueBySeq(int[] array, int value) {
		long startTime = System.currentTimeMillis();
		long endTime = 0;
		for (int i = 0, arrayLen = array.length; i < arrayLen; i++) {
			if (array[i] == value) {
				endTime = System.currentTimeMillis();
				System.out.println("优化前线性查找用时:" + (endTime - startTime) + "毫秒。");

				return i;
			}
		}
		endTime = System.currentTimeMillis();

		System.out.println("优化前线性查找用时:" + (endTime - startTime) + "毫秒。");

		return -1;

	}

3.反思

上面的操作方式,每循环一次就会对下标取值进行判断(下标是否越界),当数组元素较多时,这过程就会相当耗时,增大时间复杂度,而这种判断可以算法优化省去。具体思路是将“数组元素与待查找值的比较”与“下标是否越界”这两步操作合在起。

4.算法调整

// 线性查找(优化后)
	public int indexOfValueBySeqOptim(int[] array, int value) {
		long startTime = System.currentTimeMillis();
			// 计算出array数组的长度,避免后面反复访问其属性
		int arrLen = array.length;
		// 临时变量tmp保存最后一个元素值,便于查找结束后恢复其原始值
		int tmp = array[ arrLen - 1];
		// 将要查找的值赋值给数组的最后一个元素。
		array[arrLen - 1] = value;
		// 记录当前遍历到的数组索引
		int k = 0;
		// 当前遍历到的元素值不等于查找的值,索引k就加1,遍历下一个元素
		while (value != array[k]) {
			k++;
		}
		// 恢复数组中最后一个元素值
		array[arrLen - 1] = tmp;
		long endTime = 0;
		if (k < arrLen - 1) {
			endTime = System.currentTimeMillis();
			System.out.println("优化后线性查找用时:" + (endTime - startTime) + "毫秒。");
			return k;
		} else {
			if (value == tmp) {
				// 数组原始数据的最后一个元素等于要查找的值
				endTime = System.currentTimeMillis();
				System.out.println("优化后线性查找用时:" + (endTime - startTime) + "毫秒。");
				return arrLen - 1;
			} else {
				/*
				 * 当k=array.length - 1,则表明数组中没有value这个值(在while循环中
				 * value==array[k]时 退出循环体,是因为开始时“array[array.length - 1] =
				 * value;”赋值语句,使得最后一个元素 一定等于要查找的值,而不是数组原始数据的最后一个元素等于要查找的值)
				 */
				endTime = System.currentTimeMillis();
				System.out.println("优化后线性查找用时:" + (endTime - startTime) + "毫秒。");
				return -1;
			}
		}

	}
  • 调用查找方法
	int nums1[] = new int[100000];
	int nums2[] = new int[100000];

	// int nums3[] = new int[100000];
	// int nums4[] = new int[100000];
	public static void main(String[] cmds) {
		FindValue find = new FindValue();
		find.initArrays();
		int index1 = find.indexOfValueBySeq(find.nums1, 9700);
		int index2 = find.indexOfValueBySeqOptim(find.nums2, 9700);
		System.out.println(index1);
		System.out.println(index2);
	}

	public void initArrays() {
		Random rd = new Random();

		for (int i = 0; i < nums1.length; i++) {
			nums1[i] = rd.nextInt(100000);
		}
		// for (int i = 0; i < nums1.length; i++) {
		// nums1[i] = 100000 - i;
		// }
		nums2 = Arrays.copyOf(nums1, nums1.length);
		// nums3 = Arrays.copyOf(nums1, nums1.length);
		// nums4 = Arrays.copyOf(nums1, nums1.length);
	}
  • 控制台输出
    在这里插入图片描述
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值