排序
commonjs 一个交换变量的函数
function commit(arr,left,right){
var temp = arr[right];
arr[right] = arr[left];
arr[left] = temp;
}
冒泡排序
function maopaoSort(arr){
for(var len = arr.length,i=len;i>0;i--){
for(var j = 1;j<i;j++){
if(arr[j]<arr[j - 1]){
commit(arr,j,j - 1)
}
}
}
return arr;
}
选择排序
function xuanzeSort(arr){
for(var i = 0,len = arr.length;i<len;i++){
var index = i;
for(var j = i;j<len;j++){
if(arr[j]<arr[index]){
index = j;
}
}
commit(arr,i,index);
}
return arr
}
插入算法
function charuSort(arr){
for(var i = 1,len = arr.length;i<len;i++){
var index = i - 1;
var current = arr[i];
while(index>0 && current<arr[index]){
arr[index+1] = arr[index];
index--
}
arr[index+1] = current;
}
return arr;
}
希尔排序
function xierSort(arr){
for(var aqe = 4;aqe> = 1;aqe/=2){
for(var i = 0;i<aqe;i++){
for(var j = i + aqe,len = arr.length;j<len;j+=aqe){
var index = j - aqe;
var current = arr[j];
while(index >=i && current<arr[index]){
arr[index + aqe] = arr[index];
index -= aqe;
}
arr[index + aqe] = current;
}
}
}
return arr;
}
快速排序
function kuaisuSort(arr,left,right){
var len = arr.length,
left = typeof left == "number" ? left : 0 ,
right = typeof right == "number" ? right : len - 1;
if(right > left){
var fgline = fegeline(arr,left,right);
kuaisuSort(arr,left,fgline - 1)
kuaisuSort(arr,fgline + 1,right)
}
return arr;
}
function fegeline(arr,left,right){
var index = left + 1;
for(var i=left+1;i<=right;i++){
if(arr[i]<arr[left]){
commit(arr,i,index)
index++;
}
}
commit(arr,left,index-1);
return index - 1;
}
归并 排序
function guibingSort(arr){
var len = arr.length;
if(len<2){
return arr;
}
var mid = Math.floor(len/2);
var left = arr.slice(0,mid);
var right = arr.slice(mid);
return compare(guibingSort(left),guibingSort(right));
}
function compare(left,right){
var result = [];
while(left.length>0&& right.length>0){
if(left[0]>right[0]){
result.push(right.shift())
}else{
result.push(left.shift())
}
}
while(left.length){
result.push(left.shift())
}
while(right.length){
result.push(right.shift())
}
return result;
}
堆 排序
var len ;
function duiSort(arr){
len = arr.length;
buildDui(arr);
for(var i = len -1;i>0;i--){
commit(arr,0,1);
len--;
adjustDui(arr,0)
}
}
function buildDui(arr){
var num = Math.floor(len/2);
for(var i = num;i>=0;i--){
adjustDui(arr,i);
}
console.log('jieshu:'+ arr)
}
function adjustDui(arr,index){
var Index = index;
var left = 2*index+1;
var right = 2*index+2;
if(left <len && arr[left]>arr[Index]){
Index = left ;
}
if(right<len && arr[right]>arr[Index]){
Index = right;
}
if(Index != index){
commit(arr,index,Index);
adjustDui(arr,Index)
}
console.log(arr)
}
计数 排序
function jishuSort(arr,maxNum){
var newArr = new Array(maxNum);
for(var i = 0,len = arr.length;i<len;i++){
if(!newArr[arr[i]]){
newArr[arr[i]] = 0
}
newArr[arr[i]]++
}
arr = [];
for(var j = 0,len = newArr.length;j<len;j++){
while(newArr[j]>0){
arr.push(j);
newArr[j]--;
}
}
return arr;
}
桶 排序
function tongSort(arr){
}
function clone(target) {
// 初始化变量 result 成为最终克隆数组
function checkedType(target) {
return Object.prototype.toString.call(target).slice(8, -1)
}
let result,
// 判断 被拷贝数据类型
targetType = checkedType(target);
if (targetType === 'Array') {
result = []
} else if (targetType === 'Object') {
result = {}
} else {
return target
}
// 遍历目标数据
for (let i in target) {
let value = target[i]
if (['Array', 'Object'].includes(checkedType(value))) {
// 继续遍历
result[i] = clone(value)
} else {
result[i] = value
}
}
return result;
}
参考文章地址:https://blog.csdn.net/weixin_42444270/article/details/83543489.