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++){>

Dry Type Distribution Transformer

Henan New Electric Power Co.,Ltd. is a comprehensive enterprise integrating product research and development, product sales, and import and export services. It has more than 45 years of research and development experience. The company has a modern workshop of more than 120,000 square meters, a research and development center, and a staff apartment of more than 30,000 square meters. The production equipment is perfect, the testing method is perfect, the technical force is strong, and the qualification is complete. We can provide high, medium and low voltage transformers with voltages of 110KV and below and capacities of 120,000 kVA and below for various purposes, as well as tap changers, gas relays, oil level gauges, moisture absorbers, valves, thermometers, insulators, fans and other accessories .

Dry type transformers are widely used in local lighting, high-rise buildings, airports, terminals, CNC mechanical equipment and other places. Simply speaking, dry type transformers refer to transformers whose core and windings are not immersed in insulating oil.

Cooling methods are divided into natural air cooling (AN) and forced air cooling (AF). In case of natural air cooling, the transformer can operate continuously for a long time under rated capacity. In case of forced air cooling, the output capacity of transformer can be increased by 50%. It is applicable to intermittent overload operation or emergency overload operation; Due to the large increase of load loss and impedance voltage during overload, it is in a non economic operation state, so it should not be in a long-term continuous overload operation.

Dry Type Distribution Transformer,Dry Transformers,Air Cooling Transformer,Dry Power Transformer,Dry type epoxy resin transformer

Henan New Electric Power Co.,Ltd. , https://www.newelectricpower.com