diff --git a/index.html b/index.html index 3ff6a77..6b72629 100644 --- a/index.html +++ b/index.html @@ -432,7 +432,15 @@

Bubble Sort

compares adjacent elements, and swaps them if they are out of order.

- +
+
+ Image +
+
+

Radix Sort

+

Radix Sort is a non-comparative sorting algorithm that sorts numbers by processing individual digits.

+
+
Image @@ -493,15 +501,7 @@

Heap Sort

Heap sort is a comparison-based sorting algorithm that builds a max heap from the input data and repeatedly extracts the maximum element to sort the array.

-
-
- Image -
-
-

Radix Sort

-

Radix Sort is a non-comparative sorting algorithm that sorts numbers by processing individual digits.

-
-
+
Image diff --git a/visual.html b/visual.html index 4e2c143..18262c9 100644 --- a/visual.html +++ b/visual.html @@ -327,6 +327,7 @@

Sorting Algorithms Visualization

+ @@ -625,6 +626,49 @@

await swap(i + 1, high); return i + 1; } + function getMax(arr) { + let max = arr[0]; + for (let i = 1; i < arr.length; i++) { + if (arr[i] > max) { + max = arr[i]; + } + } + return max; +} + +// Function to count sort based on the digit represented by exp +async function countSort(arr, exp) { + let output = new Array(arr.length); + let count = new Array(10).fill(0); + + for (let i = 0; i < arr.length; i++) { + count[Math.floor(arr[i] / exp) % 10]++; + } + + for (let i = 1; i < 10; i++) { + count[i] += count[i - 1]; + } + + for (let i = arr.length - 1; i >= 0; i--) { + output[count[Math.floor(arr[i] / exp) % 10] - 1] = arr[i]; + count[Math.floor(arr[i] / exp) % 10]--; + } + + for (let i = 0; i < arr.length; i++) { + arr[i] = output[i]; + await updateSingleBar(i); + } +} + +// Function to implement Radix Sort +async function radixSort() { + let m = getMax(array); + + for (let exp = 1; Math.floor(m / exp) > 0; exp *= 10) { + await countSort(array, exp); + if (stop) return; + } +} async function highlightBars(indices, className) { indices.forEach(index => { @@ -697,6 +741,9 @@

case "quick": await quickSort(); break; + case "radix": + await radixSort(); + break; } enableSubmitButton(); }