December-software-project/sort-algo

View on GitHub
src/visualizer/algorithm/stepbysteptemplate/bucketSortStepByStep.js

Summary

Maintainability
A
0 mins
Test Coverage
F
0%
import { radixSortStepByStep } from './radixSortStepByStep';
import { genericSwapStepByStep } from './genericSwapStepByStep';

/**
 * Generates the step by step template for Bucket Sort.
 *
 * @method
 * @memberOf StepByStepTemplate
 * @param {any[]} animationArr An array that contains all the animation steps.
 * @param {number} idx Index of the current animation step.
 * @param {Object[]} stackArr An array representing the buckets.
 * @returns {string} The information regarding the current step of the animation.
 */
export const bucketSortStepByStep = (animationArr, idx, stackArr) => {
  const currentAnimation = animationArr[idx - 1];

  // Insertion sort taking place in bucket
  if (currentAnimation.length === 4) {
    const location = currentAnimation[3];
    return genericSwapStepByStep(animationArr, idx, stackArr[location].array);
  }

  if (currentAnimation.isSort === undefined) {
    return radixSortStepByStep(animationArr, idx);
  }

  // Signal start of sorting bucket
  if (currentAnimation.isSort) {
    return `Currently sorting bucket ${currentAnimation.location}`;
  } else {
    return `Bucket ${currentAnimation.location} has been sorted!`;
  }
};