From 1176f2839b51b357a278bfa54b29695943a7934d Mon Sep 17 00:00:00 2001 From: AkshatPandey Date: Sat, 13 Jul 2024 00:21:11 +0530 Subject: [PATCH 1/4] Quick Sort Page --- Algorithm/Quicksort.html | 321 +++++++++++++++++++++++++++++++++ Algorithm/Quicksort.js | 377 +++++++++++++++++++++++++++++++++++++++ Algorithm/bubbles.css | 10 +- 3 files changed, 707 insertions(+), 1 deletion(-) create mode 100644 Algorithm/Quicksort.html create mode 100644 Algorithm/Quicksort.js diff --git a/Algorithm/Quicksort.html b/Algorithm/Quicksort.html new file mode 100644 index 0000000..a15c039 --- /dev/null +++ b/Algorithm/Quicksort.html @@ -0,0 +1,321 @@ + + + + + + + + + Quick Sort + + + + + +
+
+

Quick Sort

+
+
+
+

Quick Sort is an efficient, in-place sorting algorithm that in practice is faster than Merge Sort and Heap Sort. It is based on the divide-and-conquer approach. The key process in quicksort is partitioning, which arranges the elements of the array so that all elements less than the pivot precede the pivot, and all elements greater than the pivot follow it.

+
+

Algorithm:

+
+
+
+
+
+ Bubble Sort Visualization +
+ + + + + + + + + + + + + + + + + + + + + + + + +
Complexity
Best Time ComplexityO(n log n)
Average Time ComplexityO(n log n)
Worst Time ComplexityO(n2)
Space ComplexityO(log n)
+
+
+
+
+ + +
+ +
+ + + +
+
+

Quick Sort Visualizer

+
+
+
+
+ + + + + + + + +
+
+
+
+

+

+ + +
+
+
+
+ + +
+
+

Quick Sort Code

+ +
+
+
public void quickSort(int[] arr, int low, int high) {
+                        if (low < high) {
+                            int pi = partition(arr, low, high);
+                            quickSort(arr, low, pi-1);
+                            quickSort(arr, pi+1, high);
+                        }
+                    }
+                    
+                    int partition(int[] arr, int low, int high) {
+                        int pivot = arr[high];
+                        int i = (low-1);
+                        for (int j = low; j < high; j++) {
+                            if (arr[j] < pivot) {
+                                i++;
+                                int temp = arr[i];
+                                arr[i] = arr[j];
+                                arr[j] = temp;
+                            }
+                        }
+                        int temp = arr[i+1];
+                        arr[i+1] = arr[high];
+                        arr[high] = temp;
+                        return i+1;
+                    }
+
+
+
void quickSort(int arr[], int low, int high) {
+                        if (low < high) {
+                            int pi = partition(arr, low, high);
+                            quickSort(arr, low, pi - 1);
+                            quickSort(arr, pi + 1, high);
+                        }
+                    }
+                    
+                    int partition (int arr[], int low, int high) {
+                        int pivot = arr[high];
+                        int i = (low - 1);
+                        for (int j = low; j <= high - 1; j++) {
+                            if (arr[j] < pivot) {
+                                i++;
+                                swap(&arr[i], &arr[j]);
+                            }
+                        }
+                        swap(&arr[i + 1], &arr[high]);
+                        return (i + 1);
+                    }
+                    
+                    void swap(int* a, int* b) {
+                        int t = *a;
+                        *a = *b;
+                        *b = t;
+                    }
+
+
+
void quickSort(int arr[], int low, int high) {
+                        if (low < high) {
+                            int pi = partition(arr, low, high);
+                            quickSort(arr, low, pi - 1);
+                            quickSort(arr, pi + 1, high);
+                        }
+                    }
+                    
+                    int partition (int arr[], int low, int high) {
+                        int pivot = arr[high];
+                        int i = (low - 1);
+                        for (int j = low; j <= high - 1; j++) {
+                            if (arr[j] < pivot) {
+                                i++;
+                                swap(&arr[i], &arr[j]);
+                            }
+                        }
+                        swap(&arr[i + 1], &arr[high]);
+                        return (i + 1);
+                    }
+                    
+                    void swap(int* a, int* b) {
+                        int t = *a;
+                        *a = *b;
+                        *b = t;
+                    }
+
+
+
def quickSort(arr, low, high):
+                        if low < high:
+                            pi = partition(arr, low, high)
+                            quickSort(arr, low, pi-1)
+                            quickSort(arr, pi+1, high)
+                    
+                    def partition(arr, low, high):
+                        pivot = arr[high]
+                        i = low - 1
+                        for j in range(low, high):
+                            if arr[j] < pivot:
+                                i += 1
+                                arr[i], arr[j] = arr[j], arr[i]
+                        arr[i+1], arr[high] = arr[high], arr[i+1]
+                        return i + 1
+
+
+
+
+ + +
+
+

