function sequentialSearch(arr,val){ for(let i = 0;i < arr.length;i++){ if(val == arr[i]){ return i } } return -1 }
function binarySearch(arr,val){ const sortArr = quickSort(arr) let lowIndex = 0 let highIndex = sortArr.length-1 while(lowIndex <= highIndex){ let mid = Math.floor((lowIndex + highIndex) / 2) if(val == sortArr[i]){ return mid }else if(val > sortArr[mid]){ lowIndex = mid + 1 }else if(val < sortArr[mid]){ highIndex = mid -1 }else{ return -1 } } }
function shuffle(arr){ for(let i = arr.length-1;i > 0;i--){ const randomIndex = Math.floor(Math.random() * (i+1)) swap(arr,i,randomIndex) } }
原文:https://www.cnblogs.com/zhenjianyu/p/13232581.html