Home >Web Front-end >JS Tutorial >Detailed explanation of js merge algorithm examples
This article mainly shares with you the detailed explanation of js merging algorithm examples, hoping to help everyone.
Recursively split the array into individual elements and then merge the arrays
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)
The above is the detailed content of Detailed explanation of js merge algorithm examples. For more information, please follow other related articles on the PHP Chinese website!