Merge pull request #3 from PeterShershov/master

small refactor in some sorting algorithms for better readability
This commit is contained in:
Oleksii Trekhleb 2018-05-24 09:07:08 +03:00 committed by GitHub
commit c8b3fb9983
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 5 additions and 5 deletions

View File

@ -5,7 +5,7 @@ export default class BubbleSort extends Sort {
// Flag that holds info about whether the swap has occur or not. // Flag that holds info about whether the swap has occur or not.
let swapped = false; let swapped = false;
// Clone original array to prevent its modification. // Clone original array to prevent its modification.
const array = originalArray.slice(0); const array = [...originalArray];
for (let i = 0; i < array.length; i += 1) { for (let i = 0; i < array.length; i += 1) {
swapped = false; swapped = false;

View File

@ -2,7 +2,7 @@ import Sort from '../Sort';
export default class InsertionSort extends Sort { export default class InsertionSort extends Sort {
sort(originalArray) { sort(originalArray) {
const array = originalArray.slice(0); const array = [...originalArray];
// Go through all array elements... // Go through all array elements...
for (let i = 0; i < array.length; i += 1) { for (let i = 0; i < array.length; i += 1) {

View File

@ -3,7 +3,7 @@ import Sort from '../Sort';
export default class QuickSort extends Sort { export default class QuickSort extends Sort {
sort(originalArray) { sort(originalArray) {
// Clone original array to prevent it from modification. // 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 has less then or equal to one elements then it is already sorted.
if (array.length <= 1) { if (array.length <= 1) {

View File

@ -3,7 +3,7 @@ import Sort from '../Sort';
export default class SelectionSort extends Sort { export default class SelectionSort extends Sort {
sort(originalArray) { sort(originalArray) {
// Clone original array to prevent its modification. // 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) { for (let i = 0; i < array.length - 1; i += 1) {
let minIndex = i; let minIndex = i;

View File

@ -3,7 +3,7 @@ import Sort from '../Sort';
export default class ShellSort extends Sort { export default class ShellSort extends Sort {
sort(originalArray) { sort(originalArray) {
// Prevent original array from mutations. // Prevent original array from mutations.
const array = originalArray.slice(0); const array = [...originalArray];
// Define a gap distance. // Define a gap distance.
let gap = Math.floor(array.length / 2); let gap = Math.floor(array.length / 2);