版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a15028596338/article/details/82592907
直接选择排序方法属于选择排序的一种,它的排序速度要比冒泡排序快一些。
直接排序的基本思想是将指定排序位置与其它数组元素分别对比,如果满足条件就交换元素值。
public class SelectSort {
public static void main(String[] args) {
int arr[]=new int[] {23,4,54,18,42};
SelectSort selectSort=new SelectSort();
selectSort.sort(arr);
}
public void sort(int []array) {
int index;
for (int i = 1; i < array.length; i++) {
index=0;
for (int j = 0; j < array.length-i; j++) {
if(array[j]>array[index]) {
index=j;
}
}
int temp=array[array.length-i];
array[array.length-i]=array[index];
array[index]=temp;
}
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}