Implementation and explanation of the sort algorithm in javascript (JS note) and javascript99js
Bubble Sorting
The principle of bubbling is to "float" the maximum or minimum element.
Insert sorting, select sorting, fast sorting, and Bubble Sorting
Ideas
Compare two adjacent numbers in sequence, place decimal places in front, and place large numbers in the back.
Step 1: Compare the numbers of 1st and 2nd, and place the decimal places before and after the large numbers. Compare the numbers of 2nd and 3rd. Place the decimal places before the decimal places, and then move on until the last two digits are compared. Place the decimal places before and after the decimal places.
Step 2: In the second place: Compare the number of decimal places from the first logarithm (because the number of 2nd is exchanged with the number of 3rd, the number of 1st is no less than 2nd, after a large number is placed, it is always compared to the second to the last (the highest position on the last), and the second row ends, get a new maximum number at the penultimate position (in fact, it is the second largest number in the entire series ).
In this case, repeat the above process until the sorting is completed.
Because the sorting process always places decimal places forward and large numbers backward, it is equivalent to bubbles rising, so it is called Bubble sorting.
Animation Effect of Bubble Sorting
Implementation: This code segment is relatively simple. It is the most basic and basic code in the algorithm...
Pay attention to three points
1. The method of the exchange class can be solved using a = [B, B = a] [0] In javascript,
Replace
Copy codeThe Code is as follows:
Var, a, B, temp
Temp =;
A = B;
B = temp
This exchange method
2. Pay attention to the cache of loop variables. array. length is cached here.
3. Pay attention to the nested loop, from the first number to the last n number, and n is the Compare step number.
Function bubbleSort (array) {var l = array. length; for (var I = 0; I <l; I ++) {// compare the number of steps to the length of the array for (var j = 0; j <l-I; j ++) {// The number of embedded exchanges is from the first number to the last-in-n number, n indicates the number of compare steps. if (array [j] <array [j-1]) {array [j] = [array [j-1], array [j-1] = array [j] [0] // here the switching element }}for (var k = 0; k <l; k ++) {console. log (array [k] + ",");} console. log ('this is the nth '+ (I + 1) + 'secondary sequence')} var a = [6, 54, 6, 22, 5, 7, 8, 2, 34]; bubbleSort ();
Animation Effect
Insertion Sort)
It's very simple. It's just a step for us to touch the card!
Ideas:
1. Assume that we touch a card. Currently, all the cards in our hands are set to empty = [] and a push (arr [0]).
2. Take out the next card, set it to a, and scan all the cards in empty (sorted) from the back to the front.
3. If you have this card in your hand, empty [empty. length-n] (sorted) is greater than the new element, move the card to the next position (Teng space) empty [empty. length-n] = empty [empty. length-n + 1]
4 repeat Step 3 until the sorted card empty [empty. length-n] is smaller than or equal to
5. Insert a to this position. empty [empty. length-n] =
6. Repeat Step 2.
However, javascript code is somewhat difficult to implement. The Code is as follows:
Function insert (arr) {var l = arr. length; var empty = []; // empty array, which indicates our mobile empty. push (arr [0]); // We first touch a for (var I = 1; I <l; I ++) {// note that the starting point here is 1, because we have already touched one! If (arr [I]> empty [empty. length-1]) {empty [empty. length] = arr [I]} // if it is larger than the ordered array empty, put it directly at the end of the for (var j = empty. length; j> 0 & arr [I] <empty [j-1]; j --) {// compare the maximum value with that of arr to leave arr blank. When arr <a bit of an ordered array, it does not need to be moved. Empty [j] = empty [j-1]; // move empty to the right [j-1] = arr [I]; // place the value in the empty location} // console. log (empty)} return empty}
The important knowledge point here is the & symbol, indicating "and". That is, the expressions are valid only when both conditions are met.
& Symbol can also replace if (a) {fun ()} with a & B
Another important point is
If the array is set to arr, its "last item" is arr [arr. length-1].
Sort Animation
Selection sort)
It is also a simple sorting algorithm.
Ideas:
Find the smallest element-to the array-to the small one-to the array, and so on.
First, find the minimum element in the unordered array. The method can be used to constantly judge and assign values, that is, set array [0] as the minimum element of the first element of the array, then the serial number of the "minimum element" in the array is 0.
Then traverse the array. If the second element of the array is smaller than the other element, it indicates that the second element is the smallest element and "0" is updated to "1 ".
After the traversal, we will know that the smallest element subscript of this series is "n"; directly store it to the starting position of the sorting sequence (array [n])
Then, find the smallest element from the remaining unordered elements and put it at the end of the sorting sequence. Note that the subscript for traversal starts from 1. Because we have picked out a minimum element.
And so on until all elements are sorted.
Function selectSort (array) {var min; var l = array. length; // The cache length for (var I = 0; I <l; I ++) {// starts a loop, which contains a total of 1 cycles, we can find out l elements min = I; // assume that the first element is the smallest element for (var j = I + 1; j <l; j ++) {// start from the first loop, traverse if (array [min]> array [j]) // determine whether the result is smaller than the previous min = j; // update the "minimum" subscript} if (min! = I) {// here, because operations are performed in the same number of groups, you can directly exchange elements. For example, if the first entry of the array is I, then I find that the minimum element is array [min], then I need to swap this min with I. And so on. Array [I] = [array [min], array [min] = array [I] [0]; // exchanged element} return array ;}
Here, we still note that the exchange method is array [I] = [array [min], array [min] = array [I] [0].
You can easily switch array [I] to array [min ~
Sort Animation
Quick sorting
Quick sorting is currently the most powerful sorting algorithm, which uses recursive thinking.
Ideas
Pick out an element from the array, which is called a "benchmark". This can be directly picked out using length/2.
Traverse the array. All elements are placed before the benchmark value smaller than the benchmark value, and all elements are placed behind the benchmark value larger than the benchmark value (the same number can reach either side ). Generally speaking: Male station on the left and female station on the right ..
Then we get an array rArray consisting of such array = smaller than the benchmark components lArray + benchmark + larger than the benchmark components.
Then, we only need to process lArray and rArray in the same way ~
This requires recursive writing. After processing, lArray is further divided into lArray benchmarks, which are smaller than lArray benchmarks and larger than lArray benchmarks ..
So we continue to operate, male station on the left, female station on the right ..
Until we find that the length of lArray is 1, which is not enough to be further divided, we think the sorting is over.
Function quickSort (arr) {var l = arr. length; // The length of the cached array if (arr. length <= 1) {return arr}; // if the length of lArray and rArray we get is smaller than that of 1, we don't need to arrange it ~ Var num = Math. floor (arr. length/2); // obtain the number in the middle of the array. Note that length/2 is not necessarily an integer. use Math. floor is rounded to var numValue = arr. splice (num, 1) [0]; // use the splice method to obtain an element. Pay attention to the syntax var left = []; // create the left-side reference container var right = []; // create the right-side reference container for (var I = 0; I <l; I + = 1) {// start traversing the array arr [I] <numValue? Left. push (arr [I]): right. push (arr [I]); // left of male station and right of female station ..} Return quickSort (left). concat ([numValue], quickSort (right) // recursion, continue to operate on the left and right arrays .}
Animation effect:
Pay attention to arr. although splice (num, 1) only extracts one number, but the result of splice is also an array, which requires [0]. Otherwise, a bunch of arrays (1) will appear very strange)...
Splice reference: http://www.bkjia.com/w3school/js/jsref_splice.htm
Math. floor is the reference http://www.bkjia.com/w3school/js/js_obj_math.htm for Math objects
What is recursion: http://baike.baidu.com/view/96473.htm
In addition to quick sorting, the above four algorithms are all simple sorting algorithms, which are frequently used during the interview ~
Here, we still need to emphasize that the above algorithms use a lot of loop and array-related knowledge and must be familiar with them!