Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the top ten sorting algorithm of JavaScript?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the top ten sorting algorithms of JavaScript". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the top ten sorting algorithms of JavaScript".

Bubbling sort

Through the comparison and exchange of adjacent elements, each loop can find the maximum or minimum value of the unordered array.

Best: O (n), you only need to bubble the array once to order it.

Worst: O (n ²)

Average: O (n ²)

Unidirectional bubbling

1. Function bubbleSort (nums) {2. For (let iTun0, len=nums.length; ilow; jmurm -) {16. If (nums [j])

< nums[j-1]) { 17. [nums[j], nums[j-1]] = [nums[j-1], nums[j]]; 18. mark = false; 19. } 20. } 21. low++; 22. if(mark) return; 23. } 24. } 选择排序 和冒泡排序相似,区别在于选择排序是将每一个元素和它后面的元素进行比较和交换。 最好:O(n²) 最坏:O(n²) 平均:O(n²) 1. function selectSort(nums) { 2. for(let i=0, len=nums.length; i= right) return; 5. let index = partition(arr, left, right); 6. recursive(arr, left, index - 1); 7. recursive(arr, index + 1, right); 8. return arr; 9. } 10. // 将小于基数的数放到基数左边,大于基数的数放到基数右边,并返回基数的位置 11. function partition(arr, left, right) { 12. // 取第一个数为基数 13. let temp = arr[left]; 14. while(left < right) { 15. while(left < right && arr[right] >

= temp) right--; 16. Arr [left] = arr [right]; 17. While (left

< right && arr[left] < temp) left++; 18. arr[right] = arr[left]; 19. } 20. // 修改基数的位置 21. arr[left] = temp; 22. return left; 23. } 24. recursive(nums, 0, nums.length-1); 25. } 快速排序之交换 从左右两边向中间推进的时候,遇到不符合的数就两边交换值。 1. function quickSort1(nums) { 2. function recursive(arr, left, right) { 3. if(left >

= right) return; 4. Let index = partition (arr, left, right); 5. Recursive (arr, left, index-1); 6. Recursive (arr, index + 1, right); 7. Return arr; 8.} 9. Function partition (arr, left, right) {10. Let temp = arr [left]; 11. Let p = left + 1; 12. Let Q = right 13. While (p nums [j+gap]) {12. [nums [j], nums [j + gap]] = [nums [j+gap], nums [j]]; 13.} 14. Else {15. Break; 16.} 17.} 18.} 19. Gap = parseInt (gap / 2); 20.} 21. } Thank you for your reading, the above is the content of "what are the top ten sorting algorithms of JavaScript". After the study of this article, I believe you have a deeper understanding of what the top ten sorting algorithms of JavaScript are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report