mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +08:00
Merge pull request #3 from PeterShershov/master
small refactor in some sorting algorithms for better readability
This commit is contained in:
commit
c8b3fb9983
@ -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,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