First, the bubble sorting algorithm operates as follows:
Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
Repeat the above steps for all elements, except for the last one.
Repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare
Second, choose the sort:
The selection sort evolved from a bubbling sort, with each round comparing the smallest value, then swapping the first value in each round of the "unordered zone" that participates in the comparison.
Specific code:
1 package com.study.sort;2 3 Import java.util.arrays;4 import java.util.collections;5 import java.util.random;6/**7 * @Cl Assname:maopao 8 * @date October 10, 2017 pm 5:15:219 */10 public class Maopao {All public static void main (string[] args) {1 3 int[] arr = new int[20];14 Random rd = new random (), n for (int i = 0;i<20;i++) {int a= rd.nextint (101); Arr[i] = A; System.out.println (a); 19}20//[19, 84, 47, 69, 28, 20, 79, 53, 17, 67, 96, 39, 49, 66, 1, 8, 92, 27, 94]21 System.out.println (arr), arrays.tostring (arr), Xuan (arr), System.out.println (arrays.tostring (arr)); 25}
Third, bubble sort
Specific code:
public static int[] Mao (int[] arr) {$ for (int i = 0;i<arr.length-1;i++) {A for (int j= 0;j<arr.length-1-i;j++) {( Arr[j]>arr[j+1]) {+/-int temp = arr[j];34 Arr[j] = arr[j+1];35 arr[j+1] = temp;}37}38}39 return arr;40}41 42/ /select sort of public static int[] Xuan (int[] arr) {$ for (int i = 0; i<arr.length;i++) {$ int min = i;46 for (int j=i+1;j<ar r.length;j++) {Arr[min]>arr[j] {= min = j;49}50}51 if (min!= i) {# int temp = arr[min];53 Arr[min] = arr[i];54 A Rr[i] = temp;55}56}57 return arr;58}59}
Reference Plot Analysis:
Java Array Sorting