We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
归并排序把数据拆分成小的数据块,然后再排序合并
const mergeSort = (arr) => { let len = arr.length, left = [], right = []; if(len === 1){ return arr; } let mid = Math.floor(len/2); for(let i = 0; i < len; i ++) { if(i>= mid){ right.push(arr[i]); } else { left.push(arr[i]); } } return merge(mergeSort(left),mergeSort(right)); } const merge = (left,right) => { let temp = [], leftIdx = 0, rightIdx = 0, leftLen = left.length, rightLen = right.length; while(leftIdx < leftLen && rightIdx < rightLen){ if(left[leftIdx] <right[rightIdx]) { temp.push(left[leftIdx]); leftIdx ++; } else { temp.push(right[rightIdx]); rightIdx ++; } } while(leftIdx < leftLen){ temp.push(left[leftIdx]); leftIdx ++; } while(rightIdx < rightLen){ temp.push(right[rightIdx]); rightIdx ++; } return temp; } console.log(mergeSort([9,8,7,6,5,4,3,2,1]));
The text was updated successfully, but these errors were encountered:
No branches or pull requests
归并排序把数据拆分成小的数据块,然后再排序合并
The text was updated successfully, but these errors were encountered: