mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +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 {
|
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) {
|
||||||
|
@ -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) {
|
||||||
|
@ -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;
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user