Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small refactor in some sorting algorithms for better readability #3

Merged
merged 3 commits into from
May 24, 2018
Merged
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
2 changes: 1 addition & 1 deletion src/algorithms/sorting/bubble-sort/BubbleSort.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ export default class BubbleSort extends Sort {
// Flag that holds info about whether the swap has occur or not.
let swapped = false;
// Clone original array to prevent its modification.
const array = originalArray.slice(0);
const array = [...originalArray];

for (let i = 0; i < array.length; i += 1) {
swapped = false;
2 changes: 1 addition & 1 deletion src/algorithms/sorting/insertion-sort/InsertionSort.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import Sort from '../Sort';

export default class InsertionSort extends Sort {
sort(originalArray) {
const array = originalArray.slice(0);
const array = [...originalArray];

// Go through all array elements...
for (let i = 0; i < array.length; i += 1) {
2 changes: 1 addition & 1 deletion src/algorithms/sorting/quick-sort/QuickSort.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import Sort from '../Sort';
export default class QuickSort extends Sort {
sort(originalArray) {
// Clone original array to prevent it from modification.
const array = originalArray.slice(0);
const array = [...originalArray];

// If array has less then or equal to one elements then it is already sorted.
if (array.length <= 1) {
2 changes: 1 addition & 1 deletion src/algorithms/sorting/selection-sort/SelectionSort.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import Sort from '../Sort';
export default class SelectionSort extends Sort {
sort(originalArray) {
// Clone original array to prevent its modification.
const array = originalArray.slice(0);
const array = [...originalArray];

for (let i = 0; i < array.length - 1; i += 1) {
let minIndex = i;
2 changes: 1 addition & 1 deletion src/algorithms/sorting/shell-sort/ShellSort.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import Sort from '../Sort';
export default class ShellSort extends Sort {
sort(originalArray) {
// Prevent original array from mutations.
const array = originalArray.slice(0);
const array = [...originalArray];

// Define a gap distance.
let gap = Math.floor(array.length / 2);