Java entry-level sorting algorithm and java entry-level Algorithm
To understand insertion sorting, you must first understand and implement an algorithm --> for an array a [] that has sorted orders from large to small, insert a value so that the order of the entire array is still from large to small;
The implementation of this algorithm is very simple and will not be discussed.
For the first round of the out-of-order array a []; In the outer I loop, I assume that the array has only one element a [0], and a [1] is used as the value for insertion, at the end of the first loop, an ordered array is obtained,
The second loop uses the array with two elements a [0], a [1], a [2] As the value to insert, and so on to sort the entire array.
Import java. lang. reflect. Array;
Import java. util. Arrays;
Public class TestArray {
Public static void main (String [] args ){
Int a [] = };
TestArray ();
System. out. println (Arrays. toString ());
}
Static void testArray (int [] ){
For (int I = 1; I <a. length; I ++ ){
Int value = a [I], j;
For (j = I-1; j> = 0 & value <a [j]; j --){
// Leave location a [j] blank and wait for value to be inserted
A [j + 1] = a [j];
}
// Unoptimized j-Loop
// For (j = I-1; j> = 0; j --){
// If (value <a [j]) {
// A [j + 1] = a [j];
//} Else {
// Break;
//}
// After the j loop, run j --
// Therefore, a [j + 1] = value;
A [j + 1] = value;
}
}
}