The basic knowledge of JS implementation method of several classical sorting algorithms

Source: Internet
Author: User

One. Bubble sort

function Bubblesort (array) {var length = Array.Length;
      for (var i = length-1 i > 0; i--) {//used to narrow the scope for (var j = 0; J < i; J +) {//bubble in scope, the biggest one in the range will take the last side
        if (Array[j] > Array[j+1]) {var temp = array[j];
        ARRAY[J] = array[j+1];
      ARRAY[J+1] = temp;
    } console.log (array);
  Console.log ("-----------------------------");
} return array;
} var arr = [10,9,8,7,7,6,5,11,3];
var result = Bubblesort (arr); 
Console.log (result); /* [9, 8, 7, 7, 6, 5, ten, 3, one]-----------------------------[8, 7, 7, 6, 5, 9, 3, ten, one]------------------------- ----[7, 7, 6, 5, 8, 3, 9, ten, one]-----------------------------[7, 6, 5, 7, 3, 8, 9, ten, one]----------------------- ------[6, 5, 7, 3, 7, 8, 9, ten, one]-----------------------------[5, 6, 3, 7, 7, 8, 9, ten, one]--------------------- --------[5, 3, 6, 7, 7, 8, 9, ten, one]-----------------------------[3, 5, 6, 7, 7, 8, 9, ten, one]------------------- ----------
[ 3, 5, 6, 7, 7, 8, 9, 10, 11] * * 

Two. Select Sort

function Selectionsort (array) {var length = Array.Length; for (var i = 0; i < length; i++) {//narrow the range of selection var min = array[i];//Assume that the first of the range is the minimum var index = i;////record minimum subscript f
        or (var j = i + 1; j < length; J + +) {//select Minimum if (Array[j] < min) {min = array[j];
      index = j;
      } if (index!= i) {//to swap the range minimum to the range of the first var temp = array[i];
      Array[i] = Array[index];
    Array[index] = temp;
    } console.log (array);
  Console.log ("---------------------");
} return array;
} var arr = [1, 10, 100, 90, 65, 5, 4, 10, 2, 4];
var result = Selectionsort (arr);
Console.log (result); /* [1, M, 5, 4, 2, 4]---------------------[1, 2, 5,, 4,, ten, 4]------------------- --[1, 2, 4, 5,, 4]---------------------[1, 2, 4, 4,, 5, M, A, m]------------------- --[1, 2, 4, 4, 5,, $, ten,]---------------------[1, 2, 4, 4, 5, 10, 100, 65, 10, 90]---------------------[1, 2, 4, 4, 5, ten,,, M]---------------------[1, 2, 4, 4, 5, 10, 10, 65, 100, 90]- --------------------[1, 2, 4, 4, 5, ten,,, M]---------------------[1, 2, 4, 4, 5, 10, 10, 65, 90, 100]- --------------------[1, 2, 4, 4, 5, 10, 10, 65, 90, 100] * *

Three. Insert Sort

function Insertionsort (array) {var length = Array.Length;
    for (var i = 0; i < length-1 i++) {//i represents a sorted sequence last subscript var insert = array[i+1]; var index = i + 1;//record to be inserted subscript for (var j = i; j >= 0; j--) {if (Insert < Array[j]) {//the item to be inserted is smaller than it, to
        Post-Mobile array[j+1] = array[j];
      index = j;
    } Array[index] = insert;
    Console.log (array);
  Console.log ("-----------------------");
} return array;
} var arr = [100,90,80,62,80,8,1,2,39];
var result = Insertionsort (arr);
Console.log (result); /* [8, 1, 2, M]-----------------------[+,,, 1, 2, M]---------------------"." --[+, 8, 1, 2, M]-----------------------[+, 8, 1, 2, M,]--------------------- --[8, +, 1, 2, M]-----------------------[1, 8, +, 2, M,]--------------------- --[1, 2, 8, 2, M, M, M,]-----------------------[1,, 8, 1, 2, 8, 39, 62, 80, 80, 90, 100]-----------------------[+] 

Four. Hill sort

function Shellsort (array) {var length = Array.Length;
  var gap = Math.Round (LENGTH/2);
      while (Gap > 0) {for (var i = gap; i < length; i++) {var insert = Array[i];
      var index = i;
          for (var j = i; j >= 0; j-=gap) {if (Insert < Array[j]) {Array[j+gap] = array[j];
        index = j;
    } Array[index] = insert;
    } console.log (array);
    Console.log ("-----------------------");
  Gap = Math.Round (GAP/2-0.1);
} return array;
} var arr = [13, 14, 94, 33, 82, 25, 59, 94, 65, 23, 45, 27, 73, 25, 39, 10];
var result = Shellsort (arr); 
Console.log (result); /* [[13, 14, 39, 10, 65, 23, 45]-----------------------[+] [+,],,,,, and, and -----------------------[13, 10, 39, 14, 45, 23, 59, 25, 65, 25, 73, 27, 82, 33, 9, at the same. 4, he]-----------------------[a], [a], he, M, M, M, I, O,-------------------------------[10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94] * * 

Five. Merge sort

function MergeSort (array) {var length = Array.Length;
  if (length <= 1) {return array;
    else {var num = Math.ceil (LENGTH/2);
    var left = mergesort (array.slice (0, num));
    var right = MergeSort (Array.slice (num, length));
  Return to merge (left, right);
  } function merge (left, right) {Console.log (left);
  Console.log (right);
  var a = new Array (); while (left.length > 0 && right.length > 0) {if (left[0] <= right[0]) {var temp = Left.shift (
      );
    A.push (temp);
      else {var temp = Right.shift ();
    A.push (temp);
  } if (Left.length > 0) {a = A.concat (left);
  } if (Right.length > 0) {a = A.concat (right);
  } console.log (a);
  Console.log ("-----------------------------");
return A;
} var arr = [13, 14, 94, 33, 82, 25, 59, 94, 65, 23, 45, 27, 73, 25, 39, 10];
var result = MergeSort (arr);
Console.log (result); * [[] [94] [33] [33, he]-----------------------------[a] [a] [a] [82] [25] [+]-----------------------------[] [a] [25, 82] [59, 94] [2 5, the ",", "the"-----------------------------[A,,, and,] [A,], "," [A], ",", ",", "", ""--- --------------------------[[A] [a] [a] [a]-----------------------------[a] [a]--------------- --------------[A] [[a] [a] [a] [a] [a] [a] [a] [] [[]] [[a]]----------- ------------------[[M] [a] [a]-----------------------------[a] [a] [(a)] [ten, A]------- ----------------------[A, M, a] [A, M, M] [A, M, M, O]-------------------------- ---[13, 14, 25, 33, 59, 82, 94, 94] [10, 23, 25, 27, 39, 45, 65, 73] [10/13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65 , the-----.------------------------[10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94] * * 

Six. Quick Sort

 function QuickSort (array) {var length = Array.Length;
  if (length <= 1) {return array;
    else {var smaller = [];
    var bigger = [];
    var base = [Array[0]];
      for (var i = 1; i < length; i++) {if (Array[i] <= base[0]) {Smaller.push (array[i));
      else {Bigger.push (array[i]);
    } console.log (Smaller.concat (Base.concat (bigger)));
    Console.log ("-----------------------");
  return QuickSort (smaller). Concat (Base.concat (QuickSort (bigger)));
} var arr = [8, 10, 100, 90, 65, 5, 4, 10, 2, 4];
var result = QuickSort (arr);
Console.log (result); /* [5, 4, 2, 4, 8, M, M,,]-----------------------[4, 2, 4, 5]-----------------------[2, 4, 4]----- ------------------[2, 4]-----------------------[A, M, M, M]-----------------------[a]------ -----------------[A]-----------------------[2, 4, 4, 5, 8, A, m, M, m] */

Above this several classical sorting algorithm JS realization method is small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

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.