Home >Java >javaTutorial >java data structure sorting algorithm (4) selection sort
This article mainly introduces the relevant information of Java data structure and algorithm selection sorting. This article explains it through the code. It is very good and has reference value. Friends who need it can refer to it
Each pass selects the smallest (or largest) element from the data elements to be sorted, and places it at the end of the sorted sequence until all the data elements to be sorted are arranged.
Code
public class ChoseSort { //constructor without parameters public ChoseSort(){}; //constructor with parameters public int[] ChoseSort(int[] intArr){ for(int i=0;i<intArr.length-1;i++){ int lowIndex = i; for(int j=i+1;j<intArr.length;j++){ if(intArr[j]<intArr[lowIndex]){ lowIndex = j; } } //将当前第一个元素与它后面序列中的最小的一个 元素交换,也就是将最小的元素放在最前端 int temp = intArr[i]; intArr[i] = intArr[lowIndex]; intArr[lowIndex] = temp; } return intArr; } public static void main(String[] args) { ChoseSort 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+" "); } } }
[Related recommendations]
##1.Java data structure sorting algorithm (1) Tree selection sort
2.java data structure sorting algorithm (2) merge sort
3.java data structure sorting algorithm (3) simple selection sort
4.Detailed tutorial on selection sorting (Selection Sort_java) in Java
The above is the detailed content of java data structure sorting algorithm (4) selection sort. For more information, please follow other related articles on the PHP Chinese website!