Skip to content

Improved Code reading #791

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

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 18 additions & 11 deletions dsa/Algorithms/dijkstra.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ function Dijkstra(Graph, source):

## Implementing Dijkstra's Algorithm

### Python Implementation

```python
<Tabs>
<TabItem value="Python" label="Python">
``` Python showLineNumbers
import heapq

def dijkstra(graph, start):
Expand Down Expand Up @@ -96,11 +96,13 @@ graph = {
}

print(shortest_path(graph, 'A', 'D'))

```
</TabItem>

### Java Implementation
<TabItem value="Java" label="Java">

```java
``` jsx showLineNumbers
import java.util.*;

public class Dijkstra {
Expand Down Expand Up @@ -170,11 +172,12 @@ public class Dijkstra {
}
}
}
```

### C++ Implementation
```
</TabItem>

```cpp
<TabItem value="Cpp" label="Cpp">
```cpp showLineNumbers
#include <iostream>
#include <vector>
#include <unordered_map>
Expand Down Expand Up @@ -248,11 +251,12 @@ int main() {
cout << endl;
return 0;
}
```

### JavaScript Implementation
```
</TabItem>

```javascript
<TabItem value="JavaScript" label="JavaScript">
```jsx showLineNumbers
function dijkstra(graph, start) {
let distances = {};
let previousNodes = {};
Expand Down Expand Up @@ -328,7 +332,10 @@ let graph = {
};

console.log(shortestPath(graph, 'A', 'D'));

```
</TabItem>
</Tabs>

## Applications of Dijkstra's Algorithm

Expand Down
31 changes: 18 additions & 13 deletions dsa/Algorithms/hashing.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ Hashing is the process of converting input data (keys) into a fixed-size integer

## Implementing Hash Tables

### Python Implementation

```python
<Tabs>
<TabItem value="Python" label="Python">
```Python showLineNumbers
class HashTable:
def __init__(self, size):
self.size = size
Expand Down Expand Up @@ -105,11 +105,12 @@ ht.insert("banana", 2)
print(ht.lookup("apple")) # Output: 1
ht.delete("apple")
print(ht.lookup("apple")) # Output: None
```

### Java Implementation
```
</TabItem>

```java
<TabItem value="Java" label="Java">
```jsx showLineNumbers
import java.util.LinkedList;

class HashTable<K, V> {
Expand Down Expand Up @@ -173,11 +174,11 @@ class HashTable<K, V> {
System.out.println(ht.lookup("apple")); // Output: null
}
}
```

### C++ Implementation

```cpp
```
</TabItem>
<TabItem value="Cpp" label="Cpp">
```cpp showLineNumbers
#include <iostream>
#include <list>
#include <vector>
Expand Down Expand Up @@ -233,11 +234,12 @@ int main() {
cout << ht.lookup("apple") << endl; // Output: -1
return 0;
}
```

### JavaScript Implementation
```
</TabItem>

