|
67 | 67 | - [ADT](#adt)
|
68 | 68 | - [Time Memory Trade-Off technique](#time-memory-trade-off-technique)
|
69 | 69 | - [Mandatory Algorithms](#mandatory-algorithms)
|
70 |
| - - [Binary Search Algorithm](#binary-search-algorithm) |
| 70 | + - [Binary Search on sorted Array Algorithm](#binary-search-on-sorted-array-algorithm) |
71 | 71 | - [Reverse Linked List Algorithm](#reverse-linked-list-algorithm)
|
72 | 72 | - [Merge Sort Algorithm](#merge-sort-algorithm)
|
| 73 | + - [Binary Search Tree Implementation](#binary-search-tree-implementation) |
73 | 74 | - [Coding Interview Question and Answers](#coding-interview-question-and-answers)
|
74 | 75 | - [Graphs](#graphs)
|
75 | 76 | - [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
|
776 | 777 |
|
777 | 778 | 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.
|
778 | 779 |
|
779 |
| - |
| 780 | + |
780 | 781 |
|
781 | 782 | ## Mandatory Algorithms
|
782 | 783 |
|
783 |
| -### Binary Search Algorithm |
| 784 | +### Binary Search on sorted Array Algorithm |
784 | 785 |
|
785 | 786 | 
|
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<=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 < 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> |
798 | 794 |
|
799 | 795 | ### Reverse Linked List Algorithm
|
800 | 796 |
|
801 | 797 | 
|
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 | 798 |
|
816 |
| - head = previous; |
817 | 799 |
|
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> |
820 | 805 |
|
821 | 806 | ### Merge Sort Algorithm
|
822 | 807 |
|
823 | 808 | 
|
| 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> |
824 | 814 |
|
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 < 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 < leftLength && rightIdx < rightLength) { |
848 |
| - if (left[leftIdx] <= 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 |
857 | 816 |
|
858 |
| - while (leftIdx < leftLength) { |
859 |
| - array[arrayIdx] = left[leftIdx]; |
860 |
| - leftIdx++; |
861 |
| - arrayIdx++; |
862 |
| - } |
863 |
| - while (rightIdx < rightLength) { |
864 |
| - array[arrayIdx] = right[rightIdx]; |
865 |
| - rightIdx++; |
866 |
| - arrayIdx++; |
867 |
| - } |
868 |
| -}</pre></div> |
| 817 | + |
869 | 818 |
|
870 | 819 |
|
871 | 820 |
|
|
0 commit comments