简体   繁体   中英

How to output number of passes performed in bubble sort till the array is sorted?

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);

    }

}

I am trying to output the number of swaps and passes made when an array is sorted. Number of passes is total number of array-1 I guess? Therefore I have initialised that and deducted everytime a swap is performed because once a swap is made, next pass is made. But the output is not right. Can someone help me?

Instead of decreasing the pass count, you should increase it for every outer loop iteration, and you can break the outer loop when there is no swap in the inner loop. This way you will get appropriate number of passes.

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;
            }
        }

Maybe to visualize the steps helps to understand the numbers of pass and swap . Find below an example, based on your provided snippet.

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));
                }
            }
        }
    }
}

output

pass: 01 swap: 01 array: [ 4, 5 , 3, 2, 1]
pass: 01 swap: 02 array: [ 3 , 5, 4 , 2, 1]
pass: 01 swap: 03 array: [ 2 , 5, 4, 3 , 1]
pass: 01 swap: 04 array: [ 1 , 5, 4, 3, 2 ]

pass: 02 swap: 05 array: [1, 4, 5 , 3, 2]
pass: 02 swap: 06 array: [1, 3 , 5, 4 , 2]
pass: 02 swap: 07 array: [1, 2 , 5, 4, 3 ]

pass: 03 swap: 08 array: [1, 2, 4 , 5 , 3]
pass: 03 swap: 09 array: [1, 2, 3 , 5, 4 ]

pass: 04 swap: 10 array: [1, 2, 3, 4, 5 ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM