Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Searching an Array, Two Ways
ab
ab

Posted on

     

Searching an Array, Two Ways

Today's algorithm is theSearch Insert Position problem:

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array.

For example, if the inputted array was[1, 2, 6, 8] and the target was2, the function should return the output1, because2 is at the first index in the inputted array. If the inputted array was[1, 2, 6, 8] and the target was4, the function should return the output2, because in this ordered array, if we inserted 4 into it, it would be at index 2 (between numbers 2 and 6).

In this post, I'll discuss two ways to solve this problem. The first way is a single pass approach using a for loop, solved in O(n) time. The second way is a binary search approach, solved in O(log n) time.

Method #1: The Single Pass Approach

The idea behind this approach is to walk through the inputted array and check the value at each index. If the value at the index is the target value, then return that index. If the value is larger than the target value, then we know the target would have been at the index that we're currently on, and the rest of the array would be shifted over, so we also can return that index. Finally, if we're at the end of the array, and we still haven't met or exceeded the value of the target, then we know the target would be appended to the end of the array, so we can return the length of the array as the index.

To break done what I mean with an example, if the inputted array was[1, 3, 5], and the target was3, we'd check each element of the array. At index 1, 3 = 3, so we'd return 1.

First row: array [1, 3, 5]; target = 3. Second row: same array, but blue circle is around the 1. Target still = 3. Beneath it, in blue, is i = 0. Beneath that, in green, is 1 < 3. Third row: same array, and same target, but there's a blue circle around the 3 in the array. Beneath that, in blue, i = 1. Beneath that, in green, is 3 === 3.

If the inputted array was[1, 3, 5], and the target was4, we'd again check each element of the array. We never find the element 4, but at index 2, 5 is greater than 4, so we know that if 4 was in the array, it would be found at index 2. Therefore, we'd return the index 2.

First row: array [1, 3, 5]; target = 4. Second row: same array, but blue circle is around the 1. Target still = 4. Beneath it, in blue, is i = 0. Beneath that, in green, is 1 < 4. Third row: same array, and same target, but there's a blue circle around the 3 in the array. Beneath that, in blue, i = 1. Beneath that, in green, is 3 < 4. Fourth row: same array, and same target, but there's a blue circle around the 5 in the array. Beneath that, in blue, i = 2. Beneath that, in green, 5 > 4.

In a third example, if the inputted array was still[1, 3, 5], but the target was6, we'd still check each element of the array. However, we'd get to the end of the array, and still not find a number equal to or larger than the target, which means if 6 were in the array, it would come at the very end. Therefore, we'd return index 3.

First row: array [1, 3, 5]; target = 6. Second row: same array, but blue circle is around the 1. Target still = 6. Beneath it, in blue, is i = 0. Beneath that, in green, is 1 < 6. Third row: same array, and same target, but there's a blue circle around the 3 in the array. Beneath that, in blue, i = 1. Beneath that, in green, is 3 < 6. Fourth row: same array, and same target, but there's a blue circle around the 5 in the array. Beneath that, in blue, i = 2. Beneath that, in green, 5 < 6.

Coding the First Method

In this first approach, we'll want to walk through the array using a for loop.

functionsearchInsert1(nums,target){for(leti=0;i<nums.length;i++){//...}}
Enter fullscreen modeExit fullscreen mode

At each element in thenums array, we want to check for two things: is the element equal to the target, or is the element larger than the target? In both cases, we know that the index we're on is the index we want to return, so we can simply returni.

functionsearchInsert1(nums,target){for(leti=0;i<nums.length;i++){if(nums[i]===target||nums[i]>target){returni;}//...}}
Enter fullscreen modeExit fullscreen mode

If we get to the end of thenums array and still haven't found an element equal to or larger than the target, then we know our target would come at the end of the array, so we can return the length of thenums array, which would the index of one more element tacked on to the end of the array.

functionsearchInsert1(nums,target){for(leti=0;i<nums.length;i++){if(nums[i]===target||nums[i]>target){returni;}if(i===nums.length-1){returnnums.length;}}}
Enter fullscreen modeExit fullscreen mode

Method #2: The Binary Search Approach

In this approach, we'd want to do a binary search on the sorted array. We'd create two end points, starting at each end of the array, and would find the middle point between them. If the middle point equals the target, we can return that point. If the middle point is greater than the target, then we know we should move the search frame over, and make the end equal to the middle point. If the middle point is less than the target, we know we should move the search frame over, this time making the start equal to the middle point.

We'd use a while loop to keep doing this until the start point was larger than the end point. If that happened, and we never returned the middle point, then we know the target is not in the array, so we can return the start point.

I think binary searches are harder to explain in words without having code right next to it, so I'll try to clear up this approach while working through the solution.

Coding the Second Method

In order to start a binary search, we'd have to have two indexes: the start point, index 0, and the end point,nums.length-1.

functionsearchInsert2(nums,target){letstart=0;letend=nums.length-1;//...}
Enter fullscreen modeExit fullscreen mode

We want to build a while loop to continually checking the middle point. We'll keep checking until the start index is greater than the end index.

Inside the while loop, we'll make a variable calledmidPoint, which we can find by adding the start and end index, dividing by 2, and doingMath.floor() on that result.

functionsearchInsert2(nums,target){letstart=0;letend=nums.length-1;while(start<=end){constmidPoint=Math.floor((start+end)/2);//...}//...}
Enter fullscreen modeExit fullscreen mode

If the middle point is the target, we've found our answer, so we can returnmidPoint, which is the index of the target.

If the middle point is larger than the target, we know we should be changing the end points of the search, moving it more toward the start of the array. Therefore, we should change end to bemidPoint - 1, and also tell the function to continue on to the next round in the while loop.

functionsearchInsert2(nums,target){letstart=0;letend=nums.length-1;while(start<=end){constmidPoint=Math.floor((start+end)/2);if(nums[midPoint]===target)returnmidPoint;if(nums[midPoint]>target){end=midPoint-1;continue;}//...}//...}
Enter fullscreen modeExit fullscreen mode

If the middle point is less than the target, we know our end points are off, and should instead be searching in the latter half of the array. Therefore, we should setstart equal tomidPoint + 1, and continue on in the while loop.

functionsearchInsert2(nums,target){letstart=0;letend=nums.length-1;while(start<=end){constmidPoint=Math.floor((start+end)/2);if(nums[midPoint]===target)returnmidPoint;if(nums[midPoint]>target){end=midPoint-1;continue;}if(nums[midPoint]<target){start=midPoint+1;continue;}}//...}
Enter fullscreen modeExit fullscreen mode

The last thing to do will be to add a return statement outside of the while loop. If, after checking all of the elements in the nums array, we never found the target value, and we've reached the point thatstart is now larger thanend, we know the index value of the target would be atstart, so we can returnstart.

functionsearchInsert2(nums,target){letstart=0;letend=nums.length-1;while(start<=end){constmidPoint=Math.floor((start+end)/2);if(nums[midPoint]===target)returnmidPoint;if(nums[midPoint]>target){end=midPoint-1;continue;}if(nums[midPoint]<target){start=midPoint+1;continue;}}returnstart;}
Enter fullscreen modeExit fullscreen mode

--

Let me know in the comments if you have any questions or other ways you'd approach this problem!

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
chico1992 profile image
chico1992
  • Joined

Isn’t the binary search’s complexity O(log n)?

CollapseExpand
 
a_b_102931 profile image
ab
  • Joined

Whoops! That's a typo. Good catch!

CollapseExpand
 
chico1992 profile image
chico1992
  • Joined

You’re welcome

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromab

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp