Home > Web Front-end > JS Tutorial > Detailed explanation of array sorting in js

Detailed explanation of array sorting in js

零下一度
Release: 2017-06-26 11:17:48
Original
1388 people have browsed it

When sorting arrays in JavaScript, I summarized three methods: sort sorting, bubble sorting and selection sorting.

var arr = [1,22,3,4,5,6,7,8,9,10];

sort sorting:

It is implemented using the method sort() in the array.

<span style="color: #ff0000">function <strong><code class="js plain">systemSort (arr)##{
# return arr.sort(
#                                                                                                                                                                      ##{ return a - b; }## );
## }
Bubble sorting
## The bubble method uses array traversal for comparison. Continuous comparison will traverse the minimum or maximum values ​​one by one.
                                                                                                                                                                                                        var i = 0; i < arr.length-1; i++) {//How many rounds to compare 9## for (var j = 0; j < arr .length-1-i; j++) {//The number of comparisons in each round

                                                                                                                                                                                                     
                                var temp = arr[j];
##                              arr[j] = arr[j+1 ];#                                       arr[j+1] = temp;

##                            }
                                                                                                                 #Selection sort:
The selection sort method is to use the first data of the array as the maximum or minimum value, and then output an ordered array through a comparison loop.                                                                                                             

FUNCTION Selectsort1 (ARR) {
## For (var I = 0; I & LT; Arrr.Length-1) {//// /Comparison rounds of the outer loop
## Number
counter (ARR [J] & GT; ARR [J+1]) { v varte = arr[j];
#                                                    arr[j +1] = temp;
##                                                                   ##                                                                                                                                                               

The above is the detailed content of Detailed explanation of array sorting in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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