Java 我如何重复排序直到在冒泡排序中没有完成交换?

Java How do i repeat sort until no swaps are done in bubblesort?

我取了 10 个元素并对它们进行冒泡排序。我想添加一个重复排序的算法,直到不需要交换来提高效率。 基本上我想:

这是我到目前为止所做的:

    {

        //create array
        int[] iList = new int[10];
        Scanner sc = new Scanner(System.in);

        //takes in array input for 10 numbers
        
        System.out.println("Enter a array of numbers ");

        
        for(int i = 0; i< 10; i++ ) 
        {
            int num = i + 1;
            System.out.println("Enter number " + num);
            iList[i] = sc.nextInt();
            
        }

        //Bubble sorts the array
        
        System.out.println("The array =");

        for(int a = 0; a < iList.length; a++ )
        {
          for(int b = a+1; b < iList.length; b++) 
          {
                if(iList[a] > iList[b])
                {
                    int iTemp = iList[a];
                    iList[a] = iList[b];
                    iList[b] = iTemp;   
                    
                }
                  System.out.println("Progress = " + Arrays.toString(iList) );
           }    
        
        }
        
    } ```

这是我的实现:

public static void sort(int[] nums) {
        boolean isSwapped;
        int size = nums.length - 1;

        for (int i = 0; i < size; i++) {
            isSwapped = false;
            for (int j = 0; j < size - i; j++) {
                if (nums[j] > nums[j+1]) {
                    int temp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = temp;
                    isSwapped = true;
                }
            }
            if (!isSwapped) break;
        }

        System.out.println("Sorted Array: " + Arrays.toString(nums));
    }