Skip to content

Commit 01f7fa2

Browse files
committed
up reference quick sort implement - review and manual implement later!
1 parent 3684926 commit 01f7fa2

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

src/algorithm/sorting/merge-sort.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
22

3+
34
export function mergeSort(array: number[]): number[] {
4-
// Stop point
5+
// Stop point (case)
56
if (array.length <= 1) {
67
return array
78
}
@@ -11,10 +12,13 @@ export function mergeSort(array: number[]): number[] {
1112
const middle = Math.floor(length / 2);
1213
const left = array.slice(0, middle)
1314
const right = array.slice(middle)
15+
console.log("left", left);
16+
console.log("right", right)
1417

15-
// Recursive point
18+
// Recursive point (recursive case)
19+
// Mỗi lần merge thì mất n
1620
return merge(
17-
mergeSort(left),
21+
mergeSort(left), // Chia đôi chiều dài của mảng để mảng đạt 1 phần tử tốn logN thời gian (logN lần lặp => Ví dụ mảng có 256 phần tử => Tốn log256 => tốn 8 lần chạy của hàm )
1822
mergeSort(right)
1923
)
2024
}
@@ -40,6 +44,7 @@ export function merge(left: number[], right: number[]) {
4044
rightIndex++;
4145
}
4246
}
47+
console.log(left, right);
4348

4449
return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
4550
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, it, expect } from "bun:test";
2+
import { quickSort } from "./quick-sort";
3+
4+
describe('Quick Sort Function', () => {
5+
it('should sort an unsorted array', () => {
6+
const unsortedArray = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
7+
const result = quickSort(unsortedArray, 0, unsortedArray.length - 1);
8+
expect(result).toEqual([0, 1, 2, 4, 5, 6, 44, 63, 87, 99, 283]);
9+
});
10+
11+
it('should handle an already sorted array', () => {
12+
const sortedArray = [0, 1, 2, 4, 5, 6, 44, 63, 87, 99, 283];
13+
const result = quickSort(sortedArray, 0, sortedArray.length - 1);
14+
expect(result).toEqual(sortedArray);
15+
});
16+
17+
it('should handle an empty array', () => {
18+
const result = quickSort([], 0, -1);
19+
expect(result).toEqual([]);
20+
});
21+
22+
it('should handle an array with one element', () => {
23+
const result = quickSort([42], 0, 0);
24+
expect(result).toEqual([42]);
25+
});
26+
27+
it('should handle an array with duplicate elements', () => {
28+
const result = quickSort([3, 1, 2, 3, 3, 0], 0, 5);
29+
expect(result).toEqual([0, 1, 2, 3, 3, 3]);
30+
});
31+
32+
it('should handle an array with negative numbers', () => {
33+
const result = quickSort([-3, -1, 0, -2, 1, 2], 0, 5);
34+
expect(result).toEqual([-3, -2, -1, 0, 1, 2]);
35+
});
36+
});

src/algorithm/sorting/quick-sort.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
2+
// Chọn pivot 99
3+
// Lần 1: 99 với 0 => swap 0 với 99 => [0, 99, 44, 6, 2, 1, 5, 63, 87, 283, 4]
4+
// Lần 2: 99 với 4 => swap 4 với 99 => [0, 4, 99, 44, 6, 2, 1, 5, 63, 87, 283]
5+
// Lần 3: 99 với 283 => Không thể swap
6+
// Lần 4: 99 với 87 => Swap 87 với 99 => [0, 4, 87, 99, 44, 6, 2, 1, 5, 63, 283]
7+
// Lần 5: 99 với 63 => Swap 63 với 99 => [0, 4, 87, 63, 99, 44, 6, 2, 1, 5, 283]
8+
// ...
9+
// Lần thứ n: 99 với .. => Swap với 99 => [0, 4, 87, 63, 5, 99, 44, 6, 2, 1, 283]
10+
// Lần thứ n: 99 với .. => Swap với 99 => [0, 4, 87, 63, 5, 1, 99, 44, 6, 2, 283]
11+
// Lần thứ n: 99 với .. => Swap với 99 => [0, 4, 87, 63, 5, 1, 2, 99, 44, 6, 283]
12+
// Lần thứ n: 99 với .. => Swap với 99 => [0, 4, 87, 63, 5, 1, 2, 6, 44, 99 , 283]
13+
14+
15+
export function quickSort(array: number[], left: number, right: number) {
16+
// Đây chỉ là tham khảo thôi!
17+
let pivot;
18+
let partitionIndex: number;
19+
20+
if (left < right) {
21+
pivot = right;
22+
partitionIndex = partition(array, pivot, left, right);
23+
24+
//sort left and right
25+
quickSort(array, left, partitionIndex - 1);
26+
quickSort(array, partitionIndex + 1, right);
27+
}
28+
return array;
29+
30+
}
31+
32+
function partition(array: number[], pivot: number, left: number, right: number): number {
33+
let pivotValue = array[pivot];
34+
let partitionIndex = left;
35+
36+
for (let i = left; i < right; i++) {
37+
if (array[i] < pivotValue) {
38+
swap(array, i, partitionIndex);
39+
partitionIndex++;
40+
}
41+
}
42+
swap(array, right, partitionIndex);
43+
return partitionIndex;
44+
}
45+
46+
function swap(array: number[], firstIndex: number, secondIndex: number) {
47+
var temp = array[firstIndex];
48+
array[firstIndex] = array[secondIndex];
49+
array[secondIndex] = temp;
50+
}
51+
52+
//Select first and last index as 2nd and 3rd parameters
53+
quickSort(numbers, 0, numbers.length - 1);
54+
console.log(numbers);

0 commit comments

Comments
 (0)