请大家帮助请完善SORT方法,使传入的数组按从小到大的顺序排列
public int[] sort(int[] nums){

解决方案 »

  1.   

    public int[] sort(int[] a) { // a是待排序的文件,采用自下向上扫描,对R做冒泡排序
        int temp = 0;
        int i = 0, j = 0;    for (i = 1; i < a.length; i++) {// 最多做n-1趟排序
         for (j = 0; j < a.length - i; j++) {// 对当前无序区R[i..n]自下向上扫描
          if (a[j] > a[j + 1]) { // 这里的“>”号决定了最后是升序,“<”则是降序
           temp = a[j + 1];// temp仅做暂存单元
           a[j + 1] = a[j];
           a[j] = temp;
          }
         }// 内层结束,第一趟最大的放在了最后,第二趟次大的放在了倒数第二。
        }// endfor(外循环)   return a;// 打印结果
    }
    上面是冒泡排序
    百度下还有 冒泡,选择,插入等排序的。
      

  2.   

    既然有返回值,那就不应该影响原数组了。public int[] sort(int[] nums) {
        if (nums == null)
            return nums;
        int[] tmp = nums.clone();
        java.util.Arrays.sort(tmp);
        return tmp;
    }
      

  3.   

    我再来个插入排序的,希望对楼主有帮助。
    public int[] sort(int[] nums){ 
       int j;
       for(int i = 1; i < nums.length; i++){
         int key = nums[i];
         j = i - 1; 
         while(j > 0 & nums[j] > key){
           nums[j+1] = nums[j]; 
           j--;
         }
       }
      

  4.   

    上边忘了 return 了  楼主自己加上去就可以了。