1.冒泡排序(Bubble Sort)
javascript
// 冒泡排序(Bubble Sort)
function bubbleSort(arr) {
const len = arr.length;
for (let i = 0; i < len - 1; i++) {
for (let j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
2.选择排序(Selection Sort)
javascript
// 选择排序(Selection Sort)
function selectionSort(arr) {
const len = arr.length;
let minIndex;
for (let i = 0; i < len - 1; i++) {
minIndex = i;
for (let j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
}
return arr;
}
3.插入排序(Insertion Sort)
javascript
// 插入排序(Insertion Sort)
function insertionSort(arr) {
const len = arr.length;
let current, j;
for (let i = 1; i < len; i++) {
current = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > current) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = current;
}
return arr;
}
4.希尔排序(Shell Sort)
javascript
// 希尔排序(Shell Sort)
function shellSort(arr) {
const len = arr.length;
let gap = Math.floor(len / 2);
while (gap > 0) {
for (let i = gap; i < len; i++) {
let temp = arr[i];
let j = i - gap;
while (j >= 0 && arr[j] > temp) {
arr[j + gap] = arr[j];
j -= gap;
}
arr[j + gap] = temp;
}
gap = Math.floor(gap / 2);
}
return arr;
}
5.归并排序(Merge Sort)
javascript
// 归并排序(Merge Sort)
function mergeSort(arr) {
if (arr.length <= 1) {
return arr;
}
const mid = Math.floor(arr.length / 2);
const left = arr.slice(0, mid);
const right = arr.slice(mid);
return merge(mergeSort(left), mergeSort(right));
}
function merge(left, right) {
const result = [];
let i = 0;
let j = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
result.push(left[i]);
i++;
} else {
result.push(right[j]);
j++;
}
}
return result.concat(left.slice(i)).concat(right.slice(j));
}
6.快速排序(Quick Sort)
javascript
// 快速排序(Quick Sort)
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
const pivotIndex = Math.floor(arr.length / 2);
const pivot = arr.splice(pivotIndex, 1)[0];
const left = [];
const right = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quickSort(left).concat([pivot], quickSort(right));
}
7.堆排序(Heap Sort)
javascript
// 堆排序(Heap Sort)
function heapSort(arr) {
const len = arr.length;
for (let i = Math.floor(len / 2) - 1; i >= 0; i--) {
heapify(arr, len, i);
}
for (let i = len - 1; i > 0; i--) {
[arr[0], arr[i]] = [arr[i], arr[0]];
heapify(arr, i, 0);
}
return arr;
}
function heapify(arr, len, i) {
let largest = i;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < len && arr[left] > arr[largest]) {
largest = left;
}
if (right < len && arr[right] > arr[largest]) {
largest = right;
}
if (largest !== i) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
heapify(arr, len, largest);
}
}
8.计数排序(Counting Sort)
javascript
// 计数排序(Counting Sort)
function countingSort(arr) {
const len = arr.length;
if (len <= 1) {
return arr;
}
const max = Math.max(...arr);
const countArr = new Array(max + 1).fill(0);
const sortedArr = [];
for (let i = 0; i < len; i++) {
countArr[arr[i]]++;
}
for (let i = 0; i < countArr.length; i++) {
while (countArr[i] > 0) {
sortedArr.push(i);
countArr[i]--;
}
}
return sortedArr;
}
9.桶排序(Bucket Sort)
javascript
// 桶排序(Bucket Sort)
function bucketSort(arr, bucketSize = 5) {
if (arr.length === 0) {
return arr;
}
const minValue = Math.min(...arr);
const maxValue = Math.max(...arr);
const bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;
const buckets = new Array(bucketCount);
for (let i = 0; i < buckets.length; i++) {
buckets[i] = [];
}
for (let i = 0; i < arr.length; i++) {
const bucketIndex = Math.floor((arr[i] - minValue) / bucketSize);
buckets[bucketIndex].push(arr[i]);
}
const sortedArr = [];
for (let i = 0; i < buckets.length; i++) {
if (buckets[i]) {
insertionSort(buckets[i]);
sortedArr.push(...buckets[i]);
}
}
return sortedArr;
}
10.基数排序(Radix Sort)
javascript
// 基数排序(Radix Sort)
function radixSort(arr) {
const maxDigit = getMaxDigit(arr);
let mod = 10;
let dev = 1;
for (let i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {
const bucket = [];
for (let j = 0; j < arr.length; j++) {
const num = parseInt((arr[j] % mod) / dev);
if (!bucket[num]) {
bucket[num] = [];
}
bucket[num].push(arr[j]);
}
arr = [];
for (let j = 0; j < bucket.length; j++) {
if (bucket[j]) {
arr.push(...bucket[j]);
}
}
}
return arr;
}
function getMaxDigit(arr) {
let maxDigit = 1;
for (let i = 0; i < arr.length; i++) {
const numDigit = getDigit(arr[i]);
if (numDigit > maxDigit) {
maxDigit = numDigit;
}
}
return maxDigit;
}
function getDigit(num) {
return Math.floor(Math.log10(num)) + 1;
}
十大排序算法详解: