javascript
JavaScript实现十种经典排序算法(js排序算法)
?冒泡排序算法
冒泡排序(Bubble Sort)是一種簡單直觀的排序算法。冒泡排序算法的步驟描述如下:
JavaScript實(shí)現(xiàn)冒泡排序算法的代碼如下:
| function bubbleSort(arr) { ????var len = arr.length; ????for (var i = 0; i < len - 1; i++) { ????????for (var j = 0; j < len - 1 - i; j++) { ????????????if (arr[j] > arr[j+1]) {??????? // 相鄰元素兩兩對(duì)比 ????????????????var temp = arr[j+1];??????? // 元素交換 ????????????????arr[j+1] = arr[j]; ????????????????arr[j] = temp; ????????????} ????????} ????} ????return arr; }??? |
?選擇排序算法
選擇排序是一種簡單直觀的排序算法,無論什么數(shù)據(jù)進(jìn)去都是 O(n2) 的時(shí)間復(fù)雜度。選擇排序算法的步驟描述如下:
JavaScript實(shí)現(xiàn)選擇排序算法的代碼如下:
| function selectionSort(arr) { ????var len = arr.length; ????var minIndex, temp; ????for (var i = 0; i < len - 1; i++) { ????????minIndex = i; ????????for (var j = i + 1; j < len; j++) { ????????????if (arr[j] < arr[minIndex]) {???? // 尋找最小的數(shù) ????????????????minIndex = j;???????????????? // 將最小數(shù)的索引保存 ????????????} ????????} ????????temp = arr[i]; ????????arr[i] = arr[minIndex]; ????????arr[minIndex] = temp; ????} ????return arr; }?? |
?插入排序算法
插入排序是一種最簡單直觀的排序算法,它的工作原理是通過構(gòu)建有序序列,對(duì)于未排序數(shù)據(jù),在已排序序列中從后向前掃描,找到相應(yīng)位置并插入。插入排序算法的步驟描述如下:
JavaScript實(shí)現(xiàn)插入排序算法的代碼如下:
| function insertionSort(arr) { ????var len = arr.length; ????var preIndex, current; ????for (var i = 1; i < len; i++) { ????????preIndex = i - 1; ????????current = arr[i]; ????????while(preIndex >= 0 && arr[preIndex] > current) { ????????????arr[preIndex+1] = arr[preIndex]; ????????????preIndex--; ????????} ????????arr[preIndex+1] = current; ????} ????return arr; } |
?希爾排序算法
希爾排序,也稱遞減增量排序算法,是插入排序的一種更高效的改進(jìn)版本。希爾排序算法的步驟描述如下:
JavaScript實(shí)現(xiàn)希爾排序算法的代碼如下:
| function shellSort(arr) { ????var len = arr.length, ????????temp, ????????gap = 1; ????while(gap < len/3) {????????? //動(dòng)態(tài)定義間隔序列 ????????gap =gap*3+1; ????} ????for (gap; gap > 0; gap = Math.floor(gap/3)) { ????????for (var i = gap; i < len; i++) { ????????????temp = arr[i]; ????????????for (var j = i-gap; j >= 0 && arr[j] > temp; j-=gap) { ????????????????arr[j+gap] = arr[j]; ????????????} ????????????arr[j+gap] = temp; ????????} ????} ????return arr; } |
?歸并排序算法
歸并排序(Merge sort)是建立在歸并操作上的一種有效的排序算法。歸并排序算法的步驟描述如下:
JavaScript實(shí)現(xiàn)歸并排序算法的代碼如下:
| function mergeSort(arr) {? // 采用自上而下的遞歸方法 ????var len = arr.length; ????if(len < 2) { ????????return arr; ????} ????var middle = Math.floor(len / 2), ????????left = arr.slice(0, middle), ????????right = arr.slice(middle); ????return merge(mergeSort(left), mergeSort(right)); } function merge(left, right) { ????var result = []; ????while (left.length && right.length) { ????????if (left[0] <= right[0]) { ????????????result.push(left.shift()); ????????} else { ????????????result.push(right.shift()); ????????} ????} ????while (left.length) ????????result.push(left.shift()); ????while (right.length) ????????result.push(right.shift()); ????return result; } |
?快速排序算法
快速排序是由東尼·霍爾所發(fā)展的一種排序算法。它是處理大數(shù)據(jù)最快的排序算法之一。快速排序是一種分而治之思想在排序算法上的典型應(yīng)用。本質(zhì)上來看,快速排序應(yīng)該算是在冒泡排序基礎(chǔ)上的遞歸分治法。快速排序算法的步驟描述如下:
JavaScript實(shí)現(xiàn)快速排序算法的代碼如下:
| function quickSort(arr, left, right) { ????var len = arr.length, ????????partitionIndex, ????????left = typeof left != 'number' ? 0 : left, ????????right = typeof right != 'number' ? len - 1 : right; ????if (left < right) { ????????partitionIndex = partition(arr, left, right); ????????quickSort(arr, left, partitionIndex-1); ????????quickSort(arr, partitionIndex+1, right); ????} ????return arr; } function partition(arr, left ,right) {???? // 分區(qū)操作 ????var pivot = left,????????????????????? // 設(shè)定基準(zhǔn)值(pivot) ????????index = pivot + 1; ????for (var i = index; i <= right; i++) { ????????if (arr[i] < arr[pivot]) { ????????????swap(arr, i, index); ????????????index++; ????????}??????? ????} ????swap(arr, pivot, index - 1); ????return index-1; } function swap(arr, i, j) { ????var temp = arr[i]; ????arr[i] = arr[j]; ????arr[j] = temp; } functiion paritition2(arr, low, high) { ??let pivot = arr[low]; ??while (low < high) { ????while (low < high && arr[high] > pivot) { ??????--high; ????} ????arr[low] = arr[high]; ????while (low < high && arr[low] <= pivot) { ??????++low; ????} ????arr[high] = arr[low]; ??} ??arr[low] = pivot; ??return low; } function quickSort2(arr, low, high) { ??if (low < high) { ????let pivot = paritition2(arr, low, high); ????quickSort2(arr, low, pivot - 1); ????quickSort2(arr, pivot + 1, high); ??} ??return arr; } |
?堆排序算法
堆排序(Heapsort)是指利用堆這種數(shù)據(jù)結(jié)構(gòu)所設(shè)計(jì)的一種排序算法。堆排序算法的步驟描述如下:
JavaScript實(shí)現(xiàn)堆排序算法的代碼如下:
| var len;??? // 因?yàn)槁暶鞯亩鄠€(gè)函數(shù)都需要數(shù)據(jù)長度,所以把len設(shè)置成為全局變量 function buildMaxHeap(arr) {?? // 建立大頂堆 ????len = arr.length; ????for (var i = Math.floor(len/2); i >= 0; i--) { ????????heapify(arr, i); ????} } function heapify(arr, i) {???? // 堆調(diào)整 ????var left = 2 * i + 1, ????????right = 2 * i + 2, ????????largest = i; ????if (left < len && arr[left] > arr[largest]) { ????????largest = left; ????} ????if (right < len && arr[right] > arr[largest]) { ????????largest = right; ????} ????if (largest != i) { ????????swap(arr, i, largest); ????????heapify(arr, largest); ????} } function swap(arr, i, j) { ????var temp = arr[i]; ????arr[i] = arr[j]; ????arr[j] = temp; } function heapSort(arr) { ????buildMaxHeap(arr); ????for (var i = arr.length-1; i > 0; i--) { ????????swap(arr, 0, i); ????????len--; ????????heapify(arr, 0); ????} ????return arr; } |
?計(jì)數(shù)排序算法
計(jì)數(shù)排序的核心在于將輸入的數(shù)據(jù)值轉(zhuǎn)化為鍵存儲(chǔ)在額外開辟的數(shù)組空間中。作為一種線性時(shí)間復(fù)雜度的排序,計(jì)數(shù)排序要求輸入的數(shù)據(jù)必須是有確定范圍的整數(shù)。
JavaScript實(shí)現(xiàn)計(jì)數(shù)排序算法的代碼如下:
| function countingSort(arr, maxValue) { ????var bucket = new Array(maxValue+1), ????????sortedIndex = 0; ????????arrLen = arr.length, ????????bucketLen = maxValue + 1; ????for (var i = 0; i < arrLen; i++) { ????????if (!bucket[arr[i]]) { ????????????bucket[arr[i]] = 0; ????????} ????????bucket[arr[i]]++; ????} ????for (var j = 0; j < bucketLen; j++) { ????????while(bucket[j] > 0) { ????????????arr[sortedIndex++] = j; ????????????bucket[j]--; ????????} ????} ????return arr; } |
?桶排序算法
桶排序是計(jì)數(shù)排序的升級(jí)版。它利用了函數(shù)的映射關(guān)系,高效與否的關(guān)鍵就在于這個(gè)映射函數(shù)的確定。
JavaScript實(shí)現(xiàn)桶排序算法的代碼如下:
| function bucketSort(arr, bucketSize) { ????if (arr.length === 0) { ??????return arr; ????} ????var i; ????var minValue = arr[0]; ????var maxValue = arr[0]; ????for (i = 1; i < arr.length; i++) { ??????if (arr[i] < minValue) { ??????????minValue = arr[i];??????????????? // 輸入數(shù)據(jù)的最小值 ??????} else if (arr[i] > maxValue) { ??????????maxValue = arr[i];??????????????? // 輸入數(shù)據(jù)的最大值 ??????} ????} ????//桶的初始化 ????var DEFAULT_BUCKET_SIZE = 5;??????????? // 設(shè)置桶的默認(rèn)數(shù)量為5 ????bucketSize = bucketSize || DEFAULT_BUCKET_SIZE; ????var bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;?? ????var buckets = new Array(bucketCount); ????for (i = 0; i < buckets.length; i++) { ????????buckets[i] = []; ????} ????//利用映射函數(shù)將數(shù)據(jù)分配到各個(gè)桶中 ????for (i = 0; i < arr.length; i++) { ????????buckets[Math.floor((arr[i] - minValue) / bucketSize)].push(arr[i]); ????} ????arr.length = 0; ????for (i = 0; i < buckets.length; i++) { ????????insertionSort(buckets[i]);????????????????????? // 對(duì)每個(gè)桶進(jìn)行排序,這里使用了插入排序 ????????for (var j = 0; j < buckets[i].length; j++) { ????????????arr.push(buckets[i][j]);????????????????????? ????????} ????} ????return arr; } |
?基數(shù)排序算法
基數(shù)排序是一種非比較型整數(shù)排序算法,其原理是將整數(shù)按位數(shù)切割成不同的數(shù)字,然后按每個(gè)位數(shù)分別比較。由于整數(shù)也可以表達(dá)字符串(比如名字或日期)和特定格式的浮點(diǎn)數(shù),所以基數(shù)排序也不是只能使用于整數(shù)。
JavaScript實(shí)現(xiàn)基數(shù)排序算法的代碼如下:
| var counter = []; function radixSort(arr, maxDigit) { ????var mod = 10; ????var dev = 1; ????for (var i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) { ????????for(var j = 0; j < arr.length; j++) { ????????????var bucket = parseInt((arr[j] % mod) / dev); ????????????if(counter[bucket]==null) { ????????????????counter[bucket] = []; ????????????} ????????????counter[bucket].push(arr[j]); ????????} ????????var pos = 0; ????????for(var j = 0; j < counter.length; j++) { ????????????var value = null; ????????????if(counter[j]!=null) { ????????????????while ((value = counter[j].shift()) != null) { ??????????????????????arr[pos++] = value; ????????????????} ??????????} ????????} ????} ????return arr; } |
總結(jié)
以上是生活随笔為你收集整理的JavaScript实现十种经典排序算法(js排序算法)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 太简单!日常小动作让你摆脱程序员职业病
- 下一篇: 第二阶段冲刺6