```javascript
<TabItem value="JavaScript" label="JavaScript">
```jsx showLineNumbers
class HashTable {
constructor(size) {
this.size = size;
Expand Down Expand Up @@ -287,7 +289,10 @@ ht.delete("apple");
console.log(ht.lookup("apple"));

// Output: null

```
</TabItem>
</Tabs>

## Time Complexity Analysis

Expand Down
30 changes: 18 additions & 12 deletions dsa/Algorithms/prims.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ function prim(graph, start):
return MST
```

## Implementation in Various Languages
<Tabs>
<TabItem value="Python" label="Python">

### Python

```python
```python showLineNubmers
import heapq

def prim(graph, start):
Expand Down Expand Up @@ -79,11 +78,13 @@ graph = {

mst = prim(graph, 'A')
print(mst) # Output: [('A', 'B', 1), ('B', 'D', 2), ('B', 'C', 3)]

```
</TabItem>

### Java
<TabItem value="Java" label="Java">

```java
```jsx showLineNumbers
import java.util.*;

public class Prim {
Expand Down Expand Up @@ -131,11 +132,12 @@ public class Prim {
}
}
}
```

### C++
```
</TabItem>

```cpp
<TabItem value="Cpp" label="Cpp">
```cpp showLineNumbers
#include <iostream>
#include <vector>
#include <queue>
Expand Down Expand Up @@ -191,11 +193,12 @@ int main() {
cout << "[" << edge[0] << ", " << edge[1] << ", " << edge[2] << "]" << endl; // Output: [0, 1, 1], [1, 3, 2], [1, 2, 3]
}
}
```

### JavaScript
```
</TabItem>

```javascript
<TabItem value="JavaScript" label="JavaScript">
```jsx showLineNumbers
class MinHeap {
constructor() {
this.heap = [];
Expand Down Expand Up @@ -297,7 +300,10 @@ const graph = {

const mst = prim(graph, 'A');
console.log(mst); // Output: [['A', 'B', 1], ['B', 'D', 2], ['B', 'C', 3]]

```
</TabItem>
</Tabs>

## Example

Expand Down
64 changes: 36 additions & 28 deletions dsa/Algorithms/searching.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Linear search, also known as sequential search, is a simple search algorithm tha

![linear search](https://miro.medium.com/v2/resize:fit:1200/1*eTQoIHGdG58sy-iMwcp97w.png)

### Python Implementation

```python
<Tabs>
<TabItem value="Python" label="Python">
```python showLineNumbers
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
Expand All @@ -28,11 +28,11 @@ def linear_search(arr, target):
arr = [10, 20, 30, 40, 50]
target = 30
print(linear_search(arr, target)) # Output: 2
```

### Java Implementation

```java
```
</TabItem>
<TabItem value="Java" label="Java">
```jsx showLineNumbers
public class LinearSearch {

public static int linearSearch(int[] arr, int target) {
Expand All @@ -50,11 +50,12 @@ public class LinearSearch {
System.out.println(linearSearch(arr, target)); // Output: 2
}
}
```

### C++ Implementation
```
</TabItem>

```cpp
<TabItem value="Cpp" label="Cpp">
```cpp showLineNumbers
#include <iostream>
#include <vector>

Expand All @@ -73,11 +74,11 @@ int main() {
std::cout << linearSearch(arr, target) << std::endl; // Output: 2
return 0;
}
```

### JavaScript Implementation

```javascript
```
</TabItem>
<TabItem value="JavaScript" label="JavaScript">
```jsx showLineNumbers
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
Expand All @@ -90,17 +91,20 @@ function linearSearch(arr, target) {
let arr = [10, 20, 30, 40, 50];
let target = 30;
console.log(linearSearch(arr, target)); // Output: 2

```
</TabItem>
</Tabs>

## 2. Binary Search

Binary search is a more efficient search algorithm for sorted arrays. It works by repeatedly dividing the search interval in half until the target element is found or the interval is empty.

![binary search](https://data-flair.training/blogs/wp-content/uploads/sites/2/2023/09/binary-search-in-c-1.webp)

### Python Implementation

```python
<Tabs>
<TabItem value="Python" label="Python">
```python showLineNumbers
def binary_search(arr, target):
low = 0
high = len(arr) - 1
Expand All @@ -118,11 +122,11 @@ def binary_search(arr, target):
arr = [10, 20, 30, 40, 50]
target = 30
print(binary_search(arr, target)) # Output: 2
```

### Java Implementation

```java
```
</TabItem>
<TabItem value="Java" label="Java">
```jsx showLineNumbers
public class BinarySearch {

public static int binarySearch(int[] arr, int target) {
Expand All @@ -148,11 +152,11 @@ public class BinarySearch {
System.out.println(binarySearch(arr, target)); // Output: 2
}
}
```

### C++ Implementation

```cpp
```
</TabItem>
<TabItem value="Cpp" label="Cpp">
```cpp showLineNumbers
#include <iostream>
#include <vector>

Expand All @@ -179,11 +183,12 @@ int main() {
std::cout << binarySearch(arr, target) << std::endl; // Output: 2
return 0;
}
```

### JavaScript Implementation
```
</TabItem>

```javascript
<TabItem value="JavaScript" label="JavaScript">
```jsx showLineNumbers
function binarySearch(arr, target) {
let low = 0;
let high = arr.length - 1;
Expand All @@ -204,7 +209,10 @@ function binarySearch(arr, target) {
let arr = [10, 20, 30, 40, 50];
let target = 30;
console.log(binarySearch(arr, target)); // Output: 2

```
</TabItem>
</Tabs>

## Time Complexity Analysis

Expand Down
Loading