mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Merge branch 'master' into improve-bubble-sort
This commit is contained in:
@@ -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 = 1; i < array.length; i += 1) {
|
||||
swapped = false;
|
||||
@@ -18,7 +18,7 @@ export default class BubbleSort extends Sort {
|
||||
this.callbacks.visitingCallback(array[j]);
|
||||
|
||||
// Swap elements if they are in wrong order.
|
||||
if (this.comparator.lessThen(array[j + 1], array[j])) {
|
||||
if (this.comparator.lessThan(array[j + 1], array[j])) {
|
||||
const tmp = array[j + 1];
|
||||
array[j + 1] = array[j];
|
||||
array[j] = tmp;
|
||||
|
||||
@@ -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) {
|
||||
@@ -15,7 +15,7 @@ export default class InsertionSort extends Sort {
|
||||
// If this is the case then swap that elements.
|
||||
while (
|
||||
array[currentIndex - 1] &&
|
||||
this.comparator.lessThen(array[currentIndex], array[currentIndex - 1])
|
||||
this.comparator.lessThan(array[currentIndex], array[currentIndex - 1])
|
||||
) {
|
||||
// Call visiting callback.
|
||||
this.callbacks.visitingCallback(array[currentIndex - 1]);
|
||||
|
||||
@@ -31,7 +31,7 @@ export default class MergeSort extends Sort {
|
||||
let minimumElement = null;
|
||||
|
||||
// Find minimum element of two arrays.
|
||||
if (this.comparator.lessThenOrEqual(leftArray[0], rightArray[0])) {
|
||||
if (this.comparator.lessThanOrEqual(leftArray[0], rightArray[0])) {
|
||||
minimumElement = leftArray.shift();
|
||||
} else {
|
||||
minimumElement = rightArray.shift();
|
||||
|
||||
@@ -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) {
|
||||
@@ -27,7 +27,7 @@ export default class QuickSort extends Sort {
|
||||
|
||||
if (this.comparator.equal(currentElement, pivotElement)) {
|
||||
centerArray.push(currentElement);
|
||||
} else if (this.comparator.lessThen(currentElement, pivotElement)) {
|
||||
} else if (this.comparator.lessThan(currentElement, pivotElement)) {
|
||||
leftArray.push(currentElement);
|
||||
} else {
|
||||
rightArray.push(currentElement);
|
||||
|
||||
@@ -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;
|
||||
@@ -16,7 +16,7 @@ export default class SelectionSort extends Sort {
|
||||
// Call visiting callback.
|
||||
this.callbacks.visitingCallback(array[j]);
|
||||
|
||||
if (this.comparator.lessThen(array[j], array[minIndex])) {
|
||||
if (this.comparator.lessThan(array[j], array[minIndex])) {
|
||||
minIndex = j;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
@@ -20,7 +20,7 @@ export default class ShellSort extends Sort {
|
||||
this.callbacks.visitingCallback(array[currentIndex]);
|
||||
|
||||
// Compare and swap array elements if needed.
|
||||
if (this.comparator.lessThen(array[gapShiftedIndex], array[currentIndex])) {
|
||||
if (this.comparator.lessThan(array[gapShiftedIndex], array[currentIndex])) {
|
||||
const tmp = array[currentIndex];
|
||||
array[currentIndex] = array[gapShiftedIndex];
|
||||
array[gapShiftedIndex] = tmp;
|
||||
|
||||
Reference in New Issue
Block a user