/** * 功能是实现选择排序 **/package pro2;public class work3 { public static void main(String[] args) { // TODO Auto-generated method stub int arr[]={1,2,5,7,0,6,8}; choose Choose = new choose(); Choose.sort(arr); }}class choose { public void sort(int arr[]) { int temp = 0; for (int j = 0; j < arr.length - 1; j++) { // 标记最小数组和元素 int minnumber = arr[j]; int min = j; for (int k = j + 1; k < arr.length; k++) { if (minnumber > arr[k]) { // 赋给他们最小值 minnumber = arr[k]; min = k; } // 交换值 temp = arr[j]; arr[j] = arr[min]; arr[min] = temp; } } for (int i = 0; i < arr.length; i++) { System.out.print(arr + " "); } }}
1 回答
已采纳
qq_青枣工作室_0
TA贡献446条经验 获得超754个赞
System.out.print(arr + " ");
改为
System.out.print(arr[i] + " ");
另外,交换值,最好放到外层,提高效率。如下:
class choose { public void sort(int arr[]) { for (int j = 0; j < arr.length - 1; j++) { // 标记最小数组和元素 int minnumber = arr[j]; int min = j; for (int k = j + 1; k < arr.length; k++) { if (minnumber > arr[k]) { // 赋给他们最小值 minnumber = arr[k]; min = k; } } if(min!=j) { // 交换值 arr[min] = arr[j]; arr[j] = minnumber; } } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
添加回答
举报
0/150
提交
取消