Practice Questions

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Question NumberQuestion TitleLevelLink
1Sort an ArrayEasyLink
2Kth Largest Element in an ArrayMediumLink
3Top K Frequent ElementsHardLink
+
+
+ + + + + + + + diff --git a/Algorithm/Quicksort.js b/Algorithm/Quicksort.js new file mode 100644 index 0000000..239cb0e --- /dev/null +++ b/Algorithm/Quicksort.js @@ -0,0 +1,377 @@ +const codeDisplay = document.querySelector('.tab-content .tab-pane.active pre code'); +const languageTabs = document.querySelectorAll('#languageTabs a'); +let array = []; +let stop = false; +const delayTime = 300; +const delay = ms => new Promise(res => setTimeout(res, ms)); + +// Language code snippets +const codeSnippets = { + java: `public void quickSort(int[] arr, int low, int high) { + if (low < high) { + int pi = partition(arr, low, high); + quickSort(arr, low, pi-1); + quickSort(arr, pi+1, high); + } +} + +int partition(int[] arr, int low, int high) { + int pivot = arr[high]; + int i = (low-1); + for (int j = low; j < high; j++) { + if (arr[j] < pivot) { + i++; + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + int temp = arr[i+1]; + arr[i+1] = arr[high]; + arr[high] = temp; + return i+1; +}`, + c: `void quickSort(int arr[], int low, int high) { + if (low < high) { + int pi = partition(arr, low, high); + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } +} + +int partition (int arr[], int low, int high) { + int pivot = arr[high]; + int i = (low - 1); + for (int j = low; j <= high - 1; j++) { + if (arr[j] < pivot) { + i++; + swap(&arr[i], &arr[j]); + } + } + swap(&arr[i + 1], &arr[high]); + return (i + 1); +} + +void swap(int* a, int* b) { + int t = *a; + *a = *b; + *b = t; +}`, + cpp: `void quickSort(int arr[], int low, int high) { + if (low < high) { + int pi = partition(arr, low, high); + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } +} + +int partition (int arr[], int low, int high) { + int pivot = arr[high]; + int i = (low - 1); + for (int j = low; j <= high - 1; j++) { + if (arr[j] < pivot) { + i++; + swap(&arr[i], &arr[j]); + } + } + swap(&arr[i + 1], &arr[high]); + return (i + 1); +} + +void swap(int* a, int* b) { + int t = *a; + *a = *b; + *b = t; +}`, + python: `def quickSort(arr, low, high): + if low < high: + pi = partition(arr, low, high) + quickSort(arr, low, pi-1) + quickSort(arr, pi+1, high) + +def partition(arr, low, high): + pivot = arr[high] + i = low - 1 + for j in range(low, high): + if arr[j] < pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] + arr[i+1], arr[high] = arr[high], arr[i+1] + return i + 1` +}; + +// Event listener for language tabs +languageTabs.forEach(tab => { + tab.addEventListener('click', event => { + const language = event.target.getAttribute('href').substring(1); + codeDisplay.innerText = codeSnippets[language]; + }); +}); + +// Set the initial code display content +codeDisplay.innerText = codeSnippets.java; + +// Function to submit array input +function submit() { + const input = document.getElementById("array").value; + array = input.split(" ").map(Number); + visualizeArray(array); +} + +// Function to visualize the array +function visualizeArray(arr) { + const container = document.getElementById("visualization"); + container.innerHTML = ""; + const maxVal = Math.max(...arr); + const containerWidth = container.offsetWidth; + const barWidth = Math.max(30, Math.min(100, containerWidth / arr.length - 2)); + + arr.forEach((val, idx) => { + const barContainer = document.createElement("div"); + barContainer.className = "bar-container"; + barContainer.style.width = `${barWidth}px`; + barContainer.style.left = `${idx * (barWidth + 2)}px`; + + const label = document.createElement("div"); + label.className = "bar-label"; + label.textContent = val; + + const bar = document.createElement("div"); + bar.className = "bar"; + bar.style.height = `${(val / maxVal) * 300}px`; + bar.style.width = `${barWidth}px`; + bar.id = `bar-${idx}`; + + barContainer.appendChild(label); + barContainer.appendChild(bar); + container.appendChild(barContainer); + }); +} + +// Function to update bars +async function updateBars() { + const maxVal = Math.max(...array); + array.forEach((val, idx) => { + const container = document.getElementById(`bar-${idx}`).parentElement; + const label = container.querySelector('.bar-label'); + const bar = container.querySelector('.bar'); + + label.textContent = val; + bar.style.height = `${(val / maxVal) * 300}px`; + }); + await delay(delayTime); +} + +// Function to swap array elements +async function swap(i, j) { + const temp = array[i]; + array[i] = array[j]; + array[j] = temp; + + const container1 = document.getElementById(`bar-${i}`).parentElement; + const container2 = document.getElementById(`bar-${j}`).parentElement; + + const tempLeft = container1.style.left; + container1.style.left = container2.style.left; + container2.style.left = tempLeft; + + container1.querySelector('.bar').id = `bar-${j}`; + container2.querySelector('.bar').id = `bar-${i}`; + + await updateBars(); +} + +async function quickSort(low = 0, high = array.length - 1) { + if (low < high) { + let pi = await partition(low, high); + + await quickSort(low, pi - 1); + await quickSort(pi + 1, high); + } + if (low === 0 && high === array.length - 1) { + for (let i = 0; i < array.length; i++) { + document.getElementById(`bar-${i}`).classList.add("sorted"); + } + } +} + +async function partition(low, high) { + let pivot = array[high]; + let i = low - 1; + + for (let j = low; j <= high - 1; j++) { + if (stop) return; + await highlightBars([j, high], "comparing"); + if (array[j] < pivot) { + i++; + await swap(i, j); + } + } + await swap(i + 1, high); + return i + 1; +} + +// Function to highlight bars +async function highlightBars(indices, className) { + indices.forEach(index => { + document.getElementById(`bar-${index}`).classList.remove("comparing", "sorted"); + if (className) { + document.getElementById(`bar-${index}`).classList.add(className); + } + }); + await delay(delayTime); +} + +// Function to update a single bar +async function updateSingleBar(index) { + const maxVal = Math.max(...array); + const container = document.getElementById(`bar-${index}`).parentElement; + const label = container.querySelector('.bar-label'); + const bar = container.querySelector('.bar'); + + label.textContent = array[index]; + bar.style.height = `${(array[index] / maxVal) * 300}px`; + await delay(delayTime); +} + +// Function to reset the visualization +function reset() { + stop = false; + visualizeArray(array); +} + +// Function to handle stop button click +function stopClicked() { + document.getElementById("resume").disabled = false; + document.getElementById("reset").disabled = false; +} + +// Functions to enable and disable buttons +function disableSubmitButton() { + document.getElementById("submit").disabled = true; + document.getElementById("start").disabled = true; + document.getElementById("resume").disabled = true; + document.getElementById("reset").disabled = true; +} + +function enableSubmitButton() { + document.getElementById("submit").disabled = false; + document.getElementById("start").disabled = false; + document.getElementById("resume").disabled = false; + document.getElementById("reset").disabled = false; +} + +// Function to start the sorting +async function startSort() { + disableSubmitButton(); + reset(); + const sortMethod = document.getElementById("sortSelect").value; + switch(sortMethod) { + // case "bubble": + // await bubbleSort(); + // break; + // Add other sorting algorithms here + // case "selection": + // await selectionSort(); + // break; + // case "insertion": + // await insertionSort(); + // break; + // case "merge": + // await mergeSortWrapper(); + // break; + // case "heap": + // await heapSort(); + // break; + // case "comb": + // await combSort(); + // break; + case "quick": + await quickSort(); + break; + } + enableSubmitButton(); +} + +// Function to show a step in the tour +function showStep(step) { + const tourPopup = document.getElementById("tourPopup"); + const targetElement = document.getElementById(tourSteps[step].target); + const targetRect = targetElement.getBoundingClientRect(); + + let top = targetRect.bottom + 10; + let left = targetRect.left + targetRect.width / 2 - 150; + + if (left < 10) left = 10; + if (left + 300 > window.innerWidth) left = window.innerWidth - 310; + + if (top + 200 > window.innerHeight) { + top = targetRect.top - 210; + if (top < 10) { + top = 10; + } + } + + top = Math.max(10, Math.min(top, window.innerHeight - 210)); + + tourPopup.style.left = `${left}px`; + tourPopup.style.top = `${top}px`; + + document.getElementById("tourTitle").textContent = tourSteps[step].title; + document.getElementById("tourDescription").textContent = tourSteps[step].description; + + if (step === tourSteps.length - 1) { + document.getElementById("tourNext").textContent = "Finish"; + } else { + document.getElementById("tourNext").textContent = "Next"; + } + + targetElement.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'nearest' }); +} + +// Event listener for tour next button +document.getElementById("tourNext").addEventListener("click", () => { + currentStep++; + if (currentStep < tourSteps.length) { + showStep(currentStep); + } else { + document.getElementById("tourOverlay").style.display = "none"; + currentStep = 0; + } +}); + +// Start the tour when the page loads +window.addEventListener("load", function() { + loader.style.display = "none"; + startTour(); +}); + +// Event listener for tour skip button +document.getElementById("tourSkip").addEventListener("click", () => { + document.getElementById("tourOverlay").style.display = "none"; + currentStep = 0; +}); + +// Loader +var loader = document.getElementById("Loader"); +window.addEventListener("load", function() { + loader.style.display = "none"; +}); + +const description = document.querySelector('.bubble-sort-description'); + +const bubbleSortAlgorithm = ` +1. Choose a pivot element from the array (commonly the last element). +2. Partition the array into two sub-arrays: + a. Elements less than the pivot. + b. Elements greater than the pivot. +3. Move the pivot element to its correct position in the sorted array. +4. Recursively apply steps 1-3 to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values. +5. Repeat until the base case is reached, where the sub-array has less than two elements. +6. Combine the sub-arrays and the pivot to form the sorted array. +`; + +description.querySelector('pre code.algorithm').innerText = bubbleSortAlgorithm; +$(document).ready(function() { + $('.dropdown-toggle').dropdown(); + }); \ No newline at end of file diff --git a/Algorithm/bubbles.css b/Algorithm/bubbles.css index d67bdbf..cc25f2f 100644 --- a/Algorithm/bubbles.css +++ b/Algorithm/bubbles.css @@ -1004,4 +1004,12 @@ button:hover { color: rgb(219, 127, 219); transition: all 0.3s ease; } - \ No newline at end of file + + .footer a { + color: white; +} + +.footer .btn-secondary { + background-color: white; + color: rgb(202, 111, 202); +} \ No newline at end of file From 2a9599413fb3c80505f6f64a876a6093447cd95c Mon Sep 17 00:00:00 2001 From: AkshatPandey Date: Sat, 13 Jul 2024 01:45:54 +0530 Subject: [PATCH 2/4] merge sort --- Algorithm/Mergesort.html | 363 +++++++++++++++++++++++++++++++ Algorithm/Mergesort.js | 458 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 821 insertions(+) create mode 100644 Algorithm/Mergesort.html create mode 100644 Algorithm/Mergesort.js diff --git a/Algorithm/Mergesort.html b/Algorithm/Mergesort.html new file mode 100644 index 0000000..523082f --- /dev/null +++ b/Algorithm/Mergesort.html @@ -0,0 +1,363 @@ + + + + + + + + + Merge Sort + + + + + +
+
+

Merge Sort

+
+
+
+

Merge Sort is a divide-and-conquer algorithm that splits the array into halves, recursively sorts each half, and then merges the sorted halves to produce the sorted array. It is known for its efficiency in handling large data sets, with a time complexity of O(n log n) in all cases.

+
+

Algorithm:

+
+
+
+
+
+ Bubble Sort Visualization +
+ + + + + + + + + + + + + + + + + + + + + + + + +
Complexity
Best Time ComplexityO(n log n)
Average Time ComplexityO(n log n)
Worst Time ComplexityO(n log n)
Space ComplexityO(n)
+
+
+
+
+ + +
+ +
+ + + +
+
+

Merge Sort Visualizer

+
+
+
+
+ + + + + + + + +
+
+
+
+

+

+ + +
+
+
+
+ + +
+
+

Merge Sort Code

+ +
+
+
public void mergeSort(int[] arr, int left, int right) {
+                        if (left < right) {
+                            int mid = (left + right) / 2;
+                            mergeSort(arr, left, mid);
+                            mergeSort(arr, mid + 1, right);
+                            merge(arr, left, mid, right);
+                        }
+                    }
+                    
+                    public void merge(int[] arr, int left, int mid, int right) {
+                        int n1 = mid - left + 1;
+                        int n2 = right - mid;
+                        int[] leftArr = new int[n1];
+                        int[] rightArr = new int[n2];
+                        System.arraycopy(arr, left, leftArr, 0, n1);
+                        System.arraycopy(arr, mid + 1, rightArr, 0, n2);
+                        int i = 0, j = 0, k = left;
+                        while (i < n1 && j < n2) {
+                            if (leftArr[i] <= rightArr[j]) {
+                                arr[k++] = leftArr[i++];
+                            } else {
+                                arr[k++] = rightArr[j++];
+                            }
+                        }
+                        while (i < n1) {
+                            arr[k++] = leftArr[i++];
+                        }
+                        while (j < n2) {
+                            arr[k++] = rightArr[j++];
+                        }
+                    }
+
+
+
void mergeSort(int arr[], int left, int right) {
+                        if (left < right) {
+                            int mid = left + (right - left) / 2;
+                            mergeSort(arr, left, mid);
+                            mergeSort(arr, mid + 1, right);
+                            merge(arr, left, mid, right);
+                        }
+                    }
+                    
+                    void merge(int arr[], int left, int mid, int right) {
+                        int n1 = mid - left + 1;
+                        int n2 = right - mid;
+                        int L[n1], R[n2];
+                        for (int i = 0; i < n1; i++)
+                            L[i] = arr[left + i];
+                        for (int j = 0; j < n2; j++)
+                            R[j] = arr[mid + 1 + j];
+                        int i = 0, j = 0, k = left;
+                        while (i < n1 && j < n2) {
+                            if (L[i] <= R[j]) {
+                                arr[k] = L[i];
+                                i++;
+                            } else {
+                                arr[k] = R[j];
+                                j++;
+                            }
+                            k++;
+                        }
+                        while (i < n1) {
+                            arr[k] = L[i];
+                            i++;
+                            k++;
+                        }
+                        while (j < n2) {
+                            arr[k] = R[j];
+                            j++;
+                            k++;
+                        }
+                    }
+
+
+
void mergeSort(vector<int>& arr, int left, int right) {
+                        if (left < right) {
+                            int mid = left + (right - left) / 2;
+                            mergeSort(arr, left, mid);
+                            mergeSort(arr, mid + 1, right);
+                            merge(arr, left, mid, right);
+                        }
+                    }
+                    
+                    void merge(vector<int>& arr, int left, int mid, int right) {
+                        int n1 = mid - left + 1;
+                        int n2 = right - mid;
+                        vector<int> L(n1), R(n2);
+                        for (int i = 0; i < n1; i++)
+                            L[i] = arr[left + i];
+                        for (int j = 0; j < n2; j++)
+                            R[j] = arr[mid + 1 + j];
+                        int i = 0, j = 0, k = left;
+                        while (i < n1 && j < n2) {
+                            if (L[i] <= R[j]) {
+                                arr[k] = L[i];
+                                i++;
+                            } else {
+                                arr[k] = R[j];
+                                j++;
+                            }
+                            k++;
+                        }
+                        while (i < n1) {
+                            arr[k] = L[i];
+                            i++;
+                            k++;
+                        }
+                        while (j < n2) {
+                            arr[k] = R[j];
+                            j++;
+                            k++;
+                        }
+                    }
+
+
+
def merge_sort(arr):
+                        if len(arr) > 1:
+                            mid = len(arr) // 2
+                            left_half = arr[:mid]
+                            right_half = arr[mid:]
+                            merge_sort(left_half)
+                            merge_sort(right_half)
+                            i = j = k = 0
+                            while i < len(left_half) and j < len(right_half):
+                                if left_half[i] < right_half[j]:
+                                    arr[k] = left_half[i]
+                                    i += 1
+                                else:
+                                    arr[k] = right_half[j]
+                                    j += 1
+                                k += 1
+                            while i < len(left_half):
+                                arr[k] = left_half[i]
+                                i += 1
+                                k += 1
+                            while j < len(right_half):
+                                arr[k] = right_half[j]
+                                j += 1
+                                k += 1
+
+
+
+
+ + +
+
+

Practice Questions

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Question NumberQuestion TitleLevelLink
1Sort an ArrayEasyLink
2Sort ListMediumLink
3Merge k Sorted ListsHardLink
+
+
+ + + + + + + + diff --git a/Algorithm/Mergesort.js b/Algorithm/Mergesort.js new file mode 100644 index 0000000..d72bb37 --- /dev/null +++ b/Algorithm/Mergesort.js @@ -0,0 +1,458 @@ +const codeDisplay = document.querySelector('.tab-content .tab-pane.active pre code'); +const languageTabs = document.querySelectorAll('#languageTabs a'); +let array = []; +let stop = false; +const delayTime = 300; +const delay = ms => new Promise(res => setTimeout(res, ms)); + +// Language code snippets +const codeSnippets = { + java: `public void mergeSort(int[] arr, int left, int right) { + if (left < right) { + int mid = (left + right) / 2; + mergeSort(arr, left, mid); + mergeSort(arr, mid + 1, right); + merge(arr, left, mid, right); + } +} + +public void merge(int[] arr, int left, int mid, int right) { + int n1 = mid - left + 1; + int n2 = right - mid; + int[] leftArr = new int[n1]; + int[] rightArr = new int[n2]; + System.arraycopy(arr, left, leftArr, 0, n1); + System.arraycopy(arr, mid + 1, rightArr, 0, n2); + int i = 0, j = 0, k = left; + while (i < n1 && j < n2) { + if (leftArr[i] <= rightArr[j]) { + arr[k++] = leftArr[i++]; + } else { + arr[k++] = rightArr[j++]; + } + } + while (i < n1) { + arr[k++] = leftArr[i++]; + } + while (j < n2) { + arr[k++] = rightArr[j++]; + } +}`, + c: `void mergeSort(int arr[], int left, int right) { + if (left < right) { + int mid = left + (right - left) / 2; + mergeSort(arr, left, mid); + mergeSort(arr, mid + 1, right); + merge(arr, left, mid, right); + } +} + +void merge(int arr[], int left, int mid, int right) { + int n1 = mid - left + 1; + int n2 = right - mid; + int L[n1], R[n2]; + for (int i = 0; i < n1; i++) + L[i] = arr[left + i]; + for (int j = 0; j < n2; j++) + R[j] = arr[mid + 1 + j]; + int i = 0, j = 0, k = left; + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k] = L[i]; + i++; + } else { + arr[k] = R[j]; + j++; + } + k++; + } + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } +}`, + cpp: `void mergeSort(vector<int>& arr, int left, int right) { + if (left < right) { + int mid = left + (right - left) / 2; + mergeSort(arr, left, mid); + mergeSort(arr, mid + 1, right); + merge(arr, left, mid, right); + } +} + +void merge(vector<int>& arr, int left, int mid, int right) { + int n1 = mid - left + 1; + int n2 = right - mid; + vector<int> L(n1), R(n2); + for (int i = 0; i < n1; i++) + L[i] = arr[left + i]; + for (int j = 0; j < n2; j++) + R[j] = arr[mid + 1 + j]; + int i = 0, j = 0, k = left; + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k] = L[i]; + i++; + } else { + arr[k] = R[j]; + j++; + } + k++; + } + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } +}`, + python: `def merge_sort(arr): + if len(arr) > 1: + mid = len(arr) // 2 + left_half = arr[:mid] + right_half = arr[mid:] + merge_sort(left_half) + merge_sort(right_half) + i = j = k = 0 + while i < len(left_half) and j < len(right_half): + if left_half[i] < right_half[j]: + arr[k] = left_half[i] + i += 1 + else: + arr[k] = right_half[j] + j += 1 + k += 1 + while i < len(left_half): + arr[k] = left_half[i] + i += 1 + k += 1 + while j < len(right_half): + arr[k] = right_half[j] + j += 1 + k += 1` +}; + +// Event listener for language tabs +languageTabs.forEach(tab => { + tab.addEventListener('click', event => { + const language = event.target.getAttribute('href').substring(1); + codeDisplay.innerText = codeSnippets[language]; + }); +}); + +// Set the initial code display content +codeDisplay.innerText = codeSnippets.java; + +// Function to submit array input +function submit() { + const input = document.getElementById("array").value; + array = input.split(" ").map(Number); + visualizeArray(array); +} + +// Function to visualize the array +function visualizeArray(arr) { + const container = document.getElementById("visualization"); + container.innerHTML = ""; + const maxVal = Math.max(...arr); + const containerWidth = container.offsetWidth; + const barWidth = Math.max(30, Math.min(100, containerWidth / arr.length - 2)); + + arr.forEach((val, idx) => { + const barContainer = document.createElement("div"); + barContainer.className = "bar-container"; + barContainer.style.width = `${barWidth}px`; + barContainer.style.left = `${idx * (barWidth + 2)}px`; + + const label = document.createElement("div"); + label.className = "bar-label"; + label.textContent = val; + + const bar = document.createElement("div"); + bar.className = "bar"; + bar.style.height = `${(val / maxVal) * 300}px`; + bar.style.width = `${barWidth}px`; + bar.id = `bar-${idx}`; + + barContainer.appendChild(label); + barContainer.appendChild(bar); + container.appendChild(barContainer); + }); +} + +// Function to update bars +async function updateBars() { + const maxVal = Math.max(...array); + array.forEach((val, idx) => { + const container = document.getElementById(`bar-${idx}`).parentElement; + const label = container.querySelector('.bar-label'); + const bar = container.querySelector('.bar'); + + label.textContent = val; + bar.style.height = `${(val / maxVal) * 300}px`; + }); + await delay(delayTime); +} + +// Function to swap array elements +async function swap(i, j) { + const temp = array[i]; + array[i] = array[j]; + array[j] = temp; + + const container1 = document.getElementById(`bar-${i}`).parentElement; + const container2 = document.getElementById(`bar-${j}`).parentElement; + + const tempLeft = container1.style.left; + container1.style.left = container2.style.left; + container2.style.left = tempLeft; + + container1.querySelector('.bar').id = `bar-${j}`; + container2.querySelector('.bar').id = `bar-${i}`; + + await updateBars(); +} + +async function mergeSortWrapper() { + await mergeSort(0, array.length - 1); +} + +async function mergeSort(left, right) { + if (left < right) { + const middle = Math.floor((left + right) / 2); + + await mergeSort(left, middle); + await mergeSort(middle + 1, right); + await merge(left, middle, right); + } +} + +async function merge(left, middle, right) { + const leftArray = array.slice(left, middle + 1); + const rightArray = array.slice(middle + 1, right + 1); + let i = 0, j = 0, k = left; + + while (i < leftArray.length && j < rightArray.length) { + if (stop) return; + + await highlightBars([left + i, middle + 1 + j], "comparing"); + await delay(delayTime); + + if (leftArray[i] <= rightArray[j]) { + array[k] = leftArray[i]; + i++; + } else { + array[k] = rightArray[j]; + j++; + } + await updateSingleBar(k); + k++; + } + + while (i < leftArray.length) { + if (stop) return; + array[k] = leftArray[i]; + await updateSingleBar(k); + i++; + k++; + } + + while (j < rightArray.length) { + if (stop) return; + array[k] = rightArray[j]; + await updateSingleBar(k); + j++; + k++; + } + + for (let i = left; i <= right; i++) { + await highlightBars([i], "sorted"); + } +} + +async function heapSort() { + let n = array.length; + + for (let i = Math.floor(n / 2) - 1; i >= 0; i--) + await heapify(n, i); + + for (let i = n - 1; i > 0; i--) { + if (stop) return; + await swap(0, i); + await heapify(i, 0); + document.getElementById(`bar-${i}`).classList.add("sorted"); + } + document.getElementById(`bar-0`).classList.add("sorted"); +} + +// Function to highlight bars +async function highlightBars(indices, className) { + indices.forEach(index => { + document.getElementById(`bar-${index}`).classList.remove("comparing", "sorted"); + if (className) { + document.getElementById(`bar-${index}`).classList.add(className); + } + }); + await delay(delayTime); +} + +// Function to update a single bar +async function updateSingleBar(index) { + const maxVal = Math.max(...array); + const container = document.getElementById(`bar-${index}`).parentElement; + const label = container.querySelector('.bar-label'); + const bar = container.querySelector('.bar'); + + label.textContent = array[index]; + bar.style.height = `${(array[index] / maxVal) * 300}px`; + await delay(delayTime); +} + +// Function to reset the visualization +function reset() { + stop = false; + visualizeArray(array); +} + +// Function to handle stop button click +function stopClicked() { + document.getElementById("resume").disabled = false; + document.getElementById("reset").disabled = false; +} + +// Functions to enable and disable buttons +function disableSubmitButton() { + document.getElementById("submit").disabled = true; + document.getElementById("start").disabled = true; + document.getElementById("resume").disabled = true; + document.getElementById("reset").disabled = true; +} + +function enableSubmitButton() { + document.getElementById("submit").disabled = false; + document.getElementById("start").disabled = false; + document.getElementById("resume").disabled = false; + document.getElementById("reset").disabled = false; +} + +// Function to start the sorting +async function startSort() { + disableSubmitButton(); + reset(); + const sortMethod = document.getElementById("sortSelect").value; + switch(sortMethod) { + // case "bubble": + // await bubbleSort(); + // break; + // Add other sorting algorithms here + // case "selection": + // await selectionSort(); + // break; + // case "insertion": + // await insertionSort(); + // break; + case "merge": + await mergeSortWrapper(); + break; + // case "heap": + // await heapSort(); + // break; + // case "comb": + // await combSort(); + // break; + // case "quick": + // await quickSort(); + // break; + } + enableSubmitButton(); +} + +// Function to show a step in the tour +function showStep(step) { + const tourPopup = document.getElementById("tourPopup"); + const targetElement = document.getElementById(tourSteps[step].target); + const targetRect = targetElement.getBoundingClientRect(); + + let top = targetRect.bottom + 10; + let left = targetRect.left + targetRect.width / 2 - 150; + + if (left < 10) left = 10; + if (left + 300 > window.innerWidth) left = window.innerWidth - 310; + + if (top + 200 > window.innerHeight) { + top = targetRect.top - 210; + if (top < 10) { + top = 10; + } + } + + top = Math.max(10, Math.min(top, window.innerHeight - 210)); + + tourPopup.style.left = `${left}px`; + tourPopup.style.top = `${top}px`; + + document.getElementById("tourTitle").textContent = tourSteps[step].title; + document.getElementById("tourDescription").textContent = tourSteps[step].description; + + if (step === tourSteps.length - 1) { + document.getElementById("tourNext").textContent = "Finish"; + } else { + document.getElementById("tourNext").textContent = "Next"; + } + + targetElement.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'nearest' }); +} + +// Event listener for tour next button +document.getElementById("tourNext").addEventListener("click", () => { + currentStep++; + if (currentStep < tourSteps.length) { + showStep(currentStep); + } else { + document.getElementById("tourOverlay").style.display = "none"; + currentStep = 0; + } +}); + +// Start the tour when the page loads +window.addEventListener("load", function() { + loader.style.display = "none"; + startTour(); +}); + +// Event listener for tour skip button +document.getElementById("tourSkip").addEventListener("click", () => { + document.getElementById("tourOverlay").style.display = "none"; + currentStep = 0; +}); + +// Loader +var loader = document.getElementById("Loader"); +window.addEventListener("load", function() { + loader.style.display = "none"; +}); + +const description = document.querySelector('.bubble-sort-description'); + +const bubbleSortAlgorithm = ` +1. Divide the array into two halves. +2. Recursively sort each half. +3. Merge the two sorted halves into a single sorted array. +4. Continue this process until the entire array is sorted. +5. Merge the sorted halves to get the final sorted array. +`; + +description.querySelector('pre code.algorithm').innerText = bubbleSortAlgorithm; +$(document).ready(function() { + $('.dropdown-toggle').dropdown(); + }); \ No newline at end of file From 1d6bae76d0416365340cc3bbb2c5b116731c7cf1 Mon Sep 17 00:00:00 2001 From: AkshatPandey Date: Sat, 13 Jul 2024 01:49:26 +0530 Subject: [PATCH 3/4] minor changes --- login.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/login.html b/login.html index 45d269d..841dd6b 100644 --- a/login.html +++ b/login.html @@ -21,7 +21,7 @@

Visual Sort|Login

- Not signed up? Sign In + Not signed up? Sign In

From c3eda9135ea0c5e9677acac0079f5e567d0595fc Mon Sep 17 00:00:00 2001 From: AkshatPandey Date: Sat, 13 Jul 2024 02:00:39 +0530 Subject: [PATCH 4/4] changes done --- Algorithm/Bubble.html | 24 ++++++++++++------------ Algorithm/CombSort.html | 0 Algorithm/Heapsort.html | 0 Algorithm/Insertionsort.html | 0 Algorithm/Selectionsort.html | 0 index.html | 24 ++++++++++++------------ 6 files changed, 24 insertions(+), 24 deletions(-) create mode 100644 Algorithm/CombSort.html create mode 100644 Algorithm/Heapsort.html create mode 100644 Algorithm/Insertionsort.html create mode 100644 Algorithm/Selectionsort.html diff --git a/Algorithm/Bubble.html b/Algorithm/Bubble.html index 0e4af1f..ecbd8ec 100644 --- a/Algorithm/Bubble.html +++ b/Algorithm/Bubble.html @@ -62,12 +62,12 @@