您的位置:首页 > 其它

6.5.2直接选择排序

2015-10-09 19:47 197 查看
package shuzu;

public class ShuZu {

public static void main(String[] args)
{
//直接选择排序
int arr[] = {6,7,2,9,3,5,4,1,8};

for(int j =arr.length-1; j > 0; j--)
{
int max = 0;//新定义数组中最大数的索引变量为max

for(int i = 1;i <= j; i++)//数组中的数逐个与之值比较
{

if(arr[max] < arr[i])//若小则将索引值付给max
{
max = i;

}

}

int temp = arr[j];    //将本趟查找中的最大数放到最后
arr[j] = arr [max];
arr[max] = temp;

}
//遍历输出排序好的数组
for(int x : arr)
{
System.out.println(x);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: