Maison >interface Web >js tutoriel >Explication détaillée des exemples d'algorithmes de fusion js
Cet article partage principalement avec vous l'explication détaillée des exemples d'algorithmes de fusion js, dans l'espoir d'aider tout le monde.
Divisez le tableau de manière récursive en éléments individuels, puis fusionnez les tableaux
let data3 = [14, 54, 73, 38, 39, 67, 75, 80, 50, 40, 96, 27, 105, 109, 77, 31]function breakArr (data,start,end) { if (start < end) { let mid = Math.floor((start + end)/2) breakArr(data,start,mid) breakArr(data,mid+1,end) combineArr(data,start,mid,end) } } function combineArr(data,start,mid,end){ let i = start,j=mid+1; let m = mid, n=end; let k = 0; let temp = [] while(i<=m && j<=n) { if (data[i]<=data[j]) temp[k++] = data[i++] else temp[k++] = data[j++] } while(i<=m) temp[k++] = data[i++]; while(j<=n) temp[k++] = data[j++]; for ( i = 0; i < k; i++) { data[start + i] = temp[i] } console.log(temp) } breakArr(data3,0,15)
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!