leetcode练习:912. 排序数组
const arr = Array(11).fill().map(() => ~~(Math.random() * 100));
console.log('bubbleSort(arr): ', ...bubbleSort(arr.slice()));
console.log('selectionSort(arr): ', ...selectionSort(arr.slice()));
console.log('insertionSort(arr): ', ...insertionSort(arr.slice()));
console.log('shellSort(arr): ', ...shellSort(arr.slice()));
console.log('countingSort(arr): ', ...countingSort(arr.slice()));
console.log('mergeSort1(arr): ', ...mergeSort1(arr.slice()));
console.log('quickSort1(arr): ', ...quickSort1(arr.slice()));
console.log('heapSort(arr): ', ...heapSort(arr.slice()));
console.log('bucketSort(arr): ', ...bucketSort(arr.slice()));
console.log('radixSort(arr): ', ...radixSort(arr.slice()));
function bubbleSort(arr) {
const len = arr.length;
for (let i = 0; i < len; i++) {
for (let j = 0; j < len - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
function selectionSort(arr) {
const len = arr.length;
for (let i = 0; i < len; i++) {
let temp = i;
for (let j = i + 1; j < len; j++) {
if (arr[temp] > arr[j]) {
temp = j;
}
}
[arr[temp], arr[i]] = [arr[i], arr[temp]];
}
return arr;
}
function insertionSort(arr) {
const len = arr.length;
for (let i = 1; i < len; i++) {
const current = arr[i];
let preIndex = i - 1;
while (preIndex >= 0 && arr[preIndex] > current) {
arr[preIndex + 1] = arr[preIndex];
preIndex--;
}
arr[preIndex + 1] = current;
}
return arr;
}
function shellSort(arr) {
const len = arr.length;
let increment = ~~(len / 2);
while (increment > 0) {
for (let i = increment; i < len; i++) {
const current = arr[i];
let preIndex = i - increment;
while (preIndex >= 0 && arr[preIndex] > current) {
arr[preIndex + increment] = arr[preIndex];
preIndex -= increment;
}
arr[preIndex + increment] = current;
}
increment = ~~(increment / 2);
}
return arr;
}
function countingSort(arr) {
const maxValue = Math.max(...arr);
const countArr = Array(maxValue + 1).fill(0);
arr.forEach(element => {
countArr[element]++;
});
let sortedIndex = 0;
countArr.forEach((count, index) => {
while (count--) {
arr[sortedIndex++] = index;
}
});
return arr;
}
function mergeSort1(arr) {
if (arr.length < 2) return arr;
const mid = ~~(arr.length / 2);
return merge(mergeSort1(arr.slice(0, mid)), mergeSort1(arr.slice(mid)))
}
function merge(left, right) {
let i = 0, j = 0;
const newArr = [];
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
newArr.push(left[i++]);
} else {
newArr.push(right[j++]);
}
}
while (i < left.length) {
newArr.push(left[i++]);
}
while (j < right.length) {
newArr.push(right[j++]);
}
return newArr;
};
function quickSort1(arr, left = 0, right = arr.length - 1) {
if (left < right) {
const partitionIndex = partition1(arr, left, right);
quickSort1(arr, left, partitionIndex - 1);
quickSort1(arr, partitionIndex + 1, right);
}
return arr;
}
function partition1(arr, left, right) {
const current = arr[left];
while (left < right) {
while (left < right && arr[right] >= current) {
right--;
}
arr[left] = arr[right];
while (left < right && arr[left] < current) {
left++;
}
arr[right] = arr[left];
}
arr[left] = current;
return left;
}
function quickSort2(arr, left = 0, right = arr.length - 1) {
if (left < right) {
const partitionIndex = quickSort2(arr, left, right);
quickSort2(arr, left, partitionIndex - 1);
quickSort2(arr, partitionIndex + 1, right);
}
return arr;
}
function partition2(arr, left, right) {
const pivot = arr[left];
let index = left + 1;
for(let i = index; i <= right; i++) {
if(arr[i] < pivot) {
[ arr[i], arr[index] ] = [ arr[index], arr[i] ];
index++;
}
}
[ arr[left], arr[index - 1] ] = [ arr[index - 1], arr[left] ];
return index - 1;
}
function heapSort(arr) {
const len = arr.length;
for (let i = Math.floor(len / 2); i >= 0; i--) {
sift(arr, i, len - 1);
}
for (let i = len - 1; i > 0; i--) {
[arr[0], arr[i]] = [arr[i], arr[0]];
sift(arr, 0, i - 1);
}
return arr;
}
function sift(arr, low, high) {
const current = arr[low];
let i = low, j = 2 * i + 1;
while (j <= high) {
if (j < high && arr[j] < arr[j + 1]) {
j = j + 1;
}
if (current < arr[j]) {
arr[i] = arr[j];
i = j;
j = 2 * i;
} else break;
}
arr[i] = current;
}
function bucketSort(arr) {
const len = arr.length;
let maxValue = arr[0], minValue = arr[0];
for (const num of arr) {
if (num > maxValue) maxValue = num;
if (num < minValue) minValue = num;
}
const bucketSize = 5;
const bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;
const bucket = Array(bucketCount).fill().map(() => []);
for (let i = 0; i < len; i++) {
bucket[Math.floor((arr[i] - minValue) / bucketCount)].push(arr[i]);
}
const newArr = [];
for (let i = 0; i < bucketCount; i++) {
if (bucket[i].length) {
newArr.push(...insertionSort(bucket[i]));
}
}
return newArr;
}
function radixSort(arr, radixBase = 10) {
let maxValue = arr[0], minValue = arr[0];
for (const num of arr) {
if (num > maxValue) maxValue = num;
if (num < minValue) minValue = num;
}
let currentDigit = 1;
while ((maxValue - minValue) / currentDigit >= 1) {
arr = countingSortForRadix(arr, radixBase, currentDigit, minValue);
currentDigit *= radixBase;
}
return arr;
}
function countingSortForRadix(arr, radixBase, currentDigit, minValue) {
const len = arr.length;
for (const num of arr) {
const bucketIndex = Math.floor((num - minValue) / currentDigit) % radixBase;
bucket[bucketIndex]++;
}
for (let i = 1; i < len; i++) {
bucket[i] += bucket[i - 1];
}
const res = [];
for (let i = len - 1; i >= 0; i--) {
const bucketIndex = Math.floor((arr[i] - minValue) / currentDigit) % radixBase;
res[--bucket[bucketIndex]] = arr[i];
}
return res;
}