Java array sorting, java Array
I. The operation of the Bubble Sorting Algorithm is as follows:
Compares adjacent elements. If the first is bigger than the second, exchange the two of them.
Perform the same operation on each adjacent element, from the first to the last. At this point, the final element should be the largest number.
Repeat the preceding steps for all elements except the last one.
Continue to repeat the above steps for fewer and fewer elements until no one pair of numbers needs to be compared.
Ii. Sorting:
The selected sorting is evolved from the Bubble sorting. The smallest value is obtained in each round of comparison, and then exchanged with the first value involved in the comparison in each round of "unordered area.
Code:
1 package com. study. sort; 2 3 import java. util. arrays; 4 import java. util. collections; 5 import java. util. random; 6/** 7 * @ ClassName: MaoPao 8 * @ date October 10, 2017 5:15:21 9 */10 public class MaoPao {11 12 public static void main (String [] args) {13 int [] arr = new int [20]; 14 Random rd = new Random (); 15 for (int I = 0; I <20; I ++) {16 int a = rd. nextInt (101); // random integer 17 arr [I] = a; 18 // System. out. p Rintln (a); 19} 20 // [19, 84, 47, 69, 28, 20, 79, 53, 17, 67, 96, 39, 49, 66, 1, 8, 92, 27, 63, 94] 21 System. out. println (Arrays. toString (arr); 22 // mao (arr); 23 xuan (arr); 24 System. out. println (Arrays. toString (arr); 25} 26 27 // Bubble Sorting 28 public static int [] mao (int [] arr) {29 for (int I = 0; I <arr. length-1; I ++) {30 for (int j = 0; j <arr. length-1-i; j ++) {31 if (arr [j]> arr [j + 1]) {32 33 int temp = arr [j]; 34 arr [j] = Arr [j + 1]; 35 arr [j + 1] = temp; 36} 37} 38} 39 return arr; 40} 41 42 // select sort 43 public static int [] xuan (int [] arr) {44 for (int I = 0; I <arr. length; I ++) {45 int min = I; 46 for (int j = I + 1; j <arr. length; j ++) {47 if (arr [min]> arr [j]) {48 min = j; 49} 50} 51 if (min! = I) {52 int temp = arr [min]; 53 arr [min] = arr [I]; 54 arr [I] = temp; 55} 56} 57 return arr; 58} 59}
Graphic analysis: