Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions