选择排序算法
给大家写一个选择排序的代码
java中的排序算法我总结了一下找到以下几种
冒泡排序、选择排序、插入排序
希尔排序、快速排序、归并排序
下边的是一个选择排序之后的算法我会慢慢更新
public class Demo1 {
public static void main(String[] args) {
int[] array = {2, 4, 5, 6, 7, 8, 3, 10};
selectSort(array);
System.out.println(Arrays.toString(array));
}
/*
方法分析:
public static
返回值类型 :
boolean
方法名:
test
形式参数列表:
int[] array
方法声明:
public static boolean test(int[] array)
*/
/**
* 排序算法
*
* @param array int类型数组
* @return 排序成功返回true失败返回false
*/
public static boolean selectSort(int[] array) {
// 参数合法性判断
if (null == array || array.length == 0) {
System.out.println("Input Parameter is Invalid!");
return false;
}
for (int i = 0; i < array.length - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < array.length; j++) {
//可以选择修改符号让排序从大到小或者从小到大
if (array[maxIndex] > array[j]) {
maxIndex = j;
}
}
if (maxIndex != i) {
int temp = array[maxIndex];
array[maxIndex] = array[i];
array[i] = temp;
}
}
return true;
}
}