Skip to content

Commit

Permalink
feat: 桶排序
Browse files Browse the repository at this point in the history
- 遗留问题,分散源元素到对应桶内的原则
  • Loading branch information
wangshan authored and PachVerb committed Jul 11, 2021
1 parent f6c2824 commit a5637ec
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/sort/day_3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
* @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: 插入排序
*/

// import compareFn from "../utils/compareFn";
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;

Expand All @@ -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]
/**
* 待排序项,和之前的每个项目比较。并将大值往后移.最终待插入值移动到最终位置上,
*/
114 changes: 113 additions & 1 deletion src/sort/day_7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
4 changes: 2 additions & 2 deletions src/sort/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit a5637ec

Please sign in to comment.