Bubble sort and selection sort in Java

Bubble sort:
adjacent elements pairwise comparison, the next big release, after the first is completed, the maximum appeared in the largest index,
empathy, continue, you can get a sorted array.

Law: 1 pairwise comparison, the next big release.
2. After the first comparison, the next comparison, when the comparison is to reduce one element
3. The first comparison, there are elements than 0.
Second comparison, there is an element of better than.
Third comparison, there are two elements better than.
4. The total of the -1-order comparator array length.

/ * Code implementation * /
public static void bubbleSort (int [] ARR) {

int [] ARR = {24, 51 is, 35, 54 is, 25, 64, 72, 52 is};
for (int X = 0; X <ARR . 1-.length; X ++) {
for (int Y = 0; Y <arr.length -. 1 - X; Y ++) {
IF (ARR [Y]> ARR [Y +. 1]) {
int ARR TEMP = [Y] ;
ARR [Y] = ARR [Y +. 1];
ARR [Y +. 1] = TEMP;
}
}
}

for (int I = 0; I <arr.length; I ++) {
of System.out.print (ARR [I ] + ""); // 2,425,355,152,546,472
}
}
select a sort:
starting from index 0, and after the element a comparison, small forward side, after completion of the first, the minimum in
The smallest index. Other empathy which can get a sorted array.

Law:
1: The first starts from index 0 and the other compares
the second from the start index 1 compared with other
....
2: the length of the array is the last element of array length -2 -1 compare elements.

/ * Code implementation * /
public static void bubbleSort (int [] ARR) {

int [] ARR = {24, 51 is, 35, 54 is, 25, 64, 72, 52 is};

for (int X = 0; X <ARR . 1-.length; X ++) {
for (int X = Y +. 1; Y <arr.length; Y ++) {
IF (ARR [Y] <ARR [X]) {
int ARR TEMP = [X];
ARR [X ] = ARR [Y];
ARR [Y] = TEMP;
}
}
}
for (int I = 0; I <arr.length; I ++) {
of System.out.print (ARR [I] + ""); // 2,425,355,152,546,472
}

}

Guess you like

Origin www.cnblogs.com/lszbk/p/12318606.html
Recommended