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