Remember some sort of javascript algorithm

Source: Internet
Author: User

0, written in the first

There are many ways of sorting, this article just records my familiar algorithms;

I found a very interesting sort of algorithm on the site, the relevant algorithm to make a demonstration of animation, interested students can see!

Attach sortanimate website Link: http://jun-lu.github.io/SortAnimate/index.html

first, bubble sort

This is the most basic sort algorithm, which is the entry level algorithm!

Principle: 22 Detection, compare the relationship between the size of the two, big back lost!

1 functionBubblesort (arr) {2      for(vari=0;i<arr.length;i++){3          for(varj=0;i<arr.length-i-1;i++){4             if(arr[j]>arr[j+1]){5                 varTemp=arr[j+1];6arr[j+1]=Arr[j];7arr[j]=temp;8             }9         }Ten     } One     returnarr; A}
second, quick sort

Common, sort fast, but a bit of a waste of memory space!

Principle: The center of the intermediate reference point for comparison, small left, large put right, need another 2 array storage;

1 functionQuickSort (arr) {2     if(Arr.length <= 1) {returnarr;}3     varIndex=math.floor (ARR.LENGTH/2);4     varCenter=arr.splice (index,1) [0];5     varleft=[],right=[];6      for(vari=0;i<arr.length;i++){7Arr[i]<center?Left.push (Arr[i]): Right.push (Arr[i]);8     }9     returnQuickSort (left). Concat ([Center],quicksort (right));Ten}
third, insert sort

Principle: one-by-one detection, the largest to the last lost, every time to reduce the number of detections!

1 functionInsertsort (arr) {2     vararrlen=arr.length;3      for(vari=0;i<arrlen;i++){4         varIndex=0;5          for(varj=1;j<arrlen-i;j++){6             if(Arr[j]>arr[index]) index=J;7         }8         varTemp=arr[arrlen-i-1];9arr[arrlen-i-1]=Arr[index];Tenarr[index]=temp; One     } A     returnarr; -}
Iv. End of

Sorting algorithms also have the option of sorting, hill sorting, binary sorting, etc...

I will study later, and then add this article!

--------------------End---------------------

Remember some sort of javascript algorithm

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.