Skip to content

Commit 45f6954

Browse files
committed
chore: algo
1 parent 1522e50 commit 45f6954

File tree

1 file changed

+23
-74
lines changed

1 file changed

+23
-74
lines changed

‎README.md

+23-74
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@
6767
- [ADT](#adt)
6868
- [Time Memory Trade-Off technique](#time-memory-trade-off-technique)
6969
- [Mandatory Algorithms](#mandatory-algorithms)
70-
- [Binary Search Algorithm](#binary-search-algorithm)
70+
- [Binary Search on sorted Array Algorithm](#binary-search-on-sorted-array-algorithm)
7171
- [Reverse Linked List Algorithm](#reverse-linked-list-algorithm)
7272
- [Merge Sort Algorithm](#merge-sort-algorithm)
73+
- [Binary Search Tree Implementation](#binary-search-tree-implementation)
7374
- [Coding Interview Question and Answers](#coding-interview-question-and-answers)
7475
- [Graphs](#graphs)
7576
- [Depth First Search Question](#depth-first-search-question)
@@ -776,96 +777,44 @@ abstract data type (ADT) - ADT is defined as a user point of view of a data type
776777

777778
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.
778779

779-
780+
![](https://i.imgur.com/wERnlTM.png)
780781

781782
## Mandatory Algorithms
782783

783-
### Binary Search Algorithm
784+
### Binary Search on sorted Array Algorithm
784785

785786
![](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>
787+
788+
789+
<p class="codepen" data-height="300" data-theme-id="dark" data-default-tab="js" data-slug-hash="XWRrPBE" data-user="roopkt" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;">
790+
<span>See the Pen <a href="https://codepen.io/roopkt/pen/XWRrPBE">
791+
Binary Search Algorithm</a> by Rupesh Tiwari (<a href="https://codepen.io/roopkt">@roopkt</a>)
792+
on <a href="https://codepen.io">CodePen</a>.</span>
793+
</p>
798794

799795
### Reverse Linked List Algorithm
800796

801797
![](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-
}
815798

816-
head = previous;
817799

818-
return head;
819-
}</pre></div>
800+
<p class="codepen" data-height="300" data-theme-id="dark" data-default-tab="js" data-slug-hash="WNjegBp" data-user="roopkt" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;">
801+
<span>See the Pen <a href="https://codepen.io/roopkt/pen/WNjegBp">
802+
Merge Sort Algorithm</a> by Rupesh Tiwari (<a href="https://codepen.io/roopkt">@roopkt</a>)
803+
on <a href="https://codepen.io">CodePen</a>.</span>
804+
</p>
820805

821806
### Merge Sort Algorithm
822807

823808
![](https://i.imgur.com/wozvfAe.png)
809+
<p class="codepen" data-height="300" data-theme-id="dark" data-default-tab="js" data-slug-hash="WNjegBp" data-user="roopkt" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;">
810+
<span>See the Pen <a href="https://codepen.io/roopkt/pen/WNjegBp">
811+
Merge Sort Algorithm</a> by Rupesh Tiwari (<a href="https://codepen.io/roopkt">@roopkt</a>)
812+
on <a href="https://codepen.io">CodePen</a>.</span>
813+
</p>
824814

825-
<div class="codepen" data-height="300" data-theme-id="dark" data-default-tab="js" data-slug-hash="WNjegBp" data-user="roopkt" data-prefill='{"title":"Merge Sort Algorithm","tags":[],"scripts":[],"stylesheets":[]}'>
826-
<pre data-lang="js">// O(n log(n)) time | O(1) space where n = length of the array.
827-
function mergeSort(array) {
828-
const length = array.length;
829-
if (length &lt; 2) {
830-
return;
831-
}
832-
const mid = length / 2;
833-
const left = array.slice(0, mid);
834-
const right = array.slice(mid, length);
835-
mergeSort(left);
836-
mergeSort(right);
837-
stitch(left, right, array);
838-
}
839-
840-
function stitch(left, right, array) {
841-
let leftIdx = 0;
842-
let rightIdx = 0;
843-
let arrayIdx = 0;
844-
let leftLength = left.length;
845-
let rightLength = right.length;
846-
847-
while (leftIdx &lt; leftLength && rightIdx &lt; rightLength) {
848-
if (left[leftIdx] &lt;= right[rightIdx]) {
849-
array[arrayIdx] = left[leftIdx];
850-
leftIdx++;
851-
} else {
852-
array[arrayIdx] = right[rightIdx];
853-
rightIdx++;
854-
}
855-
arrayIdx++;
856-
}
815+
### Binary Search Tree Implementation
857816

858-
while (leftIdx &lt; leftLength) {
859-
array[arrayIdx] = left[leftIdx];
860-
leftIdx++;
861-
arrayIdx++;
862-
}
863-
while (rightIdx &lt; rightLength) {
864-
array[arrayIdx] = right[rightIdx];
865-
rightIdx++;
866-
arrayIdx++;
867-
}
868-
}</pre></div>
817+
![](https://i.imgur.com/QcEahdu.png)
869818

870819

871820

0 commit comments

Comments
 (0)