Classic sorting algorithm and JS implementation case analysis

Classic sorting algorithm bubble sorting principle:

1. Compare adjacent elements and swap positions if the first one is larger than the second.
2. Repeat the above steps to get the maximum value and the next largest value. . . .
3. Repeat the above steps until there is no pair of numbers to compare

Analysis of Algorithms:

1. If the initial state of the file is positive, a scan is completed. The number of comparisons C of the required keywords and the number M of recording movements reach a minimum value: Cmin=n-1, Mmin=0, so the time complexity of bubble sorting O(n);
2. If the initial file is reversed, you need to do n-1趟 sorting. Each time the order is sorted, ni(1<=i<=n-1) keywords are compared, and each comparison must be moved 3 times (such as swapping a[i-1] and a[i]; tmp= a[i-1]; a[i-1]=a[i]; a[i]=tmp) to reach the position of the exchange record. At this time, the comparison and the number of movements reach the maximum value: Cmax=n*(n-1)/2=O(n2); Mmax=3n*(n-1)/2=O(n2), (according to the difference The series is evaluated.) The worst time complexity of bubble sorting is O(n2).
In summary, the total average time complexity of bubble sorting is O(n2);
Summary: Average time complexity: O(n2), Stability: Stable, Space complexity: O(1)

Code implementation: vararr=[1,5,3,2]; funcTIonbubbleSort(arr){for(leTI=0,l=arr.length;ifor(letj=i+1;jif(arr[i]>arr[j ]){lettemp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } }returnarr; } bubbleSort(arr); Select the sorting principle: each time from the record to be sorted Select the record with the smallest keyword, and put it to the end until the end of all sorts; j++){>;i++){>

1. From the to-be-sorted, find the smallest element
2. If the smallest element is not in the first element of the sorting sequence, exchange with the first element
3. From the remaining n-1 elements, find the smallest element, repeat steps 1, 2

Analysis of Algorithms:

1. Time complexity: O(n2)
2. Space complexity: O(1)

Code implementation: vararr1=[5,3,2,1]; funcTIonselectSort(arr){letlen=arr.length,minIndex;//minIndex records the smallest index for(vari=0;iif(arr[j]if(i !=minIndex){vartemp =arr[i]; arr[i]=arr[minIndex];arr[minIndex]=temp;} }returnarr; } selectSort(arr1); insert sorting/insertainment sorting (inserTIon sort) principle: The idea of ​​inserting sorts is a bit like the way we play cards when playing cards. When playing cards, we put the cards we have in order into the cards that have been sorted in order.

1. Compare the number in the second position with the number on the left and put it in the right position (equivalent to having a card in hand and grabbing a card)
2. Compare the numbers of the i positions with the numbers to the left of the position, and put them in the appropriate position.
3. Repeat the above steps until the sorting is complete.

Analysis of Algorithms:

1. Time complexity: O(n2)
2. Space complexity: O(1)

Code implementation: var arr1=[5,4,3,2];functioninsertionSort(arr){var cur,preIndex;for(vari=1;i =0&& arr[preIndex]>cur){ arr[preIndex+1]=arr[preIndex]; preIndex--; }arr[preIndex+1]=cur; }returnarr; }insertionSort(arr1); merge sort Principle: It is a method of combining two sequential sequences into one sequential sequence.

经典排序算法和JS实现

Analysis of Algorithms:

1. Algorithm stability: stable
2. Time complexity: O(n*log2n), the form of merge sorting is a binary tree, the number of times it needs to be traversed is the depth of the binary tree, and according to the complete binary tree
3. Space complexity: n

Code implementation: functionmergeSort(arr){if(arr.length==1){returnarr; }//First divide the unordered array into two arrays varmid =Math.floor(arr.length/2);varleft = arr. Slice(0,mid);varright = arr.slice(mid);returnmerge(mergeSort(left),mergeSort(right)); }functionmerge(left,right){varre=[];while(left.length>0&& right .length>0){if(left[0] This merge sorting code is fairly straightforward, but the mergeSort() function causes very frequent self-calls. An array of length n will eventually call mergeSort() 2*n -1 time, which means that if the array size that needs to be sorted is large, a stack overflow error will occur on some small stack browsers.
Supplement: the size limit of the browser stack, you can use the following code

Varcnt =0;try{ (function(){cnt++;arguments.callee(); })(); }catch(e) { console.log(e.message, cnt);

To prevent code that encounters stack overflows, recursively changed to iteration:

Functionmerge(left, right){varresult = [];while(left.length && right.length) {if(left[0] < right[0]) result.push(left.shift());elseresult.push( Right.shift()); }returnresult.concat(left, right); }functionmergeSort(a){if(a.length ===1)returna;varwork = [];for(vari =0, len = a. Length; i < len; i++) work.push([a[i]]); work.push([]);// If the array length is odd for(varlim = len; lim >1; lim = ~~( (lim +1) /2)) {for(varj =0, k =0; k < lim; j++, k +=2) work[j] = merge(work[k], work[k +1]) ; work[j] = [];// If the array length is odd}returnwork[0]; } quick sort (quick sort) principle: recursive divide and conquer based on bubble sorting

1. More comprehensive one of your explanation URLs, I like

Analysis of Algorithms:

1. Worst time complexity O(n2)
2. Average time complexity: O(nlogn)

functionquickSort(arr){if(arr.length<=1){ return arr; }let leftArr =[]; let rightArr =[]; let q = arr[0];for(leti=1,l=arr.length ;ii++){ if(arr[i]>q) { rightArr.push(arr[i]); }else{ leftArr.push(arr[i]); }}return[].concat(quickSort(leftArr), [q],quickSort(rightArr));;>[0]){>[minindex]){>;i++){>

Safe Lock Motor

Lock Motor product introduction:

Safe Lock Motor, also known as The anti-theft lock motor, in the field of electronic Lock technology, Safe Lock Motor is a mechanical Lock components associated with the electronic control unit of the important parts.


Safe Lock Motor the product effectively combines the advantages of traditional electronic locks and electromagnetic lock overcame the electric lock too much noise, electromagnetic lock power shortage and the development of new products, and spiritual compared with electric control lock and magnetic locks, lock its performance more perfect, more obvious advantages.


Functions: it is widely used in various intelligent locks, such as Shared bicycle lock, fingerprint lock, glass lock, safe lock, hotel lock, family lock, inductive smart lock, etc


Features: Motor For Safe Lock is based on micro precision reducer development, small intelligent micro precision reducer can do 10 mm in diameter, is widely used in safe, locks, smart locks, sensor intelligent lock;

This kind of motor has the characteristics of low noise, low energy consumption, small volume, light quality, high precision, large torque and durability

Method of use: the best stable in horizontal plane, installed on the Safe Lock Motor output shaft parts, cannot use a hammer to knock, knock prone to press into the dc Safe Lock Motor drive, may cause damage to internal components, and cannot be used in the case of blocked.

Lock motor

Operating temperature range:

Gear Motor For Safe Lock should be used at a temperature of -10~60℃.

The figures stated in the catalog specifications are based on use at ordinary room temperature catalog specifications re based on use at ordinary room temperature (approximately20~25℃.

If a Safe Lock Motor is used outside the prescribed temperature range,the grease on the gearhead area will become unable to function normally and the motor will become unable to start.Depending on the temperature conditions ,it may be possible to deal with them by changing the grease of the motor's parts.Please feel free to consult with us about this.


Storage temperature range:

Gear Motor For Safe Lock should be stored ta a temperature of -15~65℃.

In case of storage outside this range,the grease on the gearhead area will become unable to function normally and the motor will become unable to start.


Service life:

The longevity of Dc Motor For Safe Lock is greatly affected by the load conditions , the mode of operation,the environment of use ,etc.Therefore,it is necessary to check the conditions under which the product will actually be used .The following conditions will have a negative effect on longevity.Please consult with us should any of them apply.



●Use with a load that exceeds the rated torque

●Frequent starting

●Momentary reversals of turning direction

●Impact loads

●Long-term continuous operation

●Forced turning using the output shaft

●Use in which the permitted overhang load or the permitted thrust load is exceeded

●A pulse drive ,e.g.,a short break,counter electromotive force,PWM control

●Use of a voltage that is nonstandard as regards the rated voltage

●Use outside the prescribed temperature or relative-humidity range,or in a special environment.

●Please consult with us about these or any other conditions of use that may apply,so that we can be sure that you select the most appropriate model.


when it come to volume production,we're a major player as well .each month,we rurn out 600000 units,all of which are compliant with the rohs directive.Have any questions or special needed, please contact us, we have the engineer group and best sales department to service to you

Looking forward to your inquiry. Welcome to our factory.

Lock motor

Safe Lock Motor,Motor For Safe Lock,Dc Motor For Safe Lock,Gear Motor For Safe Lock

Shenzhen Shunchang Motor Co., LTD. , https://www.scgearmotor.com

Posted on