Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.md
*.jpg
*.png
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"env": {
"jest/globals": true
},
"parser": "@babel/eslint-parser",
"rules": {
"linebreak-style": 0,
"no-bitwise": "off",
"no-lonely-if": "off",
"class-methods-use-this": "off",
Expand Down
427 changes: 214 additions & 213 deletions README.md

Large diffs are not rendered by default.

165 changes: 85 additions & 80 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
"homepage": "https://github.com/trekhleb/javascript-algorithms#readme",
"devDependencies": {
"@babel/cli": "7.12.10",
"@babel/eslint-parser": "^7.15.0",
"@babel/preset-env": "7.12.11",
"@types/jest": "26.0.19",
"canvas": "^2.7.0",
"eslint": "7.16.0",
"eslint": "^7.16.0",
"eslint-config-airbnb": "18.2.1",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-jest": "24.1.3",
Expand All @@ -51,5 +52,8 @@
"engines": {
"node": ">=12.0.0",
"npm": ">=6.9.0"
},
"dependencies": {
"babel-eslint": "^10.1.0"
}
}
51 changes: 51 additions & 0 deletions src/algorithms/sorting/pancake-sort/PancakeSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Sort from '../Sort';

export default class PancakeSort extends Sort {
sort(originalArray) {
// Clone original array to prevent its modification.
const arr = [...originalArray];

let n = arr.length;

while (n > 1) {
let max = -Infinity;
let maxIndex = 0;
let k = n;

for (let i = 0; i < k; i += 1) {
if (this.comparator.greaterThanOrEqual(arr[i], max)) {
max = arr[i];
maxIndex = i;
}
}

this.callbacks.visitingCallback(arr[maxIndex]);

if (maxIndex !== n - 1) {
let i = 0;
k = maxIndex;
while (i < k) {
const temp = arr[k];
arr[k] = arr[i];
arr[i] = temp;
i += 1;
k -= 1;
}

i = 0;
k = n - 1;

while (i < k) {
const temp = arr[k];
arr[k] = arr[i];
arr[i] = temp;
i += 1;
k -= 1;
}
}
n -= 1;
}

return arr;
}
}
21 changes: 21 additions & 0 deletions src/algorithms/sorting/pancake-sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Pancake Sort

Pancake sorting is the mathematical problem of sorting a disordered stack of pancakes in order of size when a spatula can be inserted at any point in the stack and used to flip all pancakes above it. A pancake number is the minimum number of flips required for a given number of pancakes.

Following are the detailed steps. Let given array be arr[] and size of array be n.

- Start from current size equal to n and reduce current size by one while it’s greater than 1. Let the current size be curr_size. Do following for every curr_size
- Find index of the maximum element in arr [0..curr_szie-1]. Let the index be ‘mi’
- Call flip(arr, mi)
- Call flip(arr, curr_size-1)

## Complexity

| Name | Best | Average | Worst | Memory | Stable | Comments |
| ---------------- | :-----------: | :-----------: | :-----------: | :----: | :----: | :------- |
| **Pancake sort** | n<sup>2</sup> | n<sup>2</sup> | n<sup>2</sup> | 1 | No | |

## References

[Wikipedia](https://en.wikipedia.org/wiki/Pancake_sorting)
[GeeksforGeeks](https://www.geeksforgeeks.org/pancake-sorting)
60 changes: 60 additions & 0 deletions src/algorithms/sorting/pancake-sort/__test__/PancakeSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import PancakeSort from '../PancakeSort';
import {
equalArr,
notSortedArr,
reverseArr,
sortedArr,
SortTester,
} from '../../SortTester';

// Complexity constants.
const SORTED_ARRAY_VISITING_COUNT = 19;
const NOT_SORTED_ARRAY_VISITING_COUNT = 19;
const REVERSE_SORTED_ARRAY_VISITING_COUNT = 19;
const EQUAL_ARRAY_VISITING_COUNT = 19;

describe('PancakeSort', () => {
it('should sort array', () => {
SortTester.testSort(PancakeSort);
});

it('should sort array with custom comparator', () => {
SortTester.testSortWithCustomComparator(PancakeSort);
});

it('should sort negative numbers', () => {
SortTester.testNegativeNumbersSort(PancakeSort);
});

it('should visit EQUAL array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
PancakeSort,
equalArr,
EQUAL_ARRAY_VISITING_COUNT,
);
});

it('should visit SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
PancakeSort,
sortedArr,
SORTED_ARRAY_VISITING_COUNT,
);
});

it('should visit NOT SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
PancakeSort,
notSortedArr,
NOT_SORTED_ARRAY_VISITING_COUNT,
);
});

it('should visit REVERSE SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
PancakeSort,
reverseArr,
REVERSE_SORTED_ARRAY_VISITING_COUNT,
);
});
});