Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshatPandey-2004 committed Jul 16, 2024
1 parent 8129d95 commit 5eb5e39
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
20 changes: 10 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,15 @@ <h3><a href="visual.html">Bubble Sort</a></h3>
compares adjacent elements, and swaps them if they are out of order. </p>
</div>
</div>

<div class="course bg-white h-100 align-self-stretch">
<figure class="m-0">
<a href="visual.html"><img src="images/radix-sort.webp" alt="Image" class="img-fluid"></a>
</figure>
<div class="course-inner-text py-4 px-4">
<h3><a href="visual.html">Radix Sort</a></h3>
<p>Radix Sort is a non-comparative sorting algorithm that sorts numbers by processing individual digits. </p>
</div>
</div>
<div class="course bg-white h-100 align-self-stretch">
<figure class="m-0">
<a href="visual.html"><img src="images/merge.png" alt="Image" class="img-fluid"></a>
Expand Down Expand Up @@ -493,15 +501,7 @@ <h3><a href="visual.html">Heap Sort</a></h3>
<p>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.
</div>
</div>
<div class="course bg-white h-100 align-self-stretch">
<figure class="m-0">
<a href="visual.html"><img src="images/radix-sort.webp" alt="Image" class="img-fluid"></a>
</figure>
<div class="course-inner-text py-4 px-4">
<h3><a href="visual.html">Radix Sort</a></h3>
<p>Radix Sort is a non-comparative sorting algorithm that sorts numbers by processing individual digits. </p>
</div>
</div>

<div class="course bg-white h-100 align-self-stretch">
<figure class="m-0">
<a href="Compare.html"><img src="images/compare.png" alt="Image" class="img-fluid"></a>
Expand Down
47 changes: 47 additions & 0 deletions visual.html
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ <h1 id="heading">Sorting Algorithms Visualization</h1>
<option value="merge">Merge Sort</option>
<option value="heap">Heap Sort</option>
<option value="quick">Quick Sort</option>
<option value="radix">Radix Sort</option>
</select>
<button id="start" class="btn-primary" onclick="startSort()">Start Sort</button>
<button class="btn-primary" onclick="stop = true; stopClicked()">Stop</button>
Expand Down Expand Up @@ -625,6 +626,49 @@ <h3 id="tourTitle"></h3>
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 => {
Expand Down Expand Up @@ -697,6 +741,9 @@ <h3 id="tourTitle"></h3>
case "quick":
await quickSort();
break;
case "radix":
await radixSort();
break;
}
enableSubmitButton();
}
Expand Down

0 comments on commit 5eb5e39

Please sign in to comment.