Selecting sorting is also a simple sorting method, and its principle is also easy to understand. It is the same as the number of comparisons for Bubble sorting, but the number of exchanges for sorting is less than that for Bubble sorting. Bubble Sorting is after each comparison. If the order of the two elements to be compared is the opposite of the order to be sorted, the two elements to be compared are exchanged, however, when sorting is selected, only the subscript of the smallest element is recorded in each traversal process. After all the comparisons are completed, the minimum element is exchanged with the first element of the unordered sequence, this reduces the number of exchanges and improves the sorting efficiency.
The Java implementation code is as follows:
Public class chosesort {// constructor without parameterspublic chosesort () {}; // constructor with parameterspublic int [] chosesort (INT [] intarr) {for (INT I = 0; I <intarr. length; I ++) {int lowindex = I; for (Int J = I + 1; j <intarr. length; j ++) {If (intarr [J] <intarr [lowindex]) {lowindex = J ;}} // exchange the current first element with the smallest element in the sequence following it, that is, place the smallest element in the front-end int temp = intarr [I]; intarr [I] = intarr [lowindex]; intarr [lowindex] = temp;} return intarr;} public static void main (string [] ARGs) {chosesort = new chosesort (); int [] intarr = {5, 6, 9, 2, 4, 3, 8}; int [] intarraftersort = chosesort. chosesort (intarr); For (INT arritem: intarraftersort) {system. out. print (arritem + "");}}}