// 冒泡排序(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;
}
// 选择排序(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;
}
// 插入排序(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;
}
// 希尔排序(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)
// 归并排序(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));
}
// 快速排序(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));
}
// 堆排序(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);
}
}
// 计数排序(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)
// 桶排序(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)
// 基数排序(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;
}
【C#】十大排序算法(动图演示+代码实现)_c#排序算法-CSDN博客
原文地址:https://blog.csdn.net/qq_48691693/article/details/134674052
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_12129.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。