Java Select sort and Bubble sort
1.datasorter.java
public class Datasorter {//Bubble sort method//main idea: Sort by ascending order, array element 22 comparison, large immediately after public static void Bubblesort (int[] a) {for (int i = 1; i < A.length; i++) {//control outer loop count for (int j = 0; J < A.length-i; J + +) {if (A[j] > A[j+1]) {//array element 22 comparison, large immediately trailing swap (A, J, j+1);} }}}//direct selection of the sorting method//main idea: in ascending order, each cycle to find the maximum number, put him in the last position of public static void Selectsort (int[] a) {int index;//Save the subscript for each comparison of the maximum value; for ( int i = 1; i < a.length; i++) {//control outer loop Count index = 0;for (int j = 1; J <= A.length-i; j + +) {if (A[j] > A[index]) {index = j;}} Swap (A, index, a.length-i);}} Swap array elements private static void swap (int[] A, int x, int y) {int tmp = a[x];a[x] = a[y];a[y] = tmp;} Output array element public static void show (int[] a) {for (int i = 0; i < a.length; i++) {System.out.println (a[i]);}}
2. Test class Test.java
public class Test {public static void main (string[] args) {int [] a = {9,2,1,8,0,3};D atasorter.selectsort (a);D atasorter.sh Ow (a);}}
3. Test results:
Java Select sort and Bubble sort