improve readability in some sorting algorithms

This commit is contained in:
Peter Shershov 2018-05-23 23:57:32 +03:00
parent 488b7a4c0e
commit f2aebe7ccb
No known key found for this signature in database
GPG Key ID: 1C9BCC58B6BE05DB
5 changed files with 4 additions and 4269 deletions

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);

4265
yarn.lock

File diff suppressed because it is too large Load Diff