代码显示:
/**
实现选择排序功能
*/
class SelectSort
{
public static void main(String[] args)
{
int[] arr = new int[]{11,58,56,43,92,45,91,65,75,98,35}; //定义一组数组
System.out.println("排序前:");
printArr(arr);
selectSort(arr);
System.out.println("排序后:");
printArr(arr);
}
/*选择排序*/
private static void selectSort(int[] arr)
{
for (int i = 0;i<arr.length ;i++ )
{
for (int j = i+1;j<arr.length ;j++ )
{
swap(arr,i,j);
}
}
}
/*实现交换功能*/
private static void swap(int[] arr,int index1,int index2)
{
int temp = 0;
if (arr[index1]>arr[index2])
{
temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
return ;
}
/*打印数组*/
private static void printArr(int[] arr)
{
System.out.print("[");
for (int i = 0;i<arr.length ;i++ )
{
if (i == arr.length-1)
{
System.out.print(arr[i]+"");
}
else
{
System.out.print(arr[i]+",");
}
}
System.out.println("]");
}
}
结果显示: