将几个数组的元素进行降序输出
1.直接选择排序
/**
* @(#)SelectSort.java
*
*
* @author
* @version 1.00 2013/3/11
*/
public class SelectSort {
public static void main (String[] args) {
//创建一个数组,数组元素乱序
int[] array={63,89,45,56,59,65};
//创建直接排序类的对象
SelectSort sorter=new SelectSort();
//调用排序类对象的方法将数组排序
sorter.sort(array);
}
/**
*
*
* 要排序的数组
*
*/
public void sort(int[] array)
{
int index;
for(int i=1;i<array.length;i++)
{
index=0;
for(int j=1;j<array.length;j++)
{
if(array[j]>array[index])
{
index=j;
}
}
//交换在位置array.length-i和index(最大值)上的两个数
int temp=array[array.length-i];//把第一个元素值保存到临时变量中
array[array.length-i]=array[index];//吧第二个元素值保存到第一个元素单元中
array[index]=temp;//吧临时变量也就是第一个元素原值保存到第二个元素中
}
showArray(array);//输出直接选择排序后的数组值
}
public void showArray(int[] array)
{
for (int i:array)//遍历数组
{
System.out.print(">"+i);//输出每个数组元素的值
}
System.out.println();
}
}