diff --git a/index/src/pages/Algorithms/algorithms.js b/index/src/pages/Algorithms/algorithms.js deleted file mode 100644 index 9730e1f..0000000 --- a/index/src/pages/Algorithms/algorithms.js +++ /dev/null @@ -1,555 +0,0 @@ -/*********** VISUALIZATION HELPER FUNCTIONS ***********/ -var display = document.getElementById("arrtable"); -var comments = document.getElementById("comments"); -var comments2 = document.getElementById("comments2"); - -function printArrayVisual(arr) { - let table = document.createElement("table"); - let tablerow = document.createElement("tr"); - - display.appendChild(table).appendChild(tablerow); - for(let i=0; i left && i < right) tablecell.classList.add("bbold"); - if(i >= left && i <= swap1 && left != null) tablecell.classList.add("corange"); - - } - - await sleep(2000); -} - -async function visualizeHeapify(arr, sorted, swap1 = -1, swap2 = -1, isEnd = false) { - - let sleepTime = (swap1 < 0 && swap2 < 0) ? 1000 : 2500; - display.style.textAlign = "center"; - - let tree = document.createElement("div"); - tree.style.padding = "50px"; - - let depth = 1; - let spacer = 16; - let spacerBetween = 256; - let nextBreak = Math.pow(2, depth); - - - for(let i=0; i sorted-1 && sorted != 0) { - node.style.fontWeight = "bold"; - node.style.color = "black"; - } - - if(i == nextBreak) { - node.style.paddingRight = "0px"; - tree.appendChild(br); - ++depth; - spacer /= 2; - spacerBetween /= 2; - nextBreak = nextBreak + Math.pow(2, depth); - } - - } - display.appendChild(tree); - - await sleep(sleepTime); - if(!isEnd) removeAllChildren("arrtable"); -} - -function addSpace(node, spaces, after) { - - let space = ""; - for(let i=0; i setTimeout(resolve, ms)); -} - -function trimArray(arr, start, end) { - - let arrcopy = new Array((end+1)-start); - let j = 0; - - for(let i=start; i<=end; i++) { - arrcopy[j] = arr[i]; - j++; - } - return arrcopy; -} - - -/*********** SORT ALGORITHMS ***********/ - -//In-place sorting algorithms - -//1. Bubble Sort -async function bubbleSort(arr) { - - while(true) { - let swaps = 0; - - for(let i=0; i=0 && arr[j] > position) { - await tablify(arr, -1, j+1, i); - let swap = arr[j]; - arr[j] = arr[j+1]; - arr[j+1] = swap; - await tablify(arr, -1, j, i); - j = j-1; - loops++; - } - arr[j+1] = position; - if(loops == 0) { - let msg = document.createElement("p"); - msg.innerHTML = "   " + arr[i] + " already in place"; - display.appendChild(msg); - } - - } - removeAllChildren("comments"); - tablify(arr, -1, -1, arr.length); - return arr; -} - - -//4. Shell Sort -function shellSort(arr) { - //var start = performance.now(); - - let gaps = [5, 3, 1]; - - gaps.forEach(gap => { - for(let position=0; position < gap; position++) { - for(let i=position; i < arr.length; i+=gap) { - let swap = arr[i]; - let j=i; - for(j; j >= gap && arr[j-gap] > swap; j-=gap) { - arr[j] = arr[j-gap]; - } - arr[j] = swap; - //console.log(arr); - } - } - }); - //var end = performance.now(); - //console.log(end-start); - return arr; -} - - -//5. Comb Sort -function combSort(arr) { - - let gap = arr.length; - let isSorted = false; - const shrinkFactor = 1.3; - - while(!isSorted) { - gap = Math.floor(gap/shrinkFactor); - if(gap <= 1) { - gap = 1; - isSorted = true; - } - - let i=0; - while(i+gap < arr.length) { - if(arr[i] > arr[i+gap]) { - let swap = arr[i]; - arr[i] = arr[i+gap]; - arr[i+gap] = swap; - isSorted = false; - //console.log(arr); - } - i+=1; - } - } - return arr; -} - - -//6. Quick Sort -//using Lomuto partition -async function quickSort(arr, left = 0, right = arr.length-1) { - - if(left >= right || left < 0) return; - let pivot = await partition(arr, left, right); - - removeAllChildren("comments2"); - let msg = document.createElement("span"); - msg.innerHTML = "Sorting left arr in bold brackets. " //+ arr[left] + " to " + arr[pivot-1]; - //comments2.appendChild(msg); - await quickSort(arr, left, pivot-1); - - removeAllChildren("comments2"); - let msg2 = document.createElement("p"); - msg2.innerHTML = "Sorting right arr in bold brackets. " //+ arr[pivot+1] + " to " + arr[right]; - //comments2.appendChild(msg2); - await quickSort(arr, pivot+1, right); - - removeAllChildren("comments2"); - removeAllChildren("comments"); - await tablifyQuickSort(arr); - return arr; - -} - -async function partition(arr, left, right) { - - let pivot = arr[right]; - let i = left-1; - - removeAllChildren("comments"); - let pivotText = document.createElement("span"); - let br = document.createElement("br"); - let sorting = document.createElement("span") - sorting.innerHTML = "Selecting elements less than pivot..."; - pivotText.innerHTML = "Pivot = " + pivot; - comments.appendChild(pivotText); - comments.appendChild(br); - comments.appendChild(sorting); - - for(let j=left; j=0; i--) { - await heapify(arr, n, i); - } - let heapBuilt = document.createElement("div"); - heapBuilt.innerHTML = "Heap built"; - removeAllChildren("comments"); - comments.appendChild(heapBuilt); - - for(let i=n-1; i>0; i--) { - - let swapRoot = document.createElement("div"); - removeAllChildren("comments"); - swapRoot.innerHTML = "Inserting root node to position..."; - - comments.appendChild(swapRoot); - await visualizeHeapify(arr, i+1, 0, i); - let swap = arr[0]; - arr[0] = arr[i]; - arr[i] = swap; - await visualizeHeapify(arr, i); - removeAllChildren("comments"); - - await heapify(arr, i, 0); - await visualizeHeapify(arr, i); - } - - visualizeHeapify(arr, 1, -1, -1, true); - removeAllChildren("comments"); - - let msg = document.createElement("p"); - msg.innerHTML = "Final array: "; - display.appendChild(msg); - printArrayVisual(arr); - return arr; -} - -async function heapify(arr, n, i) { - - let buildingHeap = document.createElement("div"); - buildingHeap.innerHTML = "Building heap..."; - removeAllChildren("comments"); - comments.appendChild(buildingHeap); - - let largest = i, left = 2*i+1, right = 2*i+2; - - if(left < n && arr[left] > arr[largest]) { - largest = left; - } - - if(right < n && arr[right] > arr[largest]) { - largest = right; - } - - if(largest != i) { - await visualizeHeapify(arr, n, largest, i); - - let swap = arr[i]; - arr[i] = arr[largest]; - arr[largest] = swap; - await visualizeHeapify(arr, n); - await heapify(arr, n, largest); - } - - await visualizeHeapify(arr, n); - -} - - - -//Not-in place sorting - -//8. Merge Sort -function mergeSort(arr, left=0, right=arr.length-1) { - - if(left