Skip to content
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
merged 2 commits into from
Oct 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

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
search page updated in dsa folder
nsundriyal62 committed Oct 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 7eb3f67dd37ce65326a92487df4519a025b6e9b7
167 changes: 164 additions & 3 deletions content/batch/dsa/search.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,170 @@
---
title: Search
Copy link
Member

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

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>
Copy link
Member

Choose a reason for hiding this comment

The 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>