Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ddf6ce7

Browse files
committed
add quicksort method, still need to add better heuristic for choosing pivot points
1 parent 74329cd commit ddf6ce7

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

‎dist/sorting/quickSort.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
function quickSort(arr, options = { direction: 'ascending' }, indexRange = [0, arr.length - 1]) {
4+
// TODO: add ability to pass cbFn as argument used to compare values, like js builtin sort
5+
const pivot = (arr, indexRange = [0, arr.length - 1]) => {
6+
const { direction } = options;
7+
const [startIndex, lastIndex] = indexRange;
8+
let matches = 0;
9+
let pivotIndex = startIndex; /* change pivotIndex selection to be more intentional,
10+
based on heuristics about the input array's median. */
11+
let pivotNum = arr[pivotIndex];
12+
for (let i = pivotIndex + 1; i <= lastIndex; i++) {
13+
if ((direction === 'ascending' && arr[i] < pivotNum)
14+
|| direction === 'descending' && arr[i] > pivotNum) {
15+
matches++;
16+
[arr[pivotIndex + matches], arr[i]] =
17+
[arr[i], arr[pivotIndex + matches]]; // swap matched num to be left of eventual pivot index
18+
}
19+
}
20+
[arr[pivotIndex], arr[pivotIndex + matches]] =
21+
[arr[pivotIndex + matches], arr[pivotIndex]]; // swap pivot into correct sorted position
22+
pivotIndex += matches;
23+
return pivotIndex;
24+
};
25+
let [left, right] = indexRange;
26+
let pivotIndex = pivot(arr, indexRange);
27+
if (pivotIndex - left > 1) {
28+
quickSort(arr, options, [left, pivotIndex - 1]);
29+
}
30+
if (right - pivotIndex > 1) {
31+
quickSort(arr, options, [pivotIndex + 1, right]);
32+
}
33+
return arr;
34+
}
35+
exports.default = quickSort;

‎dist/sorting/quickSort.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
const quickSort_1 = __importDefault(require("./quickSort"));
7+
let arr = [];
8+
beforeEach(() => arr = [5, 2, 1, 8, 4, 7, 6, 3]);
9+
describe('quicksort basic functionality', () => {
10+
test('quicksort exists', () => {
11+
expect(typeof quickSort_1.default).toBe('function');
12+
});
13+
test('quickSort returns same length array as it receives', () => {
14+
const originalLength = arr.length;
15+
expect(quickSort_1.default(arr).length).toBe(originalLength);
16+
});
17+
test('ascending: quicksort works on basic array', () => {
18+
quickSort_1.default(arr);
19+
expect(arr).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
20+
});
21+
test('descending: quicksort works on basic array', () => {
22+
quickSort_1.default(arr, { direction: 'descending' });
23+
expect(arr).toEqual([8, 7, 6, 5, 4, 3, 2, 1]);
24+
});
25+
});

‎src/sorting/quickSort.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import quickSort from './quickSort';
2+
let arr: number[] = [];
3+
beforeEach(() => arr = [5, 2, 1, 8, 4, 7, 6, 3])
4+
5+
describe('quicksort basic functionality', () => {
6+
test('quicksort exists', () => {
7+
expect(typeof quickSort).toBe('function');
8+
});
9+
test('quickSort returns same length array as it receives', () => {
10+
const originalLength = arr.length;
11+
expect(quickSort(arr).length).toBe(originalLength);
12+
});
13+
test('ascending: quicksort works on basic array', () => {
14+
quickSort(arr);
15+
expect(arr).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
16+
});
17+
test('descending: quicksort works on basic array', () => {
18+
quickSort(arr, { direction: 'descending' });
19+
expect(arr).toEqual([8, 7, 6, 5, 4, 3, 2, 1]);
20+
});
21+
})

‎src/sorting/quickSort.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import SortOptions from './SortOptions';
2+
3+
function quickSort<T>(arr: T[], options: SortOptions = { direction: 'ascending' }, indexRange: [number, number] = [0, arr.length - 1]): T[] {
4+
// TODO: add ability to pass cbFn as argument used to compare values, like js builtin sort
5+
const pivot = (arr: T[], indexRange: [number, number] = [0, arr.length - 1]): number => {
6+
const { direction } = options;
7+
const [startIndex, lastIndex] = indexRange;
8+
let matches = 0;
9+
let pivotIndex = startIndex; /* change pivotIndex selection to be more intentional,
10+
based on heuristics about the input array's median. */
11+
let pivotNum = arr[pivotIndex];
12+
for (let i = pivotIndex + 1; i <= lastIndex; i++) {
13+
if ((direction === 'ascending' && arr[i] < pivotNum)
14+
|| direction === 'descending' && arr[i] > pivotNum) {
15+
matches++;
16+
[arr[pivotIndex + matches], arr[i]] =
17+
[arr[i], arr[pivotIndex + matches]] // swap matched num to be left of eventual pivot index
18+
}
19+
}
20+
[arr[pivotIndex], arr[pivotIndex + matches]] =
21+
[arr[pivotIndex + matches], arr[pivotIndex]]; // swap pivot into correct sorted position
22+
pivotIndex += matches;
23+
return pivotIndex;
24+
}
25+
let [left, right] = indexRange;
26+
let pivotIndex = pivot(arr, indexRange);
27+
if (pivotIndex - left > 1) {
28+
quickSort(arr, options, [left, pivotIndex - 1]);
29+
}
30+
if (right - pivotIndex > 1) {
31+
quickSort(arr, options, [pivotIndex + 1, right]);
32+
}
33+
return arr;
34+
}
35+
36+
export default quickSort;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /