Question: In the bubbleSort.java program (Listing 3.1 in LaFore book), the in index always goes from left to right, finding the largest item and carrying it

In the bubbleSort.java program (Listing 3.1 in LaFore book), the in index always goes from left to right, finding the largest item and carrying it toward out on the right. Modify the bubbleSort() method so that its bidirectional. This means the in index will first carry the largest item from left to right as before, but when it reaches out, it will reverse and carry the smallest item from right to left. Youll need two outer indexes, one on the right (the old out) and another on the left.

Listing 3.1 in Lafore Book:In the bubbleSort.java program (Listing 3.1 in LaFore book), the in indexalways goes from left to right, finding the largest item and carrying

it toward out on the right. Modify the bubbleSort() method so that

i bubbleSort. java i denonstrates bubble sort // to run this program: >java BubbleSortApp EITETiUNELEENErtt class ArrayBub private longtI a; private int nElems; i ref to array a i nunber of data items public ArrayBub(int max) i constructor a- new long[max]; f create the array i no itens yet public void insert (long value) put elenent into array a[nElems)value; Elens++ I insert it I increment size public void display() II displays array contents for(int j-jnElems; j++) I for each element, Systen.out.print (aj) Systen.out.printin display it public void bubbleSort() int out, in; for(out-nElems-1; out 1; out) outer loop (backward) f inner loop (forward) f out of order? f swap them for(in-e; incout; inte) if alin] alin+1) swaptin, in1); end bubblesort) private void swap(int one, int tuo) long temp a(one]; altotemp; l end class ArrayBub class BubbleSortApp public static void main(String[] args) int naxSize 100; ArrayBub arr: arr-ne ArrayBub(maxSize) I create the array Ii array size i reference to array arr.insert (77) arr.insert (99) arr.insert (44) arr.insert (55); arr II insert 10 items TIIEtiiIFEErrutiii class BubbleSortApp public static void main(String) args) int naxSize100; ArrayBub arr; arr ne ArrayBub(maxSize): I create the array If array size /i reference to array arr.insert(77) arr.insert(99); arr.insert(44) arr.insert (55); arr.insert (22); arr.insert (88 arr. insert ( 1 1 ); arr.insert (00) arr.insert (66); arr.insert(33); I insert 10 items arr.display)i I display items arr.bubblesort): arr.display i bubble sort then display then again end nain() end class BubblesortApp ETHTHttlSTHANBEtErr i bubbleSort. java i denonstrates bubble sort // to run this program: >java BubbleSortApp EITETiUNELEENErtt class ArrayBub private longtI a; private int nElems; i ref to array a i nunber of data items public ArrayBub(int max) i constructor a- new long[max]; f create the array i no itens yet public void insert (long value) put elenent into array a[nElems)value; Elens++ I insert it I increment size public void display() II displays array contents for(int j-jnElems; j++) I for each element, Systen.out.print (aj) Systen.out.printin display it public void bubbleSort() int out, in; for(out-nElems-1; out 1; out) outer loop (backward) f inner loop (forward) f out of order? f swap them for(in-e; incout; inte) if alin] alin+1) swaptin, in1); end bubblesort) private void swap(int one, int tuo) long temp a(one]; altotemp; l end class ArrayBub class BubbleSortApp public static void main(String[] args) int naxSize 100; ArrayBub arr: arr-ne ArrayBub(maxSize) I create the array Ii array size i reference to array arr.insert (77) arr.insert (99) arr.insert (44) arr.insert (55); arr II insert 10 items TIIEtiiIFEErrutiii class BubbleSortApp public static void main(String) args) int naxSize100; ArrayBub arr; arr ne ArrayBub(maxSize): I create the array If array size /i reference to array arr.insert(77) arr.insert(99); arr.insert(44) arr.insert (55); arr.insert (22); arr.insert (88 arr. insert ( 1 1 ); arr.insert (00) arr.insert (66); arr.insert(33); I insert 10 items arr.display)i I display items arr.bubblesort): arr.display i bubble sort then display then again end nain() end class BubblesortApp ETHTHttlSTHANBEtErr

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 Databases Questions!