------------ ------------ restore content begins
Bubble Sort with choice
Bubble Sort:
1. Bubble Sort
1> The main principle is that the two adjacent units, comparing the stored data,
If the first data cell is large, it will be two adjacent cells, the exchange data is stored.
2> Comparative main process is started from the first starting unit,
Begin the first cycle, a maximum value will be selected, placed at the end of the array elements,
After each cycle, a maximum value will be selected, on the last currently involved in the comparison unit.
3> The main core is stored in data exchange
Adjacent cells, the size of the data comparison, the larger value the first unit, the two units exchanged stored value
4> Let's look through the code to a specific bubble sort
eg.
var arr = [5,4,3,2,1], we come to the array using bubble sort order at discharge
First, we divided into two cycles.
The inner loop, each cycle a maximum comparison, then determines (two adjacent unit compares the data size, if the first
A larger data unit exchanged two data storage units, there must be an intermediate value exchange data, to temporarily store data)
code show as below:
The outer loop, the sort operation loop is executed, the number of cycles is a unit of the array minus 1-1
code show as below:
The inner loop is then placed in the outer loop. code show as below:
And then outputs the code as follows:
Finally, the optimized code:
Optimization inner layer: comparing the last value not participate in the next cycle so to -j
When the last unit cycles through the penultimate units, but does not participate in the comparison cycle participating -1
code show as below:
Optimization of the outer layer: If there are n cells participate sorting, only the last cycle n-1 times, so to -1
code show as below:
Finally, attach the complete code:
Selection Sort
2. Select Sort
1> main core: from the starting position, and after the comparison unit, if the trigger condition is stored indexing subscript of the unit.
End of cycle, if the index is not an index start position index, store data exchanged
2> main processes: the cycle starting position to define the default location of the minimum is located.
Cycle starts from a position next starting position,
If the location has a value less than the value in the index position, the index value is stored in the location of this
End of the cycle, comparing the stored index, if the index is the starting position
If not, then the exchange value of the two positions, the minimum cycle will be present, is placed in the cycle start position.
Performing multiple cycles to complete the order.
3> Let's look through the code to select specific sort
eg. var arr = [3,44,38,5,47,25,36,2,79,8,1] using the selected sorting to sort
First, we divided into two cycles.
The outer loop: the default start position of the first minimum value is sequentially stored in the index start position, i.e. J .
code show as below:
The inner loop: if the value of the unit, corresponding to the index value is less than the stored variables, stores the current value of the index smaller index.
code show as below:
After the completion of the inner loop, if the start is not stored under index table index j , exchanged min corresponding to the value stored and j index corresponding to the index value
code show as below:
And then outputs the code as follows:
Finally, the optimized code:
Optimization of the outer layer: the value before the comparison, without participation of a comparison.
code show as below:
Optimization inner layer: a position from the starting position to start the cycle.
code show as below:
Finally, attach the complete code:
These are all my summary
End ------------ ------------ restore content