问题1:20.arry[y] = temp;//这下面为啥不需要返回值呢? 答:需不需要返回值是由方法决定的,当方法是void类型就不需要返回值
问题2:01.class SelectSort
02.{
03.public static void main(String[] args)
04.{
05.int[] arry = {5,9,3,1,7,9,4,6};
06.printArry(arry);
07.selectSort(arry);
08.printArry(arry);
09.}
10.public static void selectSort(int[] arry)
11.{
12.for(int x=0;x<arry.length-1;x++)
13.{
14.for(int y=1;y<arry.length;y++)//这里应该是for(int y = x+1; y<arry.length; y++)
,对于排列的问题,自己把数组是图画出来容易理解
15.{
16.if(arry[x]>arry[y])
17.{
18.int temp = arry[x];
19.arry[y] = arry[x];//这里应该是arry[x] = arry[y];
20.arry[y] = temp;//这下面为啥不需要返回值呢?
21.}
22.}
23.}
24.}
25.public static void printArry(int[] arry)
26.{
27.System.out.print("[");
28.for(int x=0;x<arry.length;x++)
29.{
30.if(x!=arry.length-1)
31.System.out.print(arry[x]+",");
32.else
33.System.out.println(arry[x]+"]");
34.
35.}
36.
37.}
38.}
|