Skip to content

Commit ba924cb

Browse files
Merge branch 'CodeHarborHub:main' into main
2 parents e63fdd8 + 55a4e90 commit ba924cb

File tree

6 files changed

+836
-0
lines changed

6 files changed

+836
-0
lines changed

assets/LinearSearch_GFG.webp

18.8 KB
Binary file not shown.

assets/binnary-search-.webp

20.8 KB
Binary file not shown.
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
---
2+
id: Linear-Search
3+
title: Linear Search (Geeks for Geeks)
4+
sidebar_label: Linear Search
5+
tags:
6+
- Beginner
7+
- Search Algorithms
8+
- Geeks for Geeks
9+
- CPP
10+
- Python
11+
- Java
12+
- JavaScript
13+
- DSA
14+
description: "This is a solution to the Linear Search problem on Geeks for Geeks."
15+
---
16+
17+
## What is Linear Search?
18+
19+
Linear Search is a simple search algorithm used to find the presence of a target element within a list. It sequentially checks each element of the list until the target element is found or the list ends.
20+
21+
## Algorithm for Linear Search
22+
23+
1. Start from the leftmost element of the list and move towards the right.
24+
2. Compare the target element with each element of the list.
25+
3. If the target element matches with an element, return the index.
26+
4. If the target element does not match any element, return -1.
27+
28+
## How does Linear Search work?
29+
30+
- It starts from the first element and compares the target element with each element in the list.
31+
- If a match is found, it returns the index of the matching element.
32+
- If no match is found after checking all elements, it returns -1 indicating the target is not present in the list.
33+
![Example for Linear Search (GFG) ](../../assets/LinearSearch_GFG.webp)
34+
35+
## Problem Description
36+
37+
Given a list and a target element, implement the Linear Search algorithm to find the index of the target element in the list. If the element is not present, return -1.
38+
39+
## Examples
40+
41+
**Example 1:**
42+
```
43+
Input:
44+
list = [1, 3, 5, 7, 9]
45+
target = 5
46+
Output: 2
47+
```
48+
49+
**Example 2:**
50+
```
51+
Input:
52+
list = [2, 4, 6, 8, 10]
53+
target = 7
54+
Output: -1
55+
```
56+
## Your task
57+
58+
Complete the function search() which takes two integers n , k and an array arr, as input parameters and returns an integer denoting the answer. Return -1 if the number is not found in the array. You don't have to print answers or take inputs.
59+
60+
Expected Time Complexity: $O(n)$
61+
Expected Auxiliary Space: $O(1)$
62+
63+
## Constraints
64+
65+
- $1 <= n <= 10^6$
66+
- $1 <= k <= 10^6$
67+
- $1 <= arr[i] <= 10^9$
68+
69+
## Implementation
70+
71+
<Tabs>
72+
<TabItem value="Python" label="Python" default>
73+
<SolutionAuthor name="@ngmuraqrdd"/>
74+
```python
75+
def linear_search(lst, target):
76+
for i in range(len(lst)):
77+
if lst[i] == target:
78+
return i
79+
return -1
80+
```
81+
</TabItem>
82+
83+
<TabItem value="C++" label="C++">
84+
<SolutionAuthor name="@ngmuraqrdd"/>
85+
```cpp
86+
#include <iostream>
87+
#include <vector>
88+
89+
int linear_search(const std::vector<int>& lst, int target) {
90+
for (int i = 0; i < lst.size(); i++) {
91+
if (lst[i] == target) {
92+
return i;
93+
}
94+
}
95+
return -1;
96+
}
97+
98+
int main() {
99+
std::vector<int> lst = {1, 3, 5, 7, 9};
100+
int target = 5;
101+
std::cout << "Index: " << linear_search(lst, target) << std::endl;
102+
return 0;
103+
}
104+
```
105+
</TabItem>
106+
107+
<TabItem value="Java" label="Java">
108+
<SolutionAuthor name="@ngmuraqrdd"/>
109+
```java
110+
public class LinearSearch {
111+
public static int linearSearch(int[] lst, int target) {
112+
for (int i = 0; i < lst.length; i++) {
113+
if (lst[i] == target) {
114+
return i;
115+
}
116+
}
117+
return -1;
118+
}
119+
120+
public static void main(String[] args) {
121+
int[] lst = {1, 3, 5, 7, 9};
122+
int target = 5;
123+
System.out.println("Index: " + linearSearch(lst, target));
124+
}
125+
}
126+
```
127+
</TabItem>
128+
129+
<TabItem value="JavaScript" label="JavaScript">
130+
<SolutionAuthor name="@ngmuraqrdd"/>
131+
```javascript
132+
function linearSearch(lst, target) {
133+
for (let i = 0; i < lst.length; i++) {
134+
if (lst[i] === target) {
135+
return i;
136+
}
137+
}
138+
return -1;
139+
}
140+
141+
const lst = [1, 3, 5, 7, 9];
142+
const target = 5;
143+
console.log("Index:", linearSearch(lst, target));
144+
```
145+
</TabItem>
146+
</Tabs>
147+
148+
## Complexity Analysis
149+
150+
- **Time Complexity**: $O(n)$, where $n$ is the number of elements in the list. In the worst case, it will search through the entire list.
151+
- **Space Complexity**: $O(1)$, as no extra space is required apart from the input list.
152+
153+
## Advantages and Disadvantages
154+
155+
**Advantages:**
156+
- Simple and easy to implement.
157+
- Does not require the list to be sorted.
158+
159+
**Disadvantages:**
160+
- Inefficient for large lists as it has a linear time complexity.
161+
- Better alternatives exist for sorted lists or when multiple searches are required.
162+
163+
## References
164+
165+
- **GFG Problem:** [GFG Problem](https://www.geeksforgeeks.org/linear-search/)
166+
- **HackerRank Problem:** [HackerRank](https://www.hackerrank.com/challenges/linear-search/problem)
167+
- **Author's Geeks for Geeks Profile:** [MuraliDharan](https://www.geeksforgeeks.org/user/ngmuraqrdd/)
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
---
2+
id: Binary-Search
3+
title: Binary Search (Geeks for Geeks)
4+
sidebar_label: Binary Search
5+
tags:
6+
- Beginner
7+
- Search Algorithms
8+
- Geeks for Geeks
9+
- CPP
10+
- Python
11+
- Java
12+
- JavaScript
13+
- DSA
14+
description: "This is a solution to the Binary Search problem on Geeks for Geeks."
15+
---
16+
17+
## What is Binary Search?
18+
19+
Binary Search is a highly efficient search algorithm used to find the position of a target element within a sorted list. It works by repeatedly dividing the search interval in half and comparing the target value to the middle element of the interval.
20+
21+
## Algorithm for Binary Search
22+
23+
1. Start with the left pointer at the beginning of the list and the right pointer at the end.
24+
2. Calculate the middle index of the current search interval.
25+
3. Compare the target value with the middle element:
26+
- If the target value equals the middle element, return the middle index.
27+
- If the target value is less than the middle element, move the right pointer to $middle - 1$.
28+
- If the target value is greater than the middle element, move the left pointer to $middle + 1$.
29+
4. Repeat steps 2-3 until the left pointer exceeds the right pointer.
30+
5. If the target value is not found, return -1.
31+
32+
## How does Binary Search work?
33+
34+
- It starts by comparing the target value to the middle element of the list.
35+
- If the target value matches the middle element, the search is complete.
36+
- If the target value is less than the middle element, the search continues in the left half of the list.
37+
- If the target value is greater than the middle element, the search continues in the right half of the list.
38+
- This process continues until the target value is found or the search interval is empty.
39+
40+
![Example for Binary Search(GFG)](../../assets/binnary-search-.webp)
41+
42+
## Problem Description
43+
44+
Given a sorted list and a target element, implement the Binary Search algorithm to find the index of the target element in the list. If the element is not present, return -1.
45+
46+
## Examples
47+
48+
**Example 1:**
49+
```
50+
Input:
51+
list = [1, 3, 5, 7, 9]
52+
target = 5
53+
Output: 2
54+
```
55+
56+
**Example 2:**
57+
```
58+
Input:
59+
list = [2, 4, 6, 8, 10]
60+
target = 7
61+
Output: -1
62+
```
63+
64+
## Your Task:
65+
66+
You dont need to read input or print anything. Complete the function binarysearch() which takes arr[], N and K as input parameters and returns the index of K in the array. If K is not present in the array, return -1.
67+
68+
69+
Expected Time Complexity: $O(LogN)$
70+
Expected Auxiliary Space: $O(LogN)$ if solving recursively and O(1) otherwise.
71+
72+
## Constraints
73+
74+
- $1 <= N <= 10^5$
75+
- $1 <= arr[i] <= 10^6$
76+
- $1 <= K <= 10^6$
77+
78+
## Implementation
79+
80+
<Tabs>
81+
<TabItem value="Python" label="Python" default>
82+
<SolutionAuthor name="@ngmuraqrdd"/>
83+
```python
84+
def binary_search(lst, target):
85+
left, right = 0, len(lst) - 1
86+
while left <= right:
87+
mid = left + (right - left) // 2
88+
if lst[mid] == target:
89+
return mid
90+
elif lst[mid] < target:
91+
left = mid + 1
92+
else:
93+
right = mid - 1
94+
return -1
95+
```
96+
</TabItem>
97+
98+
<TabItem value="C++" label="C++">
99+
<SolutionAuthor name="@ngmuraqrdd"/>
100+
```cpp
101+
#include <iostream>
102+
#include <vector>
103+
104+
int binary_search(const std::vector<int>& lst, int target) {
105+
int left = 0, right = lst.size() - 1;
106+
while (left <= right) {
107+
int mid = left + (right - left) / 2;
108+
if (lst[mid] == target) {
109+
return mid;
110+
} else if (lst[mid] < target) {
111+
left = mid + 1;
112+
} else {
113+
right = mid - 1;
114+
}
115+
}
116+
return -1;
117+
}
118+
119+
int main() {
120+
std::vector<int> lst = {1, 3, 5, 7, 9};
121+
int target = 5;
122+
std::cout << "Index: " << binary_search(lst, target) << std::endl;
123+
return 0;
124+
}
125+
```
126+
</TabItem>
127+
128+
<TabItem value="Java" label="Java">
129+
<SolutionAuthor name="@ngmuraqrdd"/>
130+
```java
131+
public class BinarySearch {
132+
public static int binarySearch(int[] lst, int target) {
133+
int left = 0, right = lst.length - 1;
134+
while (left <= right) {
135+
int mid = left + (right - left) / 2;
136+
if (lst[mid] == target) {
137+
return mid;
138+
} else if (lst[mid] < target) {
139+
left = mid + 1;
140+
} else {
141+
right = mid - 1;
142+
}
143+
}
144+
return -1;
145+
}
146+
147+
public static void main(String[] args) {
148+
int[] lst = {1, 3, 5, 7, 9};
149+
int target = 5;
150+
System.out.println("Index: " + binarySearch(lst, target));
151+
}
152+
}
153+
```
154+
</TabItem>
155+
156+
<TabItem value="JavaScript" label="JavaScript">
157+
<SolutionAuthor name="@ngmuraqrdd"/>
158+
```javascript
159+
function binarySearch(lst, target) {
160+
let left = 0, right = lst.length - 1;
161+
while (left <= right) {
162+
const mid = left + Math.floor((right - left) / 2);
163+
if (lst[mid] === target) {
164+
return mid;
165+
} else if (lst[mid] < target) {
166+
left = mid + 1;
167+
} else {
168+
right = mid - 1;
169+
}
170+
}
171+
return -1;
172+
}
173+
174+
const lst = [1, 3, 5, 7, 9];
175+
const target = 5;
176+
console.log("Index:", binarySearch(lst, target));
177+
```
178+
</TabItem>
179+
</Tabs>
180+
181+
## Complexity Analysis
182+
183+
- **Time Complexity**: $O(log n)$, where $n$ is the number of elements in the list. The list is divided in half at each step, leading to logarithmic time complexity.
184+
- **Space Complexity**: $O(1)$, as no extra space is required apart from the input list.
185+
186+
## Advantages and Disadvantages
187+
188+
**Advantages:**
189+
- Highly efficient for large sorted lists.
190+
- Fast search time due to logarithmic time complexity.
191+
192+
**Disadvantages:**
193+
- Requires the list to be sorted.
194+
- Less efficient for small lists compared to linear search.
195+
196+
## References
197+
198+
- **GFG Problem:** [GFG Problem](https://www.geeksforgeeks.org/binary-search/)
199+
- **HackerRank Problem:** [HackerRank](https://www.hackerrank.com/challenges/binary-search/problem)
200+
- **Author's Geeks for Geeks Profile:** [MuraliDharan](https://www.geeksforgeeks.org/user/ngmuraqrdd/)

0 commit comments

Comments
 (0)