-
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
zhangbin3
committed
Sep 9, 2020
1 parent
0c6d405
commit 135d3a6
Showing
28 changed files
with
1,299 additions
and
2 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,27 @@ | ||
// 冒泡排序 | ||
// 1.它是最慢的排序算法之一,数据值会像气泡一样从数组的一端漂浮到另一端 | ||
|
||
|
||
function BArr() { | ||
this.dataStore = [10, 8, 3, 2, 9, 4, 5, 7] | ||
this.swap = swap // 交换 | ||
this.bubbleSort = bubbleSort | ||
} | ||
|
||
function swap(arr, index1, index2) { | ||
let temp = arr[index1] | ||
arr[index1] = arr[index2] | ||
arr[index2] = temp | ||
} | ||
|
||
function bubbleSort() { | ||
let data = this.dataStore | ||
let len = data.length | ||
for (let outer = len; outer >= 2; --outer) { | ||
for (let inner = 0; inner <= outer - 1; inner++) { | ||
if (data[inner] > data[inner + 1]) { | ||
this.swap(this.dataStore, inner, inner + 1) | ||
} | ||
} | ||
} | ||
} |
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,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>冒泡排序</title> | ||
</head> | ||
<body> | ||
<script src="./bubble.js"></script> | ||
|
||
<script> | ||
// test | ||
|
||
let nums = new BArr() | ||
nums.bubbleSort(nums) | ||
console.log(nums.dataStore) | ||
</script> | ||
</body> | ||
</html> |
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,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>选择排序</title> | ||
</head> | ||
<body> | ||
<script src="./select.js"></script> | ||
|
||
<script> | ||
// test | ||
|
||
let s = new SArr() | ||
s.selectSort() | ||
console.log(s.dataStore) | ||
</script> | ||
</body> | ||
</html> |
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,29 @@ | ||
// 选择排序 | ||
// 1.从数组的开头开始,将第一个元素和其他元素比较,最小的元素会被放到数组的第一个位置,再从第二个位置继续 | ||
|
||
|
||
function SArr() { | ||
this.dataStore = [1, 8, 3, 2, 9, 5, 4, 7] | ||
this.swap = swap | ||
this.selectSort = selectSort | ||
} | ||
|
||
function swap(arr, index1, index2) { | ||
let temp = arr[index1] | ||
arr[index1] = arr[index2] | ||
arr[index2] = temp | ||
} | ||
|
||
function selectSort() { | ||
let min | ||
for (let outer = 0; outer < this.dataStore.length - 2; ++outer) { | ||
min = outer | ||
for (let inner = outer + 1; inner <= this.dataStore.length - 1; ++inner) { | ||
if (this.dataStore[inner] < this.dataStore[min]) { | ||
min = inner | ||
} | ||
} | ||
this.swap(this.dataStore, outer, min) | ||
console.log(this.dataStore, outer, min) | ||
} | ||
} |
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,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>插入排序</title> | ||
</head> | ||
<body> | ||
<script src="./insert.js"></script> | ||
|
||
<script> | ||
// test | ||
|
||
let i = new IArr() | ||
i.insertSort() | ||
console.log(i.dataStore) | ||
</script> | ||
</body> | ||
</html> |
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,22 @@ | ||
// 插入排序 | ||
// 1.类似于人们按数字或字母顺序对数据进行排序,后面的要为前面的腾位置 | ||
|
||
function IArr() { | ||
this.dataStore = [10, 3, 6, 2, 4, 8, 5] | ||
this.insertSort = insertSort | ||
} | ||
|
||
function insertSort() { | ||
let temp, inner | ||
for (let outer = 1; outer < this.dataStore.length; ++outer) { | ||
temp = this.dataStore[outer] | ||
inner = outer | ||
while (inner > 0 && (this.dataStore[inner - 1] >= temp)) { | ||
this.dataStore[inner] = this.dataStore[inner - 1] | ||
console.log('内部数据', this.dataStore) | ||
inner-- | ||
} | ||
this.dataStore[inner] = temp | ||
console.log('数据', this.dataStore) | ||
} | ||
} |
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,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>希尔排序</title> | ||
</head> | ||
<body> | ||
<script src="./sheel.js"></script> | ||
|
||
<script> | ||
// test | ||
|
||
let s = new SArr() | ||
s.shellSort() | ||
console.log(s.dataStore) | ||
</script> | ||
</body> | ||
</html> |
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,24 @@ | ||
// 希尔排序 | ||
// 1.它会首先比较较远的元素而非相邻的元素。 | ||
// 2.让元素尽快回到正确的位置。 | ||
// 3.通过定义一个间隔序列来表示在排序过程中进行比较的元素间隔 | ||
|
||
function SArr() { | ||
this.dataStore = [10, 8, 3, 2, 5, 9, 7, 35, 47, 50] | ||
this.shellSort = shellSort | ||
this.gags = [5, 3, 1] | ||
} | ||
|
||
function shellSort() { | ||
for (let g = 0; g < this.gags.length; g++) { | ||
for (let i = this.gags[g]; i < this.dataStore.length; i++) { | ||
let temp = this.dataStore[i] | ||
let j | ||
for (j = i; (j >= this.gags[g]) && (this.dataStore[j - this.gags[g]] > temp); j -= this.gags[g]) { | ||
this.dataStore[j] = this.dataStore[j - this.gags[g]] | ||
} | ||
this.dataStore[j] = temp | ||
} | ||
console.log('diaohuanhou', this.dataStore) | ||
} | ||
} |
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,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>归并排序</title> | ||
</head> | ||
<body> | ||
<script src="./merge.js"></script> | ||
|
||
<script> | ||
// test | ||
|
||
let arr = [23, 43, 45, 5, 56, 661, 2, 4] | ||
mergeSort(arr) | ||
console.log(arr) | ||
</script> | ||
</body> | ||
</html> |
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,50 @@ | ||
// 归并排序 | ||
// 1.把一系列 排好序 的 子序列 合并成一个大的完整有序序列 | ||
|
||
// 该方法比较难懂 | ||
function mergeSort(arr) { | ||
if (arr.length < 2) return | ||
let step = 1 | ||
let left, right | ||
while (step < arr.length) { | ||
left = 0 | ||
right = step | ||
while (right + step <= arr.length) { | ||
mergeArr(arr, left, left + step, right, right + step) | ||
left = right + step | ||
right = left + step | ||
} | ||
if (right < arr.length) { | ||
mergeArr(arr, left, left + step, right, arr.length) | ||
} | ||
step *= 2 | ||
} | ||
} | ||
|
||
function mergeArr(arr, startLeft, stopLeft, startRight, stopRight) { | ||
let rightArr = new Array(stopRight - startRight + 1) | ||
let leftArr = new Array(stopLeft - startLeft + 1) | ||
let k = startRight | ||
for (let i = 0; i < rightArr.length - 1; i++) { | ||
rightArr[i] = arr[k] | ||
++k | ||
} | ||
k = startLeft | ||
for (let i = 0; i < leftArr.length - 1; i++) { | ||
leftArr[i] = arr[k] | ||
++k | ||
} | ||
rightArr[rightArr.length - 1] = Infinity // Infinity 属性用于存放表示正无穷大的数值 | ||
leftArr[leftArr.length - 1] = Infinity // Infinity 属性用于存放表示正无穷大的数值 | ||
let m = 0 | ||
let n = 0 | ||
for (let k = startLeft; k < stopRight; k++) { | ||
if (leftArr[m] < rightArr[n]) { | ||
arr[k] = leftArr[m] | ||
m++ | ||
} else { | ||
arr[k] = rightArr[n] | ||
n++ | ||
} | ||
} | ||
} |
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,18 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>快速排序</title> | ||
</head> | ||
<body> | ||
<script src="./quick.js"></script> | ||
|
||
<script> | ||
// test | ||
|
||
let data = [3, 10, 5, 6, 2, 1, 8, 9] | ||
console.log(quickSort(data)) | ||
</script> | ||
</body> | ||
</html> |
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,18 @@ | ||
// 快速排序 | ||
// 1.在列表中选择一个元素作为基准值,排序围绕这个基准值进行,将列表中小于基准值的放入数组左边,大于放右边 | ||
|
||
|
||
function quickSort(list) { | ||
if (list.length < 2) return list | ||
let pivot = list[0] | ||
let left = [] | ||
let right = [] | ||
for (let i = 1; i < list.length; i++) { | ||
if (list[i] < pivot) { | ||
left.push(list[i]) | ||
} else { | ||
right.push(list[i]) | ||
} | ||
} | ||
return quickSort(left).concat(pivot, quickSort(right)) | ||
} |
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,70 @@ | ||
// 检索算法 | ||
// 1.在列表中查找分为数据有两种方式,顺序查找和二分查找 | ||
// 2.顺序查找适用于元素随机排列 | ||
// 3.二分查找用于已排列好的元素 | ||
// 4.从第一个元素开始对列表元素进行查找,直到找到想要的结果,被称为线性查找,属于暴利查找 | ||
// 5.二分查找,每猜一个数字会有三种结果,猜大了猜小了猜对了 | ||
|
||
// 顺序查找 | ||
function SeqSearch(arr, data) { | ||
for (let i = 0; i < arr.length; i++) { | ||
if (arr[i] === data) { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
function findMax(arr) { | ||
let max = arr[0] | ||
for (let i = 0; i < arr.length; i++) { | ||
if (arr[i] > max) { | ||
max = arr[i] | ||
} | ||
} | ||
return max | ||
} | ||
|
||
function findMin(arr) { | ||
let min = arr[0] | ||
for (let i = 0; i < arr.length; i++) { | ||
if (arr[i] < min) { | ||
min = arr[i] | ||
} | ||
} | ||
return min | ||
} | ||
|
||
function swap(arr, index1, index2) { | ||
let temp = arr[index1] | ||
arr[index1] = arr[index2] | ||
arr[index2] = temp | ||
} | ||
|
||
// 自组织方法 | ||
function SeqSearch2(arr, data) { | ||
for (let i = 0; i < arr.length; i++) { | ||
if (arr[i] === data && i > (arr.length * 0.2)) { | ||
swap(arr, i, i - 1) | ||
return true | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
// 二分查找 | ||
function bindSearch(arr, data) { | ||
let upperBound = arr.length - 1 | ||
let lowerBound = 0 | ||
while (lowerBound <= upperBound) { | ||
let mid = Math.floor((upperBound + lowerBound) / 2) | ||
if (arr[mid] < data) { | ||
lowerBound = mid + 1 | ||
} else if (arr[mid] > data) { | ||
upperBound = mid - 1 | ||
} else { | ||
return mid | ||
} | ||
} | ||
return -1 | ||
} |
Oops, something went wrong.