A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘欢 中级黑马   /  2012-8-9 12:31  /  1845 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class ArrayTest2
  2. {

  3. public static void selectSort(int[] arr)
  4. {
  5. for (int x=0;x<arr.length-1;x++ )
  6. {
  7. for (int y=x+1;y<arr.length ;y++)
  8. {
  9. if (arr[x]>arr[y]);
  10. {
  11. int temp = arr[x];
  12. arr[x] = arr[y];
  13. arr[y] = temp;
  14. }
  15. }
  16. }
  17. }
  18. /*     
  19.    此处是正确的排序代码
  20. public static void selectSort(int[] arr) //选择排序
  21. {
  22. for (int x=0;x<arr.length-1 ;x++ )
  23. {
  24. for (int y=x+1;y<arr.length ;y++ )//x+1 1角标和2角标比较
  25. {
  26. if(arr[x]>arr[y])
  27. {
  28. int temp = arr[x];
  29. arr[x] = arr[y];
  30. arr[y] = temp;
  31. }
  32. }
  33. }
  34. }
  35. */
  36. public static void main(String[] args)
  37. {
  38. int[] arr = {5,1,6,4,2,8,9};
  39. //int selectSort = selectSort(arr);
  40. //System.out.println("selectSort="+selectSort);
  41. selectSort(arr);
  42. printArray(arr);
  43. }
  44. public static void printArray(int[] arr)
  45. {
  46. System.out.print("[");
  47. for (int x=0;x<arr.length ;x++ )
  48. {
  49. if (x!=arr.length-1)
  50. {
  51. System.out.print(arr[x]+",");
  52. }
  53. else
  54. System.out.print(arr[x]+"]");
  55. }
  56. }
  57. }
复制代码
复习的时候自己动手写了一次选择排序,但是编译出来的结果:[9,8,2,4,6,1,5]  是把arr的倒着打印了一次,后来查看视频照着把代码写了下来,对照着没发现有哪不对,而且试过新建java程序,把出错的代码拷贝,编译结果还是倒着打印。我怀疑是不是这几天感冒影响的 哈哈。备注里边是正确的排序代码,请哪位有时间的前辈指导一下。

8 个回复

倒序浏览
mei wen ti  a
回复 使用道具 举报
冯超 发表于 2012-8-9 12:38
mei wen ti  a

有木有试着运行一下
回复 使用道具 举报
本帖最后由 张君 于 2012-8-9 12:52 编辑

哥们。你的第十行代码 。看到后面那个分号了么 10.if (arr[x]>arr[y]);
岂不是相当于结束了么,按你的写法,就相当于,当 x=0 时 循环  (int y=x+1;y<arr.length ;y++ )  28.{

29.int temp = arr[x];

30.arr[x] = arr[y];

31.arr[y] = temp;

32.}

相当于把arr[]的最后一个数跟放到第一个数的位置了
相当于把这个数组右移一次
对吧

回复 使用道具 举报
本帖最后由 尤洋 于 2012-8-9 12:59 编辑

你把错误代码 和正确代码比较一遍,就会发现少了个“;”
回复 使用道具 举报
{:soso_e136:}    OMG  看到了,太丢人了,标点错了。浪费你们时间了,谢谢了。以后不会再问这种白痴问题了
回复 使用道具 举报
发现自己没有逻辑思维,现在想想根据结果也能推出问题在哪,哎  接着学了
回复 使用道具 举报
public class Test1 {
        public static void main(String[] args)
        {
                int[] arr = {5,1,6,4,2,8,9};
                //int selectSort = selectSort(arr);
                //System.out.println("selectSort="+selectSort);
                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++ )//x+1 1角标和2角标比较
                        {
                                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.print(arr[x]+"]");
                        }
                }
                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;
                                                                }
                                                }
                                }
                }
        }
        希望下次把代码对齐一下 这样方便看
回复 使用道具 举报
杨彪 发表于 2012-8-9 20:24
public class Test1 {
        public static void main(String[] args)
        {

确实乱,以后会规范的   谢谢提建议
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马