Skip to content

Commit c8b3fb9

Browse files
authored
Merge pull request trekhleb#3 from PeterShershov/master
small refactor in some sorting algorithms for better readability
2 parents d596e1d + 297875e commit c8b3fb9

File tree

5 files changed

+5
-5
lines changed

5 files changed

+5
-5
lines changed

src/algorithms/sorting/bubble-sort/BubbleSort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class BubbleSort extends Sort {
55
// Flag that holds info about whether the swap has occur or not.
66
let swapped = false;
77
// Clone original array to prevent its modification.
8-
const array = originalArray.slice(0);
8+
const array = [...originalArray];
99

1010
for (let i = 0; i < array.length; i += 1) {
1111
swapped = false;

src/algorithms/sorting/insertion-sort/InsertionSort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Sort from '../Sort';
22

33
export default class InsertionSort extends Sort {
44
sort(originalArray) {
5-
const array = originalArray.slice(0);
5+
const array = [...originalArray];
66

77
// Go through all array elements...
88
for (let i = 0; i < array.length; i += 1) {

src/algorithms/sorting/quick-sort/QuickSort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Sort from '../Sort';
33
export default class QuickSort extends Sort {
44
sort(originalArray) {
55
// Clone original array to prevent it from modification.
6-
const array = originalArray.slice(0);
6+
const array = [...originalArray];
77

88
// If array has less then or equal to one elements then it is already sorted.
99
if (array.length <= 1) {

src/algorithms/sorting/selection-sort/SelectionSort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Sort from '../Sort';
33
export default class SelectionSort extends Sort {
44
sort(originalArray) {
55
// Clone original array to prevent its modification.
6-
const array = originalArray.slice(0);
6+
const array = [...originalArray];
77

88
for (let i = 0; i < array.length - 1; i += 1) {
99
let minIndex = i;

src/algorithms/sorting/shell-sort/ShellSort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Sort from '../Sort';
33
export default class ShellSort extends Sort {
44
sort(originalArray) {
55
// Prevent original array from mutations.
6-
const array = originalArray.slice(0);
6+
const array = [...originalArray];
77

88
// Define a gap distance.
99
let gap = Math.floor(array.length / 2);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy