From a591e8d5c0786ff18ce8f0e2a5bdb25f0e46e885 Mon Sep 17 00:00:00 2001 From: nameczz Date: Fri, 19 Oct 2018 00:01:03 +0800 Subject: [PATCH] [javascript] 11 & 12 sorts --- javascript/11_sorts/sort.js | 68 ++++++++++++++++++++++++++++++++ javascript/12_sorts/MergeSort.js | 43 ++++++++++++++++++++ javascript/12_sorts/QuickSort.js | 46 +++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 javascript/11_sorts/sort.js create mode 100644 javascript/12_sorts/MergeSort.js create mode 100644 javascript/12_sorts/QuickSort.js diff --git a/javascript/11_sorts/sort.js b/javascript/11_sorts/sort.js new file mode 100644 index 0000000..0a3c9c8 --- /dev/null +++ b/javascript/11_sorts/sort.js @@ -0,0 +1,68 @@ +/** + * 冒泡,插入,选择排序 + * + * Author: nameczz + */ + +// 冒泡排序 +const bubbleSort = (arr) => { + if (arr.length <= 1) return + for (let i = 0; i < arr.length; i++) { + let hasChange = false + for (let j = 0; j < arr.length - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + const temp = arr[j] + arr[j] = arr[j + 1] + arr[j + 1] = temp + hasChange = true + } + } + // 如果false 说明所有元素已经到位 + if (!hasChange) break + } + console.log(arr) +} + +// 插入排序 +const insertionSort = (arr) => { + if (arr.length <= 1) return + for (let i = 1; i < arr.length; i++) { + const temp = arr[i] + let j = i - 1 + // 若arr[i]前有大于arr[i]的值的化,向后移位,腾出空间,直到一个<=arr[i]的值 + for (j; j >= 0; j--) { + if (arr[j] > temp) { + arr[j + 1] = arr[j] + } else { + break + } + } + arr[j + 1] = temp + } + console.log(arr) +} + +// 选择排序 +const selectionSort = (arr) => { + if (arr.length <= 1) return + // 需要注意这里的边界, 因为需要在内层进行 i+1后的循环,所以外层需要 数组长度-1 + for (let i = 0; i < arr.length - 1; i++) { + let minIndex = i + for (let j = i + 1; j < arr.length; j++) { + if (arr[j] < arr[minIndex]) { + minIndex = j // 找到整个数组的最小值 + } + } + const temp = arr[i] + arr[i] = arr[minIndex] + arr[minIndex] = temp + } + console.log(arr) +} + +const test = [4, 5, 6, 3, 2, 1] +bubbleSort(test) +const testSort = [4, 1, 6, 3, 2, 1] +insertionSort(testSort) +const testSelect = [4, 8, 6, 3, 2, 1, 0, 12] +selectionSort(testSelect) \ No newline at end of file diff --git a/javascript/12_sorts/MergeSort.js b/javascript/12_sorts/MergeSort.js new file mode 100644 index 0000000..6185ab4 --- /dev/null +++ b/javascript/12_sorts/MergeSort.js @@ -0,0 +1,43 @@ +/** + * 归并排序 + * + * Author: nameczz + */ + +const mergeArr = (left, right) => { + let temp = [] + let leftIndex = 0 + let rightIndex = 0 + // 判断2个数组中元素大小,依次插入数组 + while (left.length > leftIndex && right.length > rightIndex) { + if (left[leftIndex] <= right[rightIndex]) { + temp.push(left[leftIndex]) + leftIndex++ + } else { + temp.push(right[rightIndex]) + rightIndex++ + } + } + // 合并 多余数组 + return temp.concat(left.slice(leftIndex)).concat(right.slice(rightIndex)) +} + +const mergeSort = (arr) => { + // 当任意数组分解到只有一个时返回。 + if (arr.length <= 1) return arr + const middle = Math.floor(arr.length / 2) // 找到中间值 + const left = arr.slice(0, middle) // 分割数组 + const right = arr.slice(middle) + // 递归 分解 合并 + return mergeArr(mergeSort(left), mergeSort(right)) +} + +const testArr = [] +let i = 0 +while (i < 100) { + testArr.push(Math.floor(Math.random() * 1000)) + i++ +} + +const res = mergeSort(testArr) +console.log(res) \ No newline at end of file diff --git a/javascript/12_sorts/QuickSort.js b/javascript/12_sorts/QuickSort.js new file mode 100644 index 0000000..d8ba3bd --- /dev/null +++ b/javascript/12_sorts/QuickSort.js @@ -0,0 +1,46 @@ +/** + * 快速排序 + * + * Author: nameczz + */ + +const swap = (arr, i, j) => { + const temp = arr[i] + arr[i] = arr[j] + arr[j] = temp +} + +// 获取 pivot 交换完后的index +const partition = (arr, pivot, left, right) => { + const pivotVal = arr[pivot] + let startIndex = left + for (let i = left; i < right; i++) { + if (arr[i] < pivotVal) { + swap(arr, i, startIndex) + startIndex++ + } + } + swap(arr, startIndex, pivot) + return startIndex +} + +const quickSort = (arr, left, right) => { + if (left < right) { + let pivot = right + let partitionIndex = partition(arr, pivot, left, right) + quickSort(arr, left, partitionIndex - 1 < left ? left : partitionIndex - 1) + quickSort(arr, partitionIndex + 1 > right ? right : partitionIndex + 1, right) + } + +} + + +const testArr = [] +let i = 0 +while (i < 10) { + testArr.push(Math.floor(Math.random() * 1000)) + i++ +} +console.log('unsort', testArr) +quickSort(testArr, 0, testArr.length - 1); +console.log('sort', testArr) \ No newline at end of file