Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit dbbeff6

Browse files
committed
add 算法
1 parent b5a4c8c commit dbbeff6

File tree

5 files changed

+42
-41
lines changed

5 files changed

+42
-41
lines changed

算法与数据结构学习/algorithms/sort/quick-sort/quick-sort.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ function quickSort(arr, low, high) {
88

99
function adjustArr(arr, low, high) {
1010
let pivot = arr[low];
11-
1211
while (low < high) {
1312
while (arr[high] > pivot)
1413
high--;
@@ -24,4 +23,18 @@ function adjustArr(arr, low, high) {
2423

2524
let arr = [72,6,57,88,60,42,83,73,48,85];
2625
quickSort(arr, 0, arr.length-1);
27-
console.log(arr);
26+
console.log(arr);
27+
28+
function quickSort(arr) {
29+
const pivot = arr[0];
30+
const left = [];
31+
const right = [];
32+
33+
if (arr.length < 2) { return arr; }
34+
35+
for (let i = 1, len = arr.length; i < len; i++) {
36+
arr[i] < pivot ? left.push(arr[i]) : right.push(arr[i]);
37+
}
38+
39+
return quickSort(left).concat([pivot], quickSort(right));
40+
}
Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
1-
function quickSort(arr, low, high) {
2-
if (low < high) {
3-
// 调整数组
4-
let i = adjustArr(arr, low, high);
5-
// 分治
6-
quickSort(arr, low, i-1);
7-
quickSort(arr, i+1, high);
8-
}
9-
}
10-
11-
function adjustArr(arr, low, high) {
12-
let pivot = arr[low];
1+
var myPow = function(x, n) {
2+
return n > 0 ? quickMul(x, n) : 1.0 / quickMul(x, -n);
3+
};
134

14-
while (low < high) {
15-
while (low < high && arr[high] > pivot)
16-
high--;
17-
arr[low] = arr[high];
18-
while (low < high && arr[low] < pivot)
19-
low++;
20-
arr[high] = arr[low];
5+
var quickMul = function(x, N) {
6+
if (N == 0) {
7+
return 1.0
218
}
22-
23-
arr[low] = pivot;
24-
return low;
9+
let y = quickMul(x, Math.floor());
10+
return N % 2 == 0 ? y * y : y * y * x;
2511
}
2612

27-
let arr = [72,6,57,88,60,42,83,73,48,85];
28-
quickSort(arr, 0, arr.length-1);
29-
console.log(arr);
13+
console.log(myPow(2, 10));
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
import HashTable from './HashTable';
1+
// let k1 = [1,2,3];
2+
// let k2 = [3,4,5];
3+
// let k3 = [3,4,5];
4+
// let arr = [[k1, 'foo'], [k2, 'bar']];
25

3-
let hashTable = new HashTable();
6+
// let keys = arr.map(item => item[0]);
47

5-
hashTable.set('a', 'sky-old');
6-
hashTable.set('a', 'sky');
7-
hashTable.set('b', 'sea');
8-
hashTable.set('c', 'earth');
9-
hashTable.set('d', 'ocean');
8+
// console.log();
109

11-
hashTable.buckets[1].toString();
12-
console.log(hashTable.getKeys())
13-
console.log(hashTable.has);
1410

11+
// console.log(keys.indexOf(k3));
12+
13+
let arr = [1,2,3];
14+
let obj = {
15+
arr: arr
16+
}
17+
18+
console.log(obj.arr);

算法与数据结构学习/data-structures/heap/Heap.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
export default class Heap {
22
constructor(params) {
3-
43
// 不能直接通过 new 来创建堆
54
if (new.target === Heap) {
65
throw new TypeError('Cannot construct Heap instance directly');
76
}
8-
97
this.heapContainer = [];
10-
118
}
129

13-
1410
/**
1511
* 获取左孩子的下标
1612
*/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const p = Promise.resolve(1);
2+
3+
p.then(console.log)
4+
p.then(console.log)

0 commit comments

Comments
 (0)