前端常见算法的JS实现
生活随笔
收集整理的這篇文章主要介紹了
前端常见算法的JS实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文鏈接
排序算法
1、冒泡排序
function bubbleSort(arr){var i = 0,j = 0;for(i=1; i<arr.length; i++){for(j=0; j<=arr.length-i; j++){var temp = 0;// ">" 從小到大排序// "<" 從大到小排序if(arr[j] > arr[j+1]){temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}}return arr; }2、快速排序
function quickSort(arr,l,r){if(l < r){var i = l, j = r, x = arr[i];while(i<j){while(i<j && arr[j]>x)j--;if(i<j)//這里用i++,被換過來的必然比x小,賦值后直接讓i自加,不用再比較,可以提高效率arr[i++] = arr[j];while(i<j && arr[i]<x)i++;if(i<j)//這里用j--,被換過來的必然比x大,賦值后直接讓j自減,不用再比較,可以提高效率arr[j--] = arr[i];}arr[i] = x;quickSort(arr, l, i-1);quickSort(arr, i+1, r);} }3、二路歸并
function merge(left, right) {var result = [],il = 0,ir = 0;while (il < left.length && ir < right.length) {if (left[il] < right[ir]) {result.push(left[il++]);} else {result.push(right[ir++]);}}while(left[il]){result.push(left[il++]);}while(right[ir]){result.push(right[ir++]);}return result; }字符串操作
1、判斷回文字符串
function palindrome(str){// \W匹配任何非單詞字符。等價于“[^A-Za-z0-9_]”。var re = /[\W_]/g;// 將字符串變成小寫字符,并干掉除字母數字外的字符var lowRegStr = str.toLowerCase().replace(re,'');// 如果字符串lowRegStr的length長度為0時,字符串即是palindromeif(lowRegStr.length===0) return true;// 如果字符串的第一個和最后一個字符不相同,那么字符串就不是palindromeif(lowRegStr[0]!=lowRegStr[lowRegStr.length-1]) return false;//遞歸return palindrome(lowRegStr.slice(1,lowRegStr.length-1)); }2、翻轉字符串
思路一:反向遍歷字符串
function reverseString(str){var tmp = '';for(var i=str.length-1; i>=0; i--)tmp += str[i];return tmp }思路二:轉化成array操作
function reverseString(str){var arr = str.split("");var i = 0,j = arr.length-1;while(i<j){tmp = arr[i];arr[i] = arr[j];arr[j] = tmp;i++;j--;}return arr.join(""); }3、生成指定長度隨機字符串
function randomString(n){var str = 'abcdefghijklmnopqrstuvwxyz0123456789';var tmp = '';for(var i=0; i<n; i++) {tmp += str.charAt(Math.round(Math.random()*str.length));}return tmp; }4、統計字符串中次數最多字母
function findMaxDuplicateChar(str) {if(str.length == 1) {return str;}var charObj = {};for(var i = 0; i < str.length; i++) {if(!charObj[str.charAt(i)]) {charObj[str.charAt(i)] = 1;} else {charObj[str.charAt(i)] += 1;}}var maxChar = '',maxValue = 1;for(var k in charObj) {if(charObj[k] >= maxValue) {maxChar = k;maxValue = charObj[k];}}return maxChar + ':' + maxValue; }數組操作
1、數組去重
function unique(arr){var obj = {}var result = []for(var i in arr){if(!obj[arr[i]]){obj[arr[i]] = true;result.push(arr[i]);}}return result; }2、數組中最大差值
function getMaxProfit(arr){var min = arr[0],max = arr[0];for(var i = 0; i < arr.length; i++){if(arr[i] < min) min = arr[i];if(arr[i] > max) max = arr[i];}return max - min; }其他常見算法
1、階乘
非遞歸實現
function factorialize(num) {var result = 1;if(num < 0) return -1;if(num == 0 || num == 1) return 1;while(num>1) {result *= num--;}return result; }遞歸實現
function factorialize(num) {var result = 1;if(num < 0) return -1;if(num == 0 || num == 1) return 1;if(num > 1) return num*factorialize(num-1); }2、生成菲波那切數列
強行遞歸實現
function getfib(n){if(n == 0) return 0;if(n == 1) return 1;if(n > 1) return getfib(n-1) + getfib(n-2); } function fibo(len){var fibo = [];for(var i = 0; i < len; i++){fibo.push(getfib(i));}return fibo; }簡約非遞歸實現
function getFibonacci(n) {var fibarr = [];var i = 0;while(i < n) {if(i <= 1) {fibarr.push(i);} else {fibarr.push(fibarr[i - 1] + fibarr[i - 2])}i++;}return fibarr; }3、二分查找
非遞歸實現
function binary_search(arr, key) {var low = 0,high = arr.length - 1;while(low <= high){var mid = parseInt((high + low) / 2);if(key == arr[mid]){return mid;}else if(key > arr[mid]){low = mid + 1;}else if(key < arr[mid]){high = mid -1;}}return -1; }遞歸實現
function binary_search2(arr, low, high, key) {if(low > high) return -1;var mid = parseInt((low + high)/2);if(key == arr[mid]) {return mid;} else if(key > arr[mid]) {return binary_search2(arr, mid+1, high, key);} else if(key < arr[mid]) {return binary_search2(arr, low, mid-1, key);} }總結
以上是生活随笔為你收集整理的前端常见算法的JS实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pb中将已经建好的Grid类型转为Tab
- 下一篇: 矿泉水广告标语文案30句