-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c1f150
commit d5fdbbc
Showing
2 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 先进后出 | ||
class Stack { | ||
constructor() { | ||
this.items = []; | ||
} | ||
push(element) { | ||
this.items.push(element); | ||
} | ||
pop() { | ||
this.items.pop(); | ||
} | ||
// 只返回不移除 | ||
peek() { | ||
return this.items[this.items.length - 1]; | ||
} | ||
isEmpty() { | ||
return this.items.length === 0; | ||
} | ||
clear() { | ||
this.items = []; | ||
} | ||
size() { | ||
return this.items.length; | ||
} | ||
} | ||
|
||
// const stack = new Stack(); | ||
// stack.push(1); | ||
// stack.push(2); | ||
// stack.pop(); | ||
// stack.push(3); | ||
// stack.peek(); | ||
// stack.size(); | ||
// console.log(stack.size()); | ||
|
||
// 先进先出 | ||
// class Queue { | ||
// constructor() { | ||
// this.list = []; | ||
// this.fIndex = 0; | ||
// this.tIndex = this.list.length - 1; | ||
// } | ||
// enqueue() { | ||
// this.list[this.tIndex++] = item; | ||
// } | ||
// unqueue() { | ||
// const item = this.list[this.fIndex]; | ||
// this.fIndex++; | ||
// return item; | ||
// } | ||
// } | ||
|
||
class Queue { | ||
constructor(size) { | ||
this.size = size + 1; // 长度需要限制, 来达到空间的利用, 代表空间的长度 | ||
this.list = []; | ||
this.font = 0; // 指向首元素 | ||
this.rear = 0; // 指向准备插入元素的位置 | ||
} | ||
enQueue(value) { | ||
if (this.isFull() == true) { | ||
return false; | ||
} | ||
this.list[this.rear] = value; | ||
this.rear = (this.rear + 1) % this.size; | ||
return true; | ||
} | ||
deQueue() { | ||
if (this.isEmpty()) { | ||
return false; | ||
} | ||
this.font = (this.font + 1) % this.size; | ||
return true; | ||
} | ||
isEmpty() { | ||
return this.font === this.rear; | ||
} | ||
isFull() { | ||
return (this.rear + 1) % this.size === this.font; | ||
} | ||
toString() { | ||
console.log(this.list.slice(this.font, this.rear)); | ||
} | ||
} | ||
|
||
const queue = new Queue(5); | ||
queue.enQueue(1); | ||
queue.enQueue(3); | ||
queue.toString(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/** | ||
* 冒泡排序:两两比较,每次都交换 | ||
* @param {*} arr | ||
* @returns | ||
*/ | ||
function bubbleSort(arr) { | ||
const len = arr.length; | ||
for (let i = 0; i < len - 1; i++) { | ||
for (let j = 0, stop = len - 1 - i; j < stop; j++) { | ||
if (arr[j] > arr[j + 1]) { | ||
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; | ||
} | ||
} | ||
} | ||
return arr; | ||
} | ||
|
||
let arr = [1, 5, 4, 3, 6, 8]; | ||
console.log("原数组", arr); | ||
bubbleSort(arr); | ||
console.log("冒泡排序:", arr); | ||
console.log("---"); | ||
|
||
/** | ||
* 选择排序:两两比较,找出最大(小)值,最后交换 | ||
* @param {*} arr | ||
* @returns | ||
*/ | ||
function selectionSort(arr) { | ||
let len = arr.length, | ||
min; | ||
for (let i = 0; i < len; i++) { | ||
// 设置当前位置为最小值 | ||
min = i; | ||
//检查其余部分是否有更小的 | ||
for (let j = i + 1; j < len; j++) { | ||
if (arr[j] < arr[min]) { | ||
min = j; | ||
} | ||
} | ||
// 如果当前位置不是最小的就进行交换 | ||
if (i != min) { | ||
[arr[i], arr[min]] = [arr[min], arr[i]]; | ||
} | ||
} | ||
return arr; | ||
} | ||
|
||
let arr1 = [1, 5, 4, 3, 6, 8]; | ||
console.log("原数组", arr1); | ||
selectionSort(arr1); | ||
console.log("选择排序:", arr1); | ||
console.log("---"); | ||
|
||
function insertionSort(arr) { | ||
let len = arr.length, | ||
value, | ||
j; | ||
for (let i = 0; i < len; i++) { | ||
value = arr[i]; | ||
// 已排序元素当前元素大于value,就将当前元素向后移动 | ||
for (j = i - 1; j >= 0 && arr[j] > value; j--) { | ||
arr[j + 1] = arr[j]; | ||
} | ||
arr[j + 1] = value; | ||
} | ||
return arr; | ||
} | ||
const arr2 = [1, 5, 4, 3, 6, 8]; | ||
console.log("原数组", arr2); | ||
insertionSort(arr2); | ||
console.log("插入排序:", arr2); | ||
console.log("---"); | ||
|
||
/** | ||
* 归并排序:先分后合 | ||
* @param {*} arr | ||
* @returns | ||
*/ | ||
function mergeSort(arr) { | ||
if (arr.length < 2) return arr; | ||
let midddle = parseInt(arr.length / 2); | ||
const left = arr.slice(0, midddle); | ||
const right = arr.slice(midddle); | ||
return merge(mergeSort(left), mergeSort(right)); | ||
} | ||
const merge = (left, right) => { | ||
let result = []; | ||
//两两比较数组开头元素 | ||
while (left.length && right.length) { | ||
left[0] <= right[0] | ||
? result.push(left.shift()) | ||
: result.push(right.shift()); | ||
} | ||
// 将剩余元素放入结果中 | ||
while (left.length) result.push(left.shift()); | ||
while (right.length) result.push(right.shift()); | ||
return result; | ||
}; | ||
|
||
let arr3 = [1, 5, 4, 3, 6, 8]; | ||
console.log("原数组", arr3); | ||
arr3 = mergeSort(arr3); | ||
console.log("归并排序:", arr3); | ||
console.log("---"); | ||
|
||
/** | ||
* 快速排序 | ||
* @param {*} arr | ||
* @returns | ||
*/ | ||
function quicksort(arr) { | ||
if (arr.length <= 1) return arr; | ||
let pivot = Math.floor((arr.length - 1) / 2); | ||
let val = arr[pivot], | ||
less = [], | ||
more = []; | ||
arr.splice(pivot, 1); | ||
// 小的放左边,大的放右边 | ||
arr.forEach((e) => { | ||
e < val ? less.push(e) : more.push(e); | ||
}); | ||
return quicksort(less).concat([val], quicksort(more)); | ||
} | ||
|
||
let arr4 = [1, 5, 4, 3, 6, 8]; | ||
console.log("原数组", arr4); | ||
arr4 = quicksort(arr4); | ||
console.log("快速排序:", arr4); |