Java sorting: Direct selection and sorting; java sorting
Public class SelectSort {public static void selectSort (int [] a) {int min; int temp; if (a = null |. length <= 0) {return;} for (int I = 0; I <. length; I ++) {// number of times the External Loop is compared. Each time a minimum value is found and min = I is exchanged; // define the current subscript as the minimum subscript for (int j = I + 1; j <. length; j ++) {// compare the current data in the loop control to find the minimum value if (a [j] <a [min]) {// if there is a data value smaller than the minimum value, the subscript of the data is assigned to min. In fact, min is a flag min = j ;}} if (min! = I) {// If the min value in this trip has changed, it means that the data in this trip is smaller than the min value we defined. Therefore, we need to exchange the data with our defined a [min] temp. = a [min]; a [min] = a [I]; a [I] = temp ;}}}}
In Java, what is the difference between Bubble sorting and selection sorting?
The basic concept of BubbleSort is to compare two adjacent numbers in sequence, put decimal places in front, and put large numbers in the back. That is, in the first step: first compare the number of 1st and the number of 2nd, and put the decimal number before and after the large number. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. So far, the first stop is over, and the maximum number is placed at the end. In the second round: The comparison starts from the first logarithm (because the number of 2nd is exchanged with the number of 3rd, the number of 1st is no less than 2nd). Before and after placing the decimal number, always compare to the second to the last (the largest position in the last), and the second stop, get a new maximum number at the penultimate position (in fact, it is the second largest number in the entire series ). In this case, repeat the above process until the sorting is completed.
Public class Paixu {
Public static void main (String [] args ){
Int [] a = {, 3 };
Int I = 0;
Int j = 0;
Int n = 0;
For (I = 0; I <a. length-1; I ++ ){
For (j = 0; j <a. length-i-1; j ++ ){
If (a [j]> a [j + 1]) {
N = a [j];
A [j] = a [j + 1];
A [j + 1] = n;
}
}
}
For (I = 0; I <a. length; I ++ ){
System. out. println (a [I]);
}
}
}
Straight Select Sorting is also a simple Sorting method. Its basic idea is: the first time from R [0] ~ Select the minimum value in R [n-1] and exchange it with R [0]. The second time is from R {1 }~ Select the minimum value in R [n-1], exchange with R [1],..., I from R [I-1] ~ Select the minimum value in R [n-1], exchange with R [I-1],... n-1 from R [N-2] ~ In R [n-1], the minimum value is selected and exchanged with R [N-2]. A sequence sorted by sort code from small to large is obtained through n-1 times in total.
Public class Paixu {
Public static void main (String [] args ){
Int [] a = {, 3 };
Int I = 0;
Int j = 0;
Int n = 0;
For (I = 0; I <a. length; I ++ ){
For (j = I + 1; j <a. length; j ++ ){
If (a [I]> a [j]) {
N = a [I];
A [j] = a [I];
A [I] = n;
}
}
}
For (I = 0; I <a. length; I ++ ){
System. out. println (a [I]);
}
}
}
Sorting in java
The second for loop has less than one equal sign
J <= arry. length-I