-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
search page updated in dsa folder #146
Merged
+163
−4
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Loading status checks…
search page updated in dsa folder
commit 7eb3f67dd37ce65326a92487df4519a025b6e9b7
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,170 @@ | ||
--- | ||
title: Search | ||
description: Lear Search in JavaScript | ||
description: learn Search in JavaScript | ||
--- | ||
|
||
<Callout> | ||
|
||
Want to improve this page? Raise a issue on [@github](https://github.com/FrontendFreaks/Official-Website). | ||
</Callout> | ||
Want to improve this page?. Raise a issue on [@github](https://github.com/FrontendFreaks/Official-Website). | ||
</Callout> | ||
|
||
## What's on this section? | ||
|
||
In this section, you will: | ||
|
||
- 🎨 Explore different search algorithms in JavaScript. | ||
- 🔍 Understand the concepts of linear search and binary search. | ||
- 💡 Learn how to implement linear and binary search algorithms. | ||
- 🚀 Practice your knowledge through assignments related to search algorithms. | ||
|
||
<Tabs defaultValue="learn"> | ||
|
||
<TabsList> | ||
<TabsTrigger value="learn">Learn</TabsTrigger> | ||
<TabsTrigger value="assignment">Assignment</TabsTrigger> | ||
</TabsList> | ||
|
||
<TabsContent value="learn"> | ||
|
||
## 📺 Watch Now | ||
<p> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have to add Video Player and pass the link, check HTML page for reference |
||
<a href="https://youtu.be/7aLKVYpYugQ"> | ||
<img src="https://img.youtube.com/vi/7aLKVYpYugQ/0.jpg" alt="Searching Algorithms in JavaScript" /> | ||
</a> | ||
</p> | ||
|
||
<Callout type="info"> | ||
We hope that you found the tutorial video helpful in understanding the basic concepts of search in javascript, You can refer this notes 📝 for quick revision. | ||
</Callout> | ||
## 📝 Study Notes | ||
|
||
### Linear Search in JavaScript | ||
|
||
```javascript | ||
const arr = [1, 2, 6, 9, 0, -5]; | ||
|
||
const linearSearch = (arr, target) => { | ||
for (let i = 0; i < arr.length; i++) { | ||
if (arr[i] === target) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
console.log(linearSearch(arr, 8)); | ||
console.log(arr.includes(9)); | ||
console.log(arr.indexOf(9)); | ||
console.log(arr.find((num) => num > 0)); | ||
console.log(arr.findIndex((num) => num < 0)); | ||
``` | ||
|
||
### Binary Search In JavaScript | ||
|
||
```javascript | ||
const BinarySearch = (arr, target) => { | ||
let start = 0, end = arr.length - 1; | ||
while (start <= end) { | ||
let mid = Math.floor((start + end) / 2); | ||
|
||
if (arr[mid] === target) { | ||
return mid; | ||
} | ||
|
||
else if (arr[mid] > target) { | ||
end = mid - 1; | ||
} | ||
|
||
else { | ||
start = mid + 1; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
console.log(BinarySearch([1, 4, 6, 9, 12, 15], 8)); | ||
``` | ||
|
||
### Binary Search using Recursion | ||
|
||
```javascript | ||
const BinarySearchRecur = (arr, target) => { | ||
return BinarySearchUtil(arr, target, 0, arr.length); | ||
} | ||
|
||
const BinarySearchUtil = (arr, target, start, end) => { | ||
if (start > end) | ||
return -1; | ||
|
||
let mid = Math.floor((start + end) / 2); | ||
|
||
if (arr[mid] === target) { | ||
return mid; | ||
} | ||
|
||
else if (arr[mid] > target) { | ||
return BinarySearchUtil(arr, target, start, mid - 1); | ||
} | ||
|
||
else { | ||
return BinarySearchUtil(arr, target, mid + 1, end); | ||
} | ||
} | ||
``` | ||
|
||
### Find floor and ceil value of X in an array | ||
|
||
```javascript | ||
const floorCeil = (arr, target) => { | ||
let start = 0, end = arr.length; | ||
let floor = -1, ceil = -1; | ||
while(start <= end){ | ||
let mid = Math.floor((start + end)/2); | ||
|
||
if(arr[mid] === target){ | ||
floor = mid; | ||
ceil = mid; | ||
return [ceil, mid] | ||
} | ||
|
||
else if(arr[mid] > target){ | ||
ceil = mid; | ||
end = mid - 1; | ||
} | ||
|
||
else { | ||
floor = mid; | ||
start = mid + 1; | ||
} | ||
} | ||
|
||
return [ceil, floor] | ||
} | ||
``` | ||
|
||
|
||
</TabsContent> | ||
|
||
<TabsContent value="assignment"> | ||
## Practice Questions | ||
|
||
### Level 1 | ||
- [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | ||
- [First Bad Version](https://leetcode.com/problems/first-bad-version) | ||
- [Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/) | ||
- [Binary Search](https://leetcode.com/problems/binary-search) | ||
- [Search Insert Position](https://leetcode.com/problems/search-insert-position) | ||
- [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array) | ||
|
||
### Level 2 | ||
- [Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array) | ||
- [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | ||
- [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | ||
- [Find Peak Element](https://leetcode.com/problems/find-peak-element) | ||
- [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array) | ||
</TabsContent> | ||
|
||
|
||
</Tabs> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change Title to Linear & Binary Search