Insert Sort
The idea of inserting a sort is to traverse the entire array starting from the second element of the array. For each element, the elements that precede it are compared to him, the elements that are greater than it are moved backwards, and the element is finally inserted.
The insertion sort is a stable sorting algorithm.
Time Complexity T (n) =o (n^2)
The best case has been ordered, T (n) =o (n)
private void Swap (int[] A, int i, int j) {int temp = A[j];a[j] = a[i];a[i] = temp;}
insert a sort of key code
private void Insertsort (int[] a) {for (int i = 1; i < a.length; i++) {int J = i-1;int tmp = a[i];while (J >= 0 &am p;& A[j] > tmp) {//stable sort, note order a[j + 1] = a[j];j--;} A[J+1] = tmp;}}
Bubble Sort
Bubble sort idea is: from the first element to start the next two elements of the 22 comparison, according to the decimal in the large number under the rule exchange, a trip down the large number of sink.
Then start with the first element 22 compare adjacent two elements, until the second element of the penultimate
....
Bubble sort is also a stable sorting algorithm
The complexity of bubbling sorting is all T (n) =o (n^2)
Insert a sort of key code
public void Bubblesorts (int[] a) {/*boolean flag = true;*/for (int i = 0; i < a.length-1/*&& flag*/; i++) {/ *flag = false;*/for (int j = 0; J < a.length-1-I; j + +) {if (A[j] >= a[j + 1]) {Swap (A, J, J + 1);/*flag = true;* /}}}}
The comment is an improvement to the bubbling sort, when no call to the SWAP function indicates that the order has been queued. This can be achieved in the best case time complexity of T (n) =o (n)Select Sort Find the smallest one from all the sequences, then place it in the first position, then look at the smallest of the remaining elements and place it in the second position ... And so on
Choosing a sort is an unstable sorting algorithm, such as [3,3,1] the first three will run to the second 3 after the first exchange.
The complexity of selecting sorting is all T (n) =o (n^2)
Select the key code for sorting
public void Selectsort (Int. a[]) {for (int i = 0; i < a.length-1; i++) {//Find the smallest number starting from I int minindex = i;for (int j = i + 1 ; J < A.length; J + +) {if (A[j] < A[minindex]) {minindex = j;}} Swap (A, I, Minindex);}}
stability means that the relative order of the elements with the same merits in the output array remains the same as the input array. Stability is an important index of the sorting algorithm, and the order of radix is used as the sub-process of the stable sorting algorithm.
Perhaps this explanation of the concept is not very clear. For example, we are not just comparing numbers in real-world applications, but perhaps comparing an object with many attributes. For example, the results of two students include language and mathematics, a[80,90],b[80,85], in the ranking of the language to ensure that a before B, because of the language scores A and B is the same, but an object also contains mathematical results, can not be ranked in the language scores of the time to disrupt the order of mathematical results.
"Algorithm" Insert sort/bubble sort/Select sort