Home > Web Front-end > JS Tutorial > Detailed explanation of js merge algorithm examples

Detailed explanation of js merge algorithm examples

小云云
Release: 2018-03-16 16:24:50
Original
1213 people have browsed it

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)
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template