Several sorting algorithms

Source: Internet
Author: User

1. Bubble sort (thought: Each cycle is started from the last, compared to the previous one, if it is smaller than the previous one, swap the position until it cannot be exchanged.) Slowest but most easily implemented)

<! DOCTYPE html>functionOrder (a) {varresult = "";  for(vari = 0; i < a.length; i++) {       for(varj = a.length; J > 0; j--) {            if(A[j] < a[j-1]) {                vartemp = A[j-1]; A[j-1] =A[j]; A[J]=temp; }} result+ = "First" + (i + 1) + "secondary cycle, Result:";  for(vark = 0; K < A.length; k++) {result+ = A[k] + ","; } result+ = "<br/>"; }    returnresult; }  varOpro = document.getElementById ("Pro"); varOarr = document.getElementById ("arr"); vararr = []; Opro.onclick=function(){     for(vari = 0; I < 5; i++) {Arr[i]= Math.ceil (Math.random () * (100-0) +0)    }; Oarr.innerhtml=arr; }  varOdo = document.getElementById ("Do"); Odo.onclick=function(){    varOresult = document.getElementById ("Result"); Oresult.innerhtml=order (arr); }</script></body>

2. Select sort (thought: each cycle is starting from the first, after each bit of comparison, after comparing all the digits, the smallest number in the first place)

<! DOCTYPE html>functionOrder (a) {varresult = ""; varmin;  for(vari = 0; i < a.length-1; i++) {min=i;  for(varj = i+1; J < A.length; J + +) {        if(A[min] >A[j]) {min=J; }      }      if(min! =i) {vartemp =A[min]; A[min]=A[i]; A[i]=temp; } result+ = "First" + (i + 1) + "secondary cycle, Result:";  for(vark = 0; K < A.length; k++) {result+ = A[k] + ","; } result+ = "<br/>"; }      returnresult; }  varOpro = document.getElementById ("Pro"); varOarr = document.getElementById ("arr"); vararr = []; Opro.onclick=function(){     for(vari = 0; I < 5; i++) {Arr[i]= Math.ceil (Math.random () * (100-0) +0)    }; Oarr.innerhtml=arr; }  varOdo = document.getElementById ("Do"); Odo.onclick=function(){    varOresult = document.getElementById ("Result"); Oresult.innerhtml=order (arr); }</script></body>

3. Insert sort (thought: the outer loop is used to move the array, the inner loop is used to compare the selected elements in the outer loop and the elements behind it)

  functionOrder (a) {varresult = "";  for(vari = 1; i < a.length; i++) {      vartemp=A[i];  for(varj=i-1;j>=0&&temp<a[j];j--) {a[j+1]=A[J];//move the value greater than temp to one unit after the whole} a[j+1]=temp; Result+ = "First" + (i + 1) + "secondary cycle, Result:";  for(vark = 0; K < A.length; k++) {result+ = A[k] + ","; } result+ = "<br/>"; }      returnresult; }

4. Quick sort (thought: Select a benchmark, less than its number is on the left, greater than his number is on the right, loop call function, implement sort)

    functionOrder (arr) {varresult = ""; if(Arr.length <= 1) {returnarr;} varPivotindex = Math.floor (arr.length/2); varPivot = Arr.splice (pivotindex, 1) [0]; varleft = []; varright = [];  for(vari = 0; i < arr.length; i++) {if(Arr[i] <pivot)    {Left.push (arr[i]); } Else{Right.push (arr[i]); }} result=order (left). Concat ([pivot], order (right)); returnresult; };

Several sorting algorithms

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.