JavaScript bubble Sort and selection sorting implementation code _javascript Tips

Source: Internet
Author: User

Nonsense not much said, directly to everyone to paste the code, the specific code as follows:

var array = [1,2,3,4,5]; ---> Service
/Efficiency---> For an ordered array efficiency highest
//flag true False for 
(var j = 0; J < array.length-1;j++) {
// -the number of times that the comparison is reduced after each sort completes
var isTrue = true;//If the array itself is ascending, direct output for
(var i = 0; i < array.length-j-1;i++) {
if (Array[i] > array[i+1])
{
var temp = array[i];
Array[i] = array[i+1];
ARRAY[I+1] = temp;
IsTrue = false;
}
}
document.write (array);
if (isTrue) {break
;
}
}
var array = [12,34,4,6,20];
Length-1 j = 0-length-1 for
(var j = 0; J < array.length-1;j++) {
var min = array[j];//1 3 34 4 345 5
var minindex = j;
for (var i = j + 1; i < array.length;i++) {
if (Array[i] < min) {
min = array[i];
Minindex = i
}
}
if (Minindex!= j) {
var temp = Array[minindex];
Array[minindex] = array[j];
ARRAY[J] = temp;
}
}

Here's how JavaScript implements three sorts: bubble sort, select sort, insert sort code

<script type= "Text/javascript" > var A;
    A = [66, 53, 11, 5, 4, 3, 2, 1]; /* Bubble Sort/* (function Maopaopaixu () {for (var i = 0; i < a.length-1; i++) {//comparison is length-1 for (var j = 0; J < a.length-1-I;
            J + +) {if (A[j] > a[j + 1]) {var tmp = a[j];
            A[J] = a[j + 1];
          A[j + 1] = tmp;
    alert (a);
    })();
    A = [66, 53, 11, 5, 4, 3, 2, 1];
      /* Select sort//(function Xuanzepaixu () {var min/* index of minimum Item * * *, TMP;
        for (var out = 0, out < a.length-1 out++) {//Compare the number of times is length-1 min = out;
          for (var inner = out + 1; inner < a.length inner++) {//This is a.length, not a.lenght-1, because the latter causes the 2nd item of the right to be unable to participate in the sort.
          if (A[inner] < a[min]) {min = inner;
          ///Move the smallest item to the left TMP = A[out];
        A[out] = a[min] a[min] = tmp;
    } alert (a);
    })();
    A = [66, 53, 11, 5, 4, 3, 2, 1]; /* Insert Sort/function Charupaixu () {for (var = 1; out < a.length; out++) {var tmp = a[out];
        var inner = out;
          while (A[inner-1] > tmp) {A[inner] = a[inner-1];
        --inner;
      } A[inner] = tmp;
    alert (a);
  })(); </script>

The above is a small set to introduce the JavaScript bubble sort and select the implementation code of the sorting, hope to help everyone, if you have any questions welcome to my message, small series will promptly reply to everyone!

Related Article

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.