Question: Below is the correctjava code for bubble sorting. public static void bubbleSort(int[] array) { for (int pass = 0; pass < array.length - 1; pass++)

Below is the correctjava code for bubble sorting.

public static void bubbleSort(int[] array) {

for (int pass = 0; pass < array.length - 1; pass++) {

for (int i = 0; i < array.length - pass - 1; i++) {

if (array[i] > array[i + 1]) {

swap(array, i, i + 1);

}

}

}

}

public static void swap(int[] array, int first, int second) {

int temp = array[first];

array[first] = array[second];

array[second] = temp;

}

}

How to implement that to show every step(pass)? Thank you!!

e.g int[] arr = {79, 48, 35, 23, 19, 11, 7, 3};

bubbleSort(arr);

expected output:

Pass 1: [483523191173] 79

Pass 2: [3523191173] 48 79

Pass 3: [23191173] 354879

Pass 4: [191173] 23354879

Pass 5: [1173] 19 23354879

Pass 6: [7 3] 11 1923354879

Pass 7: 37111923354879

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!