/**
* 在要排序的一组数中,选出最小的一个数与第一个位置的数交换;
* 然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。
* @author Administrator
*
*/
public class SimpleSelectSort {
public int[] simpleSelectSort(int[] sortArray)
{
int position = 0;
for(int i=0;i<sortArray.length;i++)
{
//保存当前情况下数据最小的值的位置
position = i;
int temp = sortArray[i];
int j = i+1;
//开始进行交换
for(;j<sortArray.length;j++)
{
if(sortArray[j]<temp){
position = j;
temp = sortArray[j];
}
}
//用最小的数据与当前的第一个位置进行交换
sortArray[position]=sortArray[i];
sortArray[i]=temp;
}
return sortArray;
}
public static void main(String[] args) {
int[] a = {10,3,4,5,6,7,1,2,9,8,8};
int[] result = new SimpleSelectSort().simpleSelectSort(a);
for(int i=0;i<result.length;i++)
{
System.out.println(result[i]);
}
}
}
简单选择排序(java实现)
最新推荐文章于 2022-08-19 18:40:15 发布