Summary of various sorting methods in data structure (JS implementation) _javascript skills

Source: Internet
Author: User

New technologies are constantly changing, and mastering some of the basics is a solid foundation for future learning technologies that are constantly being updated. Recently have nothing to do, in order to brush up on the previous data structure, the structure of the sorting algorithm with JS implementation, and at the end of this article embedded demo.

Simple sort

Bubble sort

Bubble sort is the simplest sort algorithm, the time complexity of n squared, the code is as follows:

function Bubblesort (array) {for
      (var i = 0; i < Array.Length; i++) {for
        (var j = array.length; j > 0; j--) {
          if (Array[j] < array[j-1]) {
            var temp = array[j-1];
            ARRAY[J-1] = array[j];
            ARRAY[J] = temp;
          }

        }
        /* Output Results
        /document.write ("This is the first + (i + 1) +" secondary cycle ·, the result is: ");
        for (var k = 0; k < array.length; k++) {
          document.write (Array[k] + ",");
        }
        document.write ("<br/>");
        /* Output end/
      }
    }

Direct Insert Sort

The direct insertion sort also belongs to the simple sort algorithm, the time complexity is also n squared, but the performance is slightly better than bubble sort, the code is as follows:

function Insertsort (array) {
      var temp;
      for (var i = 1; i < Array.Length i++) {
        var temp = array[i];
        for (var j = i; j > 0 && Temp < array[j-1]; j--) {
          array[j] = array[j-1];
        }
        ARRAY[J] = temp
        /* Output
        /document.write ("Cap? + i + "all over the sort result is:") for
        (var n = 0; n < Array.Length; n++) {
          document.write (Array[n] + ",");
        }

        document.write ("<br/>")/
        * output end/

      }
    }

Select sort

Selection is also a simple sorting algorithm, the time complexity is also n squared, performance is also slightly better than bubble sort, the code is as follows:

function Selectsort (array) {
      var min, temp;;
      for (var i = 0; i < Array.Length; i++) {
        min = i;
        for (var j = i + 1; j < Array.Length; J + +) {
          if (Array[min] > Array[j])
            min = j;
        }
        if (min!= i) {
          temp = array[i];
          Array[i] = array[min];
          Array[min] = temp;
        }
        /* Output/
        document.write ("The first + i +" The result of the order is: ") for
        (var n = 0; n < Array.Length; n++) {
          document.write (Array[n] + ",");
        }

        document.write ("<br/>")/
        * output end/

      }
    }

Complex sort

Hill sort

Hill sort is an upgrade of the insert Sort, and in 1959 Hill broke the time complexity of n squared by comparing the 22 comparison of simple sort to setting the step jump comparison, and hill sort according to the different time complexity of the step length by the best nlogn to the worst n squared. The code is as follows:

function Shallsort (array) {
      var increment = array.length;
      var i
      var temp;//Staging
      var count = 0;
      do {
        increment = Math.floor (INCREMENT/3) + 1;
        for (i = increment i < Array.Length i++) {
          if (array[i) < array[i-increment]) {
            temp = array[i];
            for (var j = i-increment J > 0 && Temp < array[j]; J-= increment) {

              array[j + increment] = Array[j] ;

            }
            Array[j + increment] = temp;
            /* Output Results *
            /count++;
            document.write ("<br/> + count +" The result of the order is: ") for
            (var n = 0; n < Array.Length; n++) {
              document.write (Array[n] + ",");
            }
            /* Output end/}} while
      (Increment > 1)

    }

Heap Sort

Heap Sorting is an upgrade that selects the sort, and by constantly building a large or small heap to select the largest or smallest value to put into the front of the queue to sort, the time complexity of the heap sort in any case is Nlogn, and the code is as follows:

function Heapsort (array) {var temp;
      var i; for (i = Math.floor (ARRAY.LENGTH/2); I >= 0; i--) {heapadjust (array, I, array.length-1)
        Heap} for (i = array.length-1 i >= 0; i--) {/* Swap the root node out/temp = array[i];
        Array[i] = array[0];

        ARRAY[0] = temp;
        /* The remaining array continues to be constructed into a large top heap * * Heapadjust (array, 0, i-1); /* Output result/document.write ("<br/> + (array.length-i). toString () +" The result of the order is: ") for (var n = 0; n < Array.Length;
        n++) {document.write (Array[n] + ",");
      , max) {var temp, J; temp = Array[start];//temp is the value of the root node for (j = 2 * start; J < Max; J *= 2) {if (J < Max && Array[j

        ] < Array[j + 1]) {//Get the subscript ++j of the larger child; } if (temp >= array[j]) breAk
        Array[start] = Array[j];
      start = j;

    } Array[start] = temp; }

Merge sort

Merge sort is the only stable sort in a complex sort, sorted by splitting and merging the arrays to be sorted, merging the sorting time complexity of n squared, the code is as follows:

Source source array//dest target array//s start subscript//t object subscript function Msort (source, dest, S, t) {var m;//Take median Var
      Dest2 = new Array ();
       
      if (s = = t) {Dest[s] = Source[s];
        else {m = Math.floor ((s + t)/2);
        Msort (source, Dest2, S, m);
        Msort (source, Dest2, m+1, T);
        Merge (Dest2, dest, S, M, T); /* Output result/document.write ("<br/> + ++count +") for (var n = 0; n < dest.length;
        n++) {document.write (Array[n] + ","); /* Output end/////////Two arrays are fused in order from small to large//source original array//dest sorted array//s first subscript//m second Array subscript//Total length function merge (source, dest, S, M, N) {for (var j = m+1, k = s; J <= N && s <= m;
        k++) {if (Source[s] < source[j]) {dest[k] = source[s++];
        else {Dest[k] = source[j++]; }//Add an ordered array of remaining rows to the end of the dest IF (S <= m) {for (var l = 0; l <= m-s; l++) {dest[k + L] = source[s+l]; } if (j <= N) {for (var l = 0; l <= n-j l++) {dest[k + L] = source[j+l]
          ; }
        
      }
    }

Quick Sort

A quick sort is the fastest sort of speed known, with a time complexity of NLOGN, with the following code:

 var count = 0;
      
      function QuickSort (array, low, high) {var temp;
        if (Low < high) {var KeyPoint = quicksorthelp (array, low, high);
        count++; document.write ("<br/>")? + Count + "á" for (var l = 0;) driving??. L < Array.Length;
        l++) {document.write (Array[l] + ",");
        } quickSort (Array, low, keypoint-1);
        

        QuickSort (Array, KeyPoint + 1, high); The function quicksorthelp (array, low, high) {While [low < high] {while [Low < high &&A mp
        Array[low] <= Array[high]) {high--;
        temp = Array[low];
        Array[low] = Array[high];
        Array[high] = temp;
        while (Low < high && Array[low] <= Array[high]) {low++} temp = Array[low];
        Array[low] = Array[high];

      Array[high] = temp;
    return to Low; }

The above data structure of the various sorting methods summary (JS implementation) is a small series to share all the content, hope to give you a reference, but also hope that we support 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.