我在此选择排序中的输出有问题。
这是代码:
public class SelectionSort{
public static void main(String args[]){
int [] arr_sort = {1, 7, 2, 18, 23, 13};
System.out.println("Selection Sort");
System.out.print("Before sorting: ");
int x;
for(x=0; x<arr_sort.length; x++){
System.out.print(arr_sort[x] + " ");
}
System.out.println("");
System.out.print("After sorting: ");
int n = arr_sort.length;
int i,j, min, temp;
for(i=0; i<n; i++){
min=1;
for(j=i+1; j<n; j++){
if (arr_sort[j]<arr_sort[min]){
min=j;
temp=arr_sort[i];
arr_sort[i]=arr_sort[min];
arr_sort[min]=temp;
}
}
System.out.print(arr_sort[i] + " ");
}
}
}
输出:
Selection Sort
Before sorting: 1 7 2 18 23 13
After sorting: 2 1 7 18 23 13