有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

问题气泡排序(递归)Java中的整数数组

正在尝试实现递归方法对整数数组进行排序:

    public static void bubbleSort(int[] arr, int n){

      int index = n-1;

      System.out.println("Round number: " + n);
      System.out.println(Arrays.toString(arr));

      while(index>=1)
      {
          if(arr[index] <  arr[index-1])
              swap(arr, index, index-1);


          index--;
      }
      if(n>1)
          bubbleSort(arr, n-1);
  }

}

在最初的几轮中,它似乎工作得很好,将集合中最小的整数移动到前面,但它只是在中途停止工作。知道为什么吗?谢谢


共 (2) 个答案

  1. # 1 楼答案

    试试这个:

    import java.util.*;
    
    public class Test{
        public static void main(String[] args){
            int[] ttt = {9,8,7,6,5,4,3,2,1};
    
            bubbleSort(ttt, ttt.length);
        }
    
        public static void bubbleSort(int[] arr, int n){
            int index = 0;
    
            System.out.println("Round number: " + n);
            System.out.println(Arrays.toString(arr));
    
            while(index < n - 1){
                if(arr[index] >  arr[index + 1]){
                    swap(arr, index, index + 1);
                }
                index++;
            }
    
            if(n > 1){
                bubbleSort(arr, n-1);
            }
        }
    
        public static void swap(int[] arr, int i, int j){
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
    }
    

    在你的情况下,在每一轮中,你保证在每一轮结束时,你将有最小的数被推到它的位置,但然后省略数组的最后一个元素,它不一定是最大的数。你应该按相反的顺序做,把最大的数字推到它的位置,然后在下一轮中把它排除在外

    http://en.wikipedia.org/wiki/File:Bubble-sort-example-300px.gif

    仅供参考:冒泡排序最糟糕的情况是O(n^2),也许你想实现更好的排序算法,比如快速排序或合并排序

  2. # 2 楼答案

    if条件更改为:

    ...
    if(arr[index] <  arr[index-1]){
       swap(arr, index, index-1);
       index = n;
    }
    index ;
    ...
    

    问题是,在找到一个数组成员在整个数组中“委派”之后,您需要“重新启动”,因为可能还有其他成员需要“重新考虑”。 使用调试器运行,如果我的描述不够清晰,请查看我的意思

    完整解决方案:

    import java.util.Arrays;
    
    /**
     * User: alfasin
     * Date: 8/5/13
     */
    public class BubbleSort {
    
        public static void bubbleSort(int[] arr, int n){
    
            int index = n-1;
    
            System.out.println("Round number: " + n);
            System.out.println(Arrays.toString(arr));
    
            while(index>=1)
            {
                if(arr[index] <  arr[index-1]){
                    swap(arr, index, index-1);
                    index = n;
                }
                index ;
            }
            if(n>1)
                bubbleSort(arr, n-1);
        }
    
        private static void swap(int[] arr, int index, int i) {
            arr[i] = arr[i] ^ arr[index];
            arr[index] = arr[i] ^ arr[index];
            arr[i] = arr[i] ^ arr[index];
        }
    
        public static void main(String...args){
            int[] arr = {4,2,9,6,2,8,1};
            bubbleSort(arr, arr.length);
            for(int i=0; i<arr.length; i++){
                System.out.print(arr[i]+" ");
            }
        }
    
    }
    

    就像sjee397建议的那样——这更像是气泡排序的“版本”

    气泡排序的一个更“保守”的版本:

    public static void bubbleSort(int[] arr, int n){
            boolean swapped= true;
            while (swapped){
                swapped = false;
                for(int i=0; i<arr.length-1; i++){
                    if(arr[i]>arr[i+1]){
                        swap(arr,i,i+1);
                        swapped = true;
                    }
                }
            }
        }