Java basics-Bubble sorting, java basics bubble
// Bubble sort, and compare public static void sort1 (int [] aa) {int size = aa from the back of the array. length; int temp; // cyclic array for (int I = 0; I <size; I ++) {// aa [I] compares and exchanges all numbers after the I bit respectively. aa [I] becomes the minimum value for (int j = I + 1; j <size; j ++) {if (aa [I]> aa [j]) {temp = aa [I]; aa [I] = aa [j]; aa [j] = temp ;}}for (int I = 0; I <aa. length; I ++) {System. out. println (aa [I]) ;}}
Sort idea: the first layer for loop, each value in the loop array, the second layer for loop, compare the size of aa [I] In the first for loop with other numbers starting from I + 1. If it is smaller than aa [I, switch aa [I] and aa [j]. Through the second-layer for loop, aa [I] will be the minimum value of all numbers starting from I + 1. And so on, you can obtain the minimum values of the remaining digits that are larger than the remaining digits by swapping them. In this way, we can sort the array from small to large.
// Bubble sort. Compare the public static void sort2 (int [] aa) {int size = aa from the forward loop after the array. length; int temp; // cyclic array for (int I = 0; I <size; I ++) {// aa [I] compares and exchanges all numbers after the I bit respectively. aa [I] becomes the minimum value for (int j = size-1; j> I; j --) {if (aa [I]> aa [j]) {temp = aa [I]; aa [I] = aa [j]; aa [j] = temp ;}}for (int I = 0; I <aa. length; I ++) {System. out. println (aa [I]) ;}}