bubbleSort() { let dataStore = this.dataStore let len = dataStore.length for (let i = 0; i < len - 1; i++) { for (let j = len - 1; j > i; j--) { if (dataStore[j] < dataStore[j - 1]) { this.swap(j, j - 1) } } } }
测试:
1 2 3 4 5 6 7 8 9 10 11 12
// 生成工具函数对象 let cArr = new cArray(10) // 设置随机数据 cArr.setData()
// 选择排序 selectionSort() { let dataStore = this.dataStore let len = dataStore.length for (let i = 0; i < len - 1; i++) { for (let j = i + 1; j < len; j++) { if (dataStore[i] > dataStore[j]) { this.swap(i, j) } } } }
测试
1 2 3 4 5 6 7 8 9 10 11 12
// 生成工具函数对象 let cArr = newcArray(10) // 设置随机数据 cArr.setData()
// 希尔排序 shellSort () { // 间隔序列 let N = this.dataStore.length let h = 1 while (h < N / 3) { h = 3 * h + 1 } while (h >= 1) { for (let i = h; i < this.dataStore.length; i++) { let temp = this.dataStore[i] let j = i while (j >= h && this.dataStore[j - h] > temp) { this.dataStore[j] = this.dataStore[j - h] this.swap(j, j - h) j -= h } this.dataStore[j] = temp } h = (h - 1) / 3 } }
mergeSort() { let arr = this.dataStore if (arr.length < 2) { return } let step = 1 let left, right while (step < arr.length) { left = 0; right = step while (right + step <= arr.length) { this.mergeArrays(arr, left, left + step, right, right + step) left = right + step right = left + step } if (right < arr.length) { this.mergeArrays(arr, left, left + step, right, arr.length) } step *= 2;
} } mergeArrays(arr, startLeft, stopLeft, startRight, stopRight) { let rightArr = newArray(stopRight - startRight + 1) let leftArr = newArray(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 leftArr[leftArr.length - 1] = 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++ } } }
classcArray { constructor(numElements) { this.dataStore = newArray(numElements) this.init() } // 初始化数组 init() { for (let i = 0; i < this.dataStore.length; i++) { this.dataStore[i] = i } } // 把数组设置成随机数组 setData() { for (let i = 0; i < this.dataStore.length; i++) { this.dataStore[i] = Math.floor( Math.random() * (this.dataStore.length + 1) ) } } // 清空数组 clear() { for (let i = 0; i < this.dataStore.length; i++) { this.dataStore[i] = 0 } } // 在数组顶部插入元素 insert(ele) { this.dataStore.push(ele) } // 将数组转成字符串 toString() { let result = '' for (let i = 0; i < this.dataStore.length; i++) { result += this.dataStore[i] + ' ' if (i > 0 && i % 10 == 9) { result += '\n' } } return result } // 交换数组中的元素 swap(index1, index2) { let temp = this.dataStore[index1] this.dataStore[index1] = this.dataStore[index2] this.dataStore[index2] = temp } // 冒泡排序 bubbleSort() { let dataStore = this.dataStore let len = dataStore.length for (let i = 0; i < len - 1; i++) { for (let j = len - 1; j > i; j--) { if (dataStore[j] < dataStore[j - 1]) { this.swap(j, j - 1) } } } } // 选择排序 selectionSort() { let dataStore = this.dataStore let len = dataStore.length for (let i = 0; i < len - 1; i++) { for (let j = i + 1; j < len; j++) { if (dataStore[i] > dataStore[j]) { this.swap(i, j) } } } } // 插入排序 insertionSort() { let temp, inser let dataStore = this.dataStore let len = dataStore.length for (let outer = 1; outer < len; outer++) { temp = dataStore[outer] let inner = outer while (inner > 0 && dataStore[inner - 1] > temp) { this.swap(inner, inner - 1) inner-- } dataStore[inner] = temp } }
// 希尔排序 shellSort() { // 间隔序列 let N = this.dataStore.length let h = 1 while (h < N / 3) { h = 3 * h + 1 } while (h >= 1) { for (let i = h; i < this.dataStore.length; i++) { let temp = this.dataStore[i] let j = i while (j >= h && this.dataStore[j - h] > temp) { this.dataStore[j] = this.dataStore[j - h] this.swap(j, j - h) j -= h } this.dataStore[j] = temp } h = (h - 1) / 3 } } // 归并排序 mergeSort() { let arr = this.dataStore if (arr.length < 2) { return } let step = 1 let left, right while (step < arr.length) { left = 0; right = step while (right + step <= arr.length) { this.mergeArrays(arr, left, left + step, right, right + step) left = right + step right = left + step } if (right < arr.length) { this.mergeArrays(arr, left, left + step, right, arr.length) } step *= 2;
} } mergeArrays(arr, startLeft, stopLeft, startRight, stopRight) { let rightArr = newArray(stopRight - startRight + 1) let leftArr = newArray(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 leftArr[leftArr.length - 1] = 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++ } } } // 快速排序 qSort() { this.dataStore = this.qSortArr(this.dataStore) } qSortArr(list) { if (list.length == 0) { return [] } let lesser = [] let greater = [] let pivot = list[0] for (let i = 1; i < list.length; i++) { if (list[i] < pivot) { lesser.push(list[i]) } else { greater.push(list[i]) } } returnthis.qSortArr(lesser).concat(pivot, this.qSortArr(greater)) } } // 生成工具函数对象 let cArr = newcArray(10) // 设置随机数据 cArr.setData()