Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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:
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 ETHTHttlSTHANBEtErrStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started