A few big sort ideas (written by JavaScript)

Source: Internet
Author: User
Tags benchmark

Hello! I'm super meow. Two ~ ~ ~ Today practiced a few interview questions, suddenly feel for a long time did not well summed up the sorting algorithm. Used to write a sort of c/java, this time with JS to summarize the next five sorting algorithm it:

1, bubble sort (common and frequent sorting algorithm):

Needless to say, bubble is a float up ~ ~ ~ that is, you need a comparison:

<script type= "Text/javascript" >//Bubble Sort, one-time contiguous two-digit ratio until the maximum number is found    varArray = [5,2,4,6,8]; vartemp; functionSort (array) { for(vari=0;i<array.length-1;i++){                for(varj=0;j<array.length-i-1;j++){                   if(array[j]>array[j+1]) {temp=array[j+1]; Array[j+1]=Array[j]; ARRAY[J]=temp;    }}} console.log (array); } sort (Array)//[2,4,5,6,8]</script>
Bubble

2. Insert Sort:

Insert Insert, of course, need to plug in the position of the plug. The simple understanding is that each loop to a number, the number of its front as an array, and then inserted in the array in the position of the plug:

<script type= "Text/javascript" >varArray = [5,2,4,6,8]; vartemp; //The insertion Sort is the first number, the first one is the array, the second number is compared to it, and it is inserted. Consider the first two as an array, and the third one to compare where you inserted the interpolation, and so on .    functionSort (array) { for(vari=0;i<array.length;i++){                for(varj=0;j<i+1;j++){                   if(array[i]<Array[j]) {Temp=Array[i]; Array[i]=Array[j]; ARRAY[J]=temp;    }}} console.log (array); } sort (Array)//[2,4,5,6,8]</script>
Insert

3. Select Sort:

Select a small number at a time (in ascending order), first think the first number of the smallest, in turn, compare the number behind it, there is a smaller record position, will this smaller record, if there are still a few, the more decimal as the benchmark, and then continue to find, if there is smaller than smaller, the record position, and then continue to find, Until the smallest one is found and the first number is exchanged. Next, start with the second number in the array, and then ~ ~ ~

  

<script type= "Text/javascript" >varArray = [5,2,4,6,8]; vartemp; //Select Sort, compare from the first number, encounter small with its record position, and then compare this number with the number behind it, if there is no exchange these two number, if there is, then continue to compare.     functionSort (array) { for(vari=0;i<array.length-1;i++){               varindex=i;  for(varj=i+1;j<array.length;j++){                   if(array[index]>Array[j]) {Index=J; }} Temp=Array[i]; Array[i]=Array[index]; Array[index]=Temp} console.log (array); } sort (Array)//[2,4,5,6,8]</script>
Select

4. Quick sort:

Quick sort, as we all know, the fastest, a lot of companies interview this exam, handwriting is still a bit difficult. We can master the C language version, written in other languages is very easy ... I am referring to the Nanyi teacher wrote, mainly to find a benchmark element, smaller than the benchmark element on the left, a larger than the benchmark element on the right, and then around as two arrays, respectively, find the respective Datum elements and then group ~ ~ ~

<script type= "Text/javascript" >varArray = [5,2,4,6,8]; vartemp; //Quick Sort: First find a datum element, smaller than it on the left array, large in the right array, and then separate in the left and right array to find the datum elements to compare    varquicksort=function(array) {if(array.length<=1) {               returnArray; }Else{               varPriotindex=math.floor (ARRAY.LENGTH/2);varPriot=array.splice (priotindex,1) [0]; varleft=[]; varright=[];  for(vari = 0; i < Array.Length; i++) {                   if(array[i]<Priot)                   {Left.push (array[i]); }                   Else{Right.push (array[i]); }               }               returnquicksort (left). Concat ([Priot],quicksort (right)); }} console.log (Quicksort (array));</script>
Quick

5. Merge sort:

The merge sort is similar to the quick row, the main is only uses the recursion, the grouping is compared again, the concrete realization is as follows:

//Merge Sort: Compare from two two, then four a group, then 8 a group compare    varArray = [5,2,4,6,8]; varMerge=function(left,right) {varArr=[];  while(left.length>0&&right.length>0){            if(left[0]<right[0]) {Arr.push (Left.shift ()); }Else{Arr.push (Right.shift ()); }        }        returnArr.concat (left,right); }    varmergesort=function(array) {if(array.length==1) {            returnArray; }Else if(array.length>1) {            varMiddle=math.floor (ARRAY.LENGTH/2), Left=array.slice (0, middle), right=Array.slice (middle); returnmerge (MergeSort (left), MergeSort (right)); }} console.log (MergeSort (array));
Merge

For the time being these five, the sorting algorithm also has many kinds, the heap sorts, the random fast platoon .... This is self-understanding, and compare the time complexity of each algorithm, space complexity to determine who is better, depending on the situation ~ ~

A few big sort ideas (written by JavaScript)

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.