标签:his const struct data ons list() 数组 arraylist 一个
//归并排序 class ArrayList { constructor () { this.array = [] } insert (data) { return this.array.push(data) } mergeSort () { this.array = this.merge_1(this.array) } //辅助函数1;分数组,直到一个数为一个数组 merge_1 (arr) { //获取数组长度 let length = arr.length if (length > 1) { let index = Math.floor(length / 2) let left = arr.slice(0, index) let right = arr.slice(index) console.log(arr) return this.merge_2(this.merge_1(left), this.merge_1(right)) } else return arr } // 辅助函数2: 合并数组,返回合并后的数组 merge_2 (leftArr, rightArr) { let mergeArr = [] // while (leftArr[0] && rightArr[0]) { while (leftArr.length && rightArr.length) { if (leftArr[0] > rightArr[0]) mergeArr.push(rightArr.shift()) else mergeArr.push(leftArr.shift()) } return mergeArr.concat(leftArr, rightArr) } } let arr = new ArrayList() arr.insert(9) arr.insert(5) arr.insert(8) arr.insert(0) arr.insert(1) arr.insert(6) arr.insert(4) arr.insert(7) arr.insert(3) arr.insert(2)
标签:his const struct data ons list() 数组 arraylist 一个
原文地址:https://www.cnblogs.com/JunLan/p/12368644.html