1. 程式人生 > 實用技巧 >桶排序的思路

桶排序的思路

典型案例:arr1 [ 1, 2, 5, 2, 1, 8, 9, 5, 2, 8, 9, 10] 按照 arr2 [1, 5, 9, 8, 2, 10]的順序排序

結果: [1,1,5,5,9,9,8,8,2,2,2,10]

思路:

functionsolution(arr1,arr2){
constmap=newMap(),
len1=arr1.length,
len2=arr2.length,
res=[]
for(leti=0;i<len2;i+=1){
map.set(arr2[i],[])
}

for(leti=0;i<len1;i+=1){
constitem=arr1[i]
if(map.has(item)){
consttemp=map.get(item)
temp.push(item)
}
}

map.forEach((value)=>{
res=res.concat(value)
})

returnres
}