Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/lib/dsaAlgorithms.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,54 @@ export const generateInsertionSortSteps = (array) => {
return steps;
};

// Assumes non-negative integers
export const generateRadixSortSteps = (array) => {

const steps = [];
const arr = [...array];
const n = arr.length;

if (n <= 1) {
if (n === 1) steps.push({ type: 'sorted', indices: [0] });
return steps;
}

let max = arr[0];
for (let i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
}


for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) {
const buckets = Array.from({ length: 10 }, () => []);

// Distribute elements into buckets
for (let i = 0; i < n; i++) {
const digit = Math.floor(arr[i] / exp) % 10;
steps.push({ type: 'select', indices: [i] });
buckets[digit].push(arr[i]);
}

// Collect from buckets back into array
let idx = 0;
for (let d = 0; d < 10; d++) {
for (let val of buckets[d]) {
steps.push({ type: 'overwrite', indices: [idx], value: val });
arr[idx] = val;
idx++;
}
}
}

// Mark all as sorted
for (let i = 0; i < n; i++) {
steps.push({ type: 'sorted', indices: [i] });
}

return steps;
};


export const generateMergeSortSteps = (array) => {
const steps = [];
const arr = [...array];
Expand Down Expand Up @@ -189,6 +237,7 @@ function partition(arr, low, high, steps) {
}



// --- Searching Algorithms ---

export const generateLinearSearchSteps = (array, target) => {
Expand Down Expand Up @@ -424,4 +473,4 @@ export const generateAStarSteps = (numNodes, adj, startNode, endNode, nodes) =>
}

return steps;
}
}
2 changes: 1 addition & 1 deletion src/pages/DsaVisualization.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ const DsaVisualization = () => {
};

const algoOptions = {
sorting: ['BubbleSort', 'SelectionSort', 'InsertionSort', 'MergeSort', 'QuickSort'],
sorting: ['BubbleSort', 'SelectionSort', 'InsertionSort', 'MergeSort', 'QuickSort', 'RadixSort'],
searching: ['BinarySearch', 'LinearSearch'],
graphs: ['BFS', 'DFS', 'Dijkstra', 'AStar'],
linkedlist: [],
Expand Down