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

Javascript scripts #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Algorithms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

| Algorithm | Complexity | Implementations |
| --- | --- | --- |
| [Linear Search](#LinearSearch) | O(n) | <img src="https://img.shields.io/badge/-Python-blue"> <img src="https://img.shields.io/badge/-C-black"> <img src="https://img.shields.io/badge/-C++-grey"> <img src="https://img.shields.io/badge/-Java-red"> <img src="https://img.shields.io/badge/-PHP-purple"> |
| [Linear Search](#LinearSearch) | O(n) | <img src="https://img.shields.io/badge/-Python-blue"> <img src="https://img.shields.io/badge/-C-black"> <img src="https://img.shields.io/badge/-C++-grey"> <img src="https://img.shields.io/badge/-Java-red"> <img src="https://img.shields.io/badge/-PHP-purple"> <img src="https://img.shields.io/badge/-JavaScript-yellow">|
| [Binary Search](#BinarySearch) | Theta(logn) | <img src="https://img.shields.io/badge/-Python-blue"> <img src="https://img.shields.io/badge/-C-black"> <img src="https://img.shields.io/badge/-C++-grey"> <img src="https://img.shields.io/badge/-Java-red"> <img src="https://img.shields.io/badge/-PHP-purple">|
| [Ternary Search](#TernarySearch) | Theta(logn) | <img src="https://img.shields.io/badge/-C++-grey"> |
| [Jump Search](#JumpSearch) | O(sqrt(n)) | <img src="https://img.shields.io/badge/-Python-blue"> <img src="https://img.shields.io/badge/-C-black"> <img src="https://img.shields.io/badge/-C++-grey"> <img src="https://img.shields.io/badge/-Java-red"> <img src="https://img.shields.io/badge/-PHP-purple"> |
Expand Down
40 changes: 40 additions & 0 deletions Algorithms/Searching/binarySearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Binary Search implementathon in JavaScript. Theta(log n).
*/

function binarySearch(elements, start, end, x){
if (start > end){
return -1
}

let middle = Math.floor((start + end) / 2)
console.log(middle)
if (elements[middle] === x){
return middle
}
if (x < elements[middle]){
return binarySearch(elements, start, middle - 1, x)
} else {
return binarySearch(elements, middle + 1, end, x)
}

return -1
}

elements = [1, 3, 4, 5, 12, 16, 43, 39, 99]
x = 12

left = 0
right = elements.length

position = binarySearch(elements, left, right, x)

console.log("The elemens are: ", elements)
console.log("The element to find is ", x)

if (position === -1){
console.log("The element has not been found")
} else {
console.log("The element has been found at position ", position + 1)
}

27 changes: 27 additions & 0 deletions Algorithms/Searching/linearSearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Linear Search implementation in JavaScript
* O(n)
*/

function LinearSearch(elements, x){
// Linear Search performed on elements to found x
for (var i=0; i < elements.length; i++){
if (elements[i] === x){
return i;
}
}
return -1
}

let elements = [3, 1, 2, 4, 6, 7, 8, 49, 33, 192]
let x = 4; // element to be found


console.log("The elemtents are ", elements)
console.log("The element to be found is: ", x)

let position = LinearSearch(elements, x)
if (position === -1){
console.log("The elements has not been found")
} else{
console.log("The element has been found at position ", position + 1)
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ g++ script.cpp -o compiled/script

* <img src="https://img.shields.io/badge/-JavaScipt-yellow"> :

You have to have node installed.

```
node script.js
```

* <img src="https://img.shields.io/badge/-PHP-purple"> :

***
Expand Down