thought : 22 comparison, once the discovery does not meet the order requirements of the exchange, know that the entire sequence satisfies the ordering requirements.
Typical: bubble sort and quick sort.
Bubble Sort
thought: compare adjacent two, reverse the exchange, each order will be the largest ' sinking ' or the smallest ' float '.
function Bubblesort (arr) {Const LEN = Arr.length;let temp = 0;for (let i=0;i<len-1;i++) {for (let j=0;j<len-i-1;j++) { if (arr[j]>arr[j+1]) {temp = Arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}} return arr;}
Complexity of Time:
Best case: Positive order. It takes only a single trip to sort, make n-1 comparisons, and not move records.
Worst case: reverse. As long as the n-1 sequencing, the comparison is N^2/2, the number of moves 3N^2/2.
So the complexity of Time is O (n^2).
Complexity of space:
The space Complexity is O (1)when a secondary space is required to do the staging record only when two records are exchanged.
Characteristics:
1, is a stable sort.
2, can be used for chain storage structure.
3, the number of mobile records more, the average time performance of the algorithm than the direct insertion sorting difference. This algorithm is not suitable when the initial record is unordered and N is large.
Quick Sort
Poke: here
Sort algorithm-Exchange sort (JavaScript)