diff --git a/src/sort/day_3.ts b/src/sort/day_3.ts index 451b47e2..627e5517 100644 --- a/src/sort/day_3.ts +++ b/src/sort/day_3.ts @@ -2,7 +2,7 @@ * @Author: wangshan * @Date: 2021-07-04 14:14:24 * @LastEditors: wangshan - * @LastEditTime: 2021-07-04 16:31:30 + * @LastEditTime: 2021-07-11 22:36:17 * @Description: 插入排序 */ @@ -10,8 +10,8 @@ import { compareFn } from "../utils/index"; // 基础类型 // 函数类型 -let arr: number[] = [5, 4, 3, 2, 1]; -function insertionSort(arr: number[], compare = compareFn) { +// let arr: number[] = [5, 4, 3, 2, 1]; +export function insertionSort(arr: number[], compare = compareFn) { const { length } = arr; let temp: number; @@ -27,8 +27,8 @@ function insertionSort(arr: number[], compare = compareFn) { return arr; } -let res = insertionSort(arr); -console.log(res); // [1, 2, 3, 4, 5] +// let res = insertionSort(arr); +// console.log(res); // [1, 2, 3, 4, 5] /** * 待排序项,和之前的每个项目比较。并将大值往后移.最终待插入值移动到最终位置上, */ diff --git a/src/sort/day_7.ts b/src/sort/day_7.ts index 85b946ef..dedc5968 100644 --- a/src/sort/day_7.ts +++ b/src/sort/day_7.ts @@ -2,6 +2,118 @@ * @Author: wangshan * @Date: 2021-07-09 11:02:34 * @LastEditors: wangshan - * @LastEditTime: 2021-07-09 11:02:35 + * @LastEditTime: 2021-07-11 22:39:55 * @Description: 桶排序 */ +// 创建桶 +import { insertionSort } from "./day_3"; // 使用插入排序 + +function createBukets(array: number[], bucketsSize: number): number[][] { + // 初始化最大最小值,用于桶的size计算 + let minxValue = array[0]; + let maxValue = array[0]; + + // 计算最大最小值 + for (let i: number = 1; i < array.length; i++) { + if (array[i] > maxValue) { + maxValue = array[i]; + } else if (array[i] < minxValue) { + minxValue = array[i]; + } + } + + // 根据最大最小值差计算桶的个数 + const buketCount: number = + Math.floor((maxValue - minxValue) / bucketsSize) + 1; + const bukets: number[][] = []; + for (let i: number = 0; i < buketCount; i++) { + bukets[i] = []; + } + + // 将元素放入桶中 + for (let i: number = 0; i < array.length; i++) { + let buketIndex: number = Math.floor((array[i] - minxValue) / bucketsSize); + bukets[buketIndex].push(array[i]); + } + + return bukets; +} + +// 对已经分配的桶,进行排序 +function sortBukets(bukets: number[][]) { + const sortedArray: number[] = []; + for (let i: number = 0; i < bukets.length; i++) { + if (bukets[i] != null) { + insertionSort(bukets[i]); // 对每个桶进行插入排序 + sortedArray.push(...bukets[i]); + } + } + + return sortedArray; +} + +// 测试 +function buketSort(array: number[], bucketsSize = 5) { + if (array.length < 2) { + return array; + } + const bukets = createBukets(array, bucketsSize); + console.log(bukets); + return sortBukets(bukets); +} + +let arr = [5, 4, 3, 2, 6, 1, 7, 10, 9, 8]; + +console.log(buketSort(arr)); + +// 官方写法 +// function createBuckets(array: number[], bucketSize: number): number[][] { +// let minValue = array[0]; +// let maxValue = array[0]; +// for (let i = 1; i < array.length; i++) { +// if (array[i] < minValue) { +// minValue = array[i]; +// } else if (array[i] > maxValue) { +// maxValue = array[i]; +// } +// } + +// const bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1; +// const buckets: number[][] = []; +// for (let i = 0; i < bucketCount; i++) { +// buckets[i] = []; +// } + +// for (let i = 0; i < array.length; i++) { +// buckets[Math.floor((array[i] - minValue) / bucketSize)].push(array[i]); +// } + +// return buckets; +// } + +// function sortBuckets(buckets: number[][]) { +// const sortedArray = []; +// for (let i = 0; i < buckets.length; i++) { +// if (buckets[i] != null) { +// insertionSort(buckets[i]); + +// sortedArray.push(...buckets[i]); +// } +// } + +// return sortedArray; +// } + +// export function bucketSort(array: number[], bucketSize = 5) { +// if (array.length < 2) { +// return array; +// } + +// const buckets = createBuckets(array, bucketSize); + +// return sortBuckets(buckets); +// } + +// let arr = [5, 4, 3, 2, 6, 1, 7, 10, 9, 8]; + +// console.log(bucketSort(arr)); diff --git a/src/sort/index.ts b/src/sort/index.ts index c9d227ca..fb99c64b 100644 --- a/src/sort/index.ts +++ b/src/sort/index.ts @@ -2,11 +2,11 @@ * @Author: wangshan * @Date: 2021-06-24 22:14:33 * @LastEditors: wangshan - * @LastEditTime: 2021-07-08 21:22:50 + * @LastEditTime: 2021-07-11 22:25:05 * @Description: 板块-入口基点 */ // import "./day_1"; // import "./day_2"; // import "./day_4"; // import "./day_5"; -import "./day_6"; +import "./day_7";