JS array sorting method instance analysis

Source: Internet
Author: User
This article mainly introduces the JS array sorting method, and analyzes the principles and implementation skills of javascript Array Bubble sorting and selection sorting in combination with examples, for more information about how to sort JS arrays, see the examples in this article. We will share this with you for your reference. The details are as follows:

Method 1. Bubble Sorting

Train of Thought: Compare the first element and the second element in the array in sequence. If the first element is greater than the second element, the switching position is required. Therefore, two functions are required: The switching position function and the comparison function.

The number of rounds is the length of the array.

Var arr = [2, 58, 49,26, 34]; function change (f, s) {var temp = arr [f]; arr [f] = arr [s]; arr [s] = temp;} for (var I = 0; iarr [j + 1]) {change (j, + j + 1 );}}} alert (arr );

Method 2: Select sorting

Find the minimum value from the array, throw it to the first value of the array, and then cyclically operate from the remaining Array

Var arr = [, 34]; function change () {if (arr. length = 1) {return arr;} var iMin = arr [0]; var index = 0; for (var I = 0; I

The above is the sample analysis content of the JS array sorting method. For more articles, please follow the PHP Chinese Network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.