[Java SE] How to Use Java to achieve direct selection and sorting, and sejava to achieve sorting
Abstract: Direct selection of sorting is a kind of sorting, but its sorting algorithm is faster than Bubble sorting, because its algorithm is relatively simple, therefore, it is also suitable for beginners to learn and master.
1 package liuenci.cn. package_3; 2 3 public class SelectSort {4 5/** 6 * directly select sort 7 */8 public static void main (String [] args) {9 // TODO Auto-generated method stub10 // create an unordered array 11 int [] array = }; 12 // create a directly ordered object 13 SelectSort sorter = new SelectSort (); 14 // call the directly selected algorithm to sort the array by 15 sorter. sort (array); 16} 17 public void sort (int [] array) {18 int index; 19 for (int I = 1; I <array. length; I ++) {20 index = 0; 21 for (int j = 1; j <= array. length-I; j ++) {22 if (array [j]> array [index]) {23 index = j; 24} 25 // swap in location array. two numbers 26 int temp = array [array. length-I]; 27 array [array. length-I] = array [index]; 28 array [index] = temp; 29} 30 31} 32 showArray (array ); 33} 34 public void showArray (int [] array) {35 // TODO Auto-generated method stub36 for (int I = 0; I <array. length; I ++) {37 int num = array [I]; 38 System. out. print (num + ""); 39} 40 System. out. println (); 41} 42 43}