Algorithmic Tours | Select Sort method
HTML5 Academy-code Carpenter: The fast calculation and sequencing of data is directly related to the performance of the front page. Since there are many algorithms for sorting, in this "algorithm series" sharing, we first start with a simple and easy to choose the sorting method, the other sorting algorithm will be followed to share with you.
What is the basic concept algorithm of the algorithm and what does it do?
The methods and steps taken to solve a problem are called algorithms.
We can consider the algorithm as a "FU word paper-cut tutorial", wherein each algorithm is a paper-cut tutorial in a "fixed step" of the paper-cut method, users just follow the steps to cut paper, you can cut out good luck.
There are so many algorithms, the efficiency of different algorithms to solve the problem is different, suitable for different scenarios. As the scale of the problem increases, the gap between the algorithms can become inaccessible. Increasing the efficiency of problem solving depends not only on the choice of fast hardware, but also on the selection of an effective (suitable) algorithm.
Usage Scenarios for sorting
Sorts from large to small (or small to large) for the array. For example: in the management system according to the rank of the score, according to the reading quantity to the article sorting and so on.
The fast calculation and sequencing of data is directly related to the performance of the front page. (for example, there are 10,000 of the data on the page need to be sorted by JS, the time difference between different algorithms is very large, directly affect the user experience of the site)
Common sorting methods
The more common sort methods include: bubble sort, selection sort, quick sort, binary insertion sort, etc.
Since there are many algorithms for sorting, in this "algorithm series" sharing, we first start with a simple and easy to choose the sorting method, the other sorting algorithm will be followed to share with you.
The basic principle of choosing the sorting method
The smallest number in the sequence is first found, and it is swapped with the first number in the sequence;
Next, continue this operation in the remaining sequence: find the smallest number, and swap it with the second number in the sequence;
And so on, until the complete sequence is sorted.
In short, select Sort is----continuously selects the smallest person in the remaining sequence, and then exchanges the position with the "first" digit of the unsorted column.
Case description
In the following array, the black stands for sorting, and the blue represents the sorted
To implement the selection sort step decomposition sort count
Sort count: The sequence length –1 (note, not the number of comparisons);
Because the last number in the sequence does not need to be compared again, the number of sorts is the sequence length –1.
Find the smallest number
The smallest number is found in the sequence and the index value of the number is recorded;
Since Minindex starts at 0 by default, the first number does not need to be compared to itself, so j = i + 1;
- Traverse the sequence to find the smallest number
- for (var j = i + 1; j < Len; J + +) {
- if (Arr[j] < Arr[minindex]) {
- Index of minimum number of records
- Minindex = j;
- };
- };
Multiple traversal within a sort number finds the smallest number, so you need to use a For statement to control it.
- Number of orders
- for (var i = 0; i < len-1; i++) {
- The index of the default minimum number is I
- Minindex = i;
- Traverse the sequence to find the smallest number
- for (var j = i + 1; j < Len; J + +) {
- if (Arr[j] < Arr[minindex]) {
- Index of minimum number of records
- Minindex = j;
- };
- };
- };
Two-digit exchange position
Using the TEMP variable, the exchange of values between the two array elements is realized, i.e. the interaction position.
- temp = Arr[i];
- Arr[i] = Arr[minindex];
- Arr[minindex] = temp;
Select the sorting method complete code
- var arr = [9, 8, 3, 1, 2, 4],
- Len = Arr.length,
- Minindex, temp;
- Number of orders
- HTML5 Academy
- for (var i = 0; i < len-1; i++) {
- The index of the default minimum number is I
- Minindex = i;
- Traverse the remainder of the sequence to find the smallest number
- for (var j = i + 1; j < Len; J + +) {
- if (Arr[j] < Arr[minindex]) {
- Index of minimum number of records
- Minindex = j;
- };
- };
- HTML5 Academy Productions
- Two-digit interaction location
- temp = Arr[i];
- Arr[i] = Arr[minindex];
- Arr[minindex] = temp;
- };
- Console.log (arr);
The basic concept of the complexity of the efficiency algorithm of choosing the sorting method
The complexity of the algorithm is divided into time complexity and space complexity (time and space are the most important resources of computer, so the complexity is divided into time and space).
Time complexity: Refers to the computational effort required to perform the algorithm;
Spatial complexity: Refers to the amount of memory space required to execute the algorithm.
Complexity of Time: O (n*n)
Time complexity is the item (without coefficients) that is most affected by the change of n in the expression of total operation number.
The first cycle compares n-1 times, then n-2 times, n-3 times, and so on, the last cycle compared 1 times, the total number of comparisons and for (n-1 + 1) * N/2, that is, the time complexity of the comparison operation is O (n^2)
Tips: The number of comparisons selected for sorting is independent of the initial ordering of the sequence.
Space complexity: O (1)
The sorting algorithm requires an extra space (the TEMP variable) to swap the position of the element.
An algorithm for unstable ordering
Select Sort is an unstable sort algorithm.
For example: Sequence [3, 8, 3, 1, 9], the first loop 1th element 3 will and 1 exchange, become [1, 8, 3, 3, 9], at this time, the original sequence in the order of two 3 is destroyed.
Algorithmic Tours | Select Sort method