Skip to content

Commit 9cbd0a1

Browse files
committed
chore algo
1 parent 0d30da4 commit 9cbd0a1

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

‎README.md

+52
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
- [Get the Average value at each level of the tree](#get-the-average-value-at-each-level-of-the-tree)
6767
- [ADT](#adt)
6868
- [Time Memory Trade-Off technique](#time-memory-trade-off-technique)
69+
- [Mandatory Algorithms](#mandatory-algorithms)
70+
- [Binary Search Algorithm](#binary-search-algorithm)
71+
- [Reverse Linked List Algorithm](#reverse-linked-list-algorithm)
72+
- [Merge Sort Algorithm](#merge-sort-algorithm)
6973
- [Coding Interview Question and Answers](#coding-interview-question-and-answers)
7074
- [Graphs](#graphs)
7175
- [Depth First Search Question](#depth-first-search-question)
@@ -772,6 +776,53 @@ abstract data type (ADT) - ADT is defined as a user point of view of a data type
772776

773777
Trade off or invest some memory to improve run time complexity. Suppose use Has-table, set etc. to insert some of the calculations that you want to not repeat.
774778

779+
780+
781+
## Mandatory Algorithms
782+
783+
### Binary Search Algorithm
784+
785+
![](https://i.imgur.com/6qgCObC.png)
786+
<div class="codepen" data-height="300" data-theme-id="dark" data-default-tab="js" data-slug-hash="XWRrPBE" data-user="roopkt" data-prefill='{"title":"Binary Search Algorithm","tags":[],"scripts":[],"stylesheets":[]}'>
787+
<pre data-lang="js">// O(log (n)) time | O(1) space
788+
function binarySearch(array, valueToSearch) {
789+
let low =0; let high = array.length-1;
790+
while(low&lt;=high) {
791+
let mid = low + Math.floor((high-low)/2);
792+
if(valueToSearch === array[mid]) return mid; // Found value, return (exit)
793+
else if (valueToSearch &lt; array[mid]) high = mid-1; // Search in left
794+
else low = mid + 1; // Search in right
795+
}
796+
return -1;
797+
}</pre></div>
798+
799+
### Reverse Linked List Algorithm
800+
801+
![](https://i.imgur.com/MbFK2KZ.png)
802+
<div class="codepen" data-height="300" data-theme-id="dark" data-default-tab="js" data-slug-hash="jOmNvRw" data-user="roopkt" data-prefill='{"title":"Reverse Linked List","tags":[],"scripts":[],"stylesheets":[]}'>
803+
<pre data-lang="js">// O(n) time | O(1) space
804+
function reverse(head) {
805+
let current = head;
806+
let previous = null;
807+
let next = null;
808+
809+
while (current) {
810+
next = current.next;
811+
current.next = previous;
812+
previous = current;
813+
current = next;
814+
}
815+
816+
head = previous;
817+
818+
return head;
819+
}</pre></div>
820+
821+
### Merge Sort Algorithm
822+
823+
![](https://i.imgur.com/wozvfAe.png)
824+
825+
775826
## Coding Interview Question and Answers
776827

777828
### Graphs
@@ -1003,6 +1054,7 @@ Try `Binary Search`.
10031054
</p>
10041055

10051056
- [Question](https://codepen.io/roopkt/pen/mdWayxv?editors=0010)
1057+
-
10061058
- [Answer](https://codepen.io/roopkt/pen/dyvwPej?editors=0011)
10071059

10081060
#### Find the merge point of 2 Linked List

0 commit comments

Comments
 (0)