选择排序

来源:互联网 发布:mac 符号 编辑:程序博客网 时间:2024/05/17 05:11
class Sort
{
public static void main(String[] args) 
{
int[] arr={5,1,6,4,2,8,9};
printArray(arr);//先按原顺序输出一次
selectSort(arr);//调用函数
printArray(arr);//排序后在输出一次
}
      public static void selectSort(int[] arr)//定义排序函数
{
for (int x=0;x<arr.length-1 ;x++ )
{
for (int y=x+1;y<arr.length ;y++ )
{
if (arr[x]>arr[y])
{
int temp=arr[x];
arr[x]=arr[y];
arr[y]=temp;
}
}
}
}
public static void printArray(int[] arr)//定义输出函数
{
System.out.print("[");
for (int x=0;x<arr.length ; x++)
{
if(x!=arr.length-1)
System.out.print(arr[x]+", ");
else
System.out.println(arr[x]+"]");
}
}
}
0 0
原创粉丝点击