0
0
DSA Javascriptprogramming~30 mins

Search in Rotated Sorted Array in DSA Javascript - Build from Scratch

Choose your learning style9 modes available
Search in Rotated Sorted Array
📖 Scenario: Imagine you have a list of numbers that was originally sorted from smallest to largest. But then, someone took a part of the list from the front and moved it to the end. This is called a rotated sorted array.For example, the list [1, 2, 3, 4, 5] might become [3, 4, 5, 1, 2] after rotation.
🎯 Goal: You will write a program to find the position of a number in this rotated list. If the number is not in the list, your program should say it is not found.
📋 What You'll Learn
Create an array called rotatedArray with exact numbers given.
Create a variable called target to hold the number to find.
Write a function called searchRotatedArray that finds the index of target in rotatedArray using a smart search.
Print the index found or -1 if the number is not in the array.
💡 Why This Matters
🌍 Real World
Rotated sorted arrays appear in systems where data is shifted or rotated for performance or storage reasons, like in circular buffers or time-based logs.
💼 Career
Understanding how to search efficiently in rotated sorted arrays is useful for software engineers working on search algorithms, database indexing, and system optimizations.
Progress0 / 4 steps
1
Create the rotated sorted array
Create an array called rotatedArray with these exact numbers in order: 15, 18, 2, 3, 6, 12
DSA Javascript
Hint

Use square brackets [] to create the array and separate numbers with commas.

2
Set the target number to find
Create a variable called target and set it to the number 3
DSA Javascript
Hint

Use const to create the variable and assign the number 3.

3
Write the search function for rotated sorted array
Write a function called searchRotatedArray that takes arr and target as inputs and returns the index of target in arr. Use a while loop with variables left and right to do a smart search in the rotated array.
DSA Javascript
Hint

Use binary search logic but adjust the left and right pointers based on which side is sorted.

4
Print the index of the target
Use console.log to print the result of calling searchRotatedArray with rotatedArray and target.
DSA Javascript
Hint

Call the function with rotatedArray and target and print the result.