【问题标题】:How to output number of passes performed in bubble sort till the array is sorted?在对数组进行排序之前,如何输出冒泡排序中执行的遍数?
【发布时间】:2020-03-11 06:12:46
【问题描述】:
package codeabb;
import java.util.*;

public class Bsort {

    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        int arr[] = new int[100];
        int swap = 0;
        int pass = num-1;

        for( int i = 0; i < num; i++) {
            arr[i] = in.nextInt();
        }

        for( int i = 0; i < num-1; i++) {
            pass--;
            for( int j = i+1; j < num; j++) {
                int temp;
                if(arr[i] > arr[j]) {
                    swap++;

                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;

                }


            }



        }



            System.out.print(pass + " "+swap);

    }

}

我正在尝试输出对数组进行排序时进行的交换和传递的数量。传球数是我猜的array-1的总数?因此,我已经初始化并在每次执行交换时扣除,因为一旦进行交换,就会进行下一次传递。但是输出不对。有人可以帮我吗?

【问题讨论】:

  • 你说的“传球次数”是什么意思?在此处提供您想要的示例。
  • @ankitjain 冒泡排序中的通行证
  • 好的,那么通过次数将仅为 n-1,除非排序在前几次通过中完成。

标签: java swap bubble-sort


【解决方案1】:

您应该在每次外循环迭代时增加它,而不是减少通过计数,并且当内循环中没有交换时,您可以中断外循环。 这样您将获得适当数量的通行证。

int pass=0;
for (int i = 0; i < num - 1; i++) {
            boolean swapsMade = false;
            pass++;
            for (int j = i + 1; j < num; j++) {
                int temp;
                if (arr[j] > arr[j+1]) {
                    swap++;
                    swapsMade = true;
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
            if (!swapsMade) {
                break;
            }
        }

【讨论】:

  • @MenlamChoden 已更新答案,对您的代码进行了一些更改。这应该为您完成这项工作。
  • Ankit,还是一样
  • 对于哪个输入?
  • 3 4 1 7 12 13 19 11 2 14 16 15 5 8 6 18 10 9 17 这个输出应该是 pass 10, swap 60 但是你的代码是 pass 18 and swap 60跨度>
  • 刚刚看了你的排序算法,不正确,马上更新代码
【解决方案2】:

也许将这些步骤可视化有助于理解passswap 的数字。 根据您提供的 sn-p,在下面找到一个示例。

import java.util.Arrays;

public class Bsort {
    public static void main(String args[]) {
        int arr[] = {5, 4, 3, 2 ,1};
        int swap = 0;
        int pass = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            pass++;
            for (int j = i + 1; j < arr.length; j++) {
                int temp;
                if (arr[i] > arr[j]) {
                    swap++;
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                    System.out.printf("pass: %02d   swap: %02d  array: %s%n",  pass, swap, Arrays.toString(arr));
                }
            }
        }
    }
}

输出

通过:01 交换:01 数组:[4, 5, 3, 2, 1]
通过:01 交换:02 数组:[3, 5, 4, 2, 1]
通过:01 交换:03 数组:[2, 5, 4, 3, 1]
通过:01 交换:04 数组:[1, 5, 4, 3, 2]

通过:02 交换:05 数组:[1, 4, 5, 3, 2]
通过:02 交换:06 数组:[1, 3, 5, 4, 2]
通过:02 交换:07 数组:[1, 2, 5, 4, 3]

通过:03 交换:08 数组:[1, 2, 4, 5, 3]
通过:03 交换:09 数组:[1, 2, 3, 5, 4]

通过:04 交换:10 数组:[1, 2, 3, 4, 5]

【讨论】:

  • 3 4 1 7 12 13 19 11 2 14 16 15 5 8 6 18 10 9 17 这个输出应该是 pass 10, swap 60 但是你的代码是 pass 18 and swap 60跨度>
猜你喜欢
  • 1970-01-01
  • 2020-05-17
  • 1970-01-01
  • 2019-01-01
  • 2014-05-17
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
相关资源
最近更新 更多