mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 23:21:18 +08:00
improve readability in some sorting algorithms
This commit is contained in:
parent
488b7a4c0e
commit
f2aebe7ccb
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user