Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

applet, the in index always goes from left to right, finding the largest item an

ID: 3675203 • Letter: A

Question

applet, 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 it’s

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. You’ll need two outer indexes, one on the right (the old

out) and another on the left.

// bubbleSort.java
// demonstrates bubble sort
// to run this program: C>java BubbleSortApp
////////////////////////////////////////////////////////////////
class ArrayBub
{
private long[] a; // ref to array a
private int nElems; // number of data items
//--------------------------------------------------------------
public ArrayBub(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
//--------------------------------------------------------------
public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//--------------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//--------------------------------------------------------------
public void bubbleSort()
{
int out, in;

for(out=nElems-1; out>1; out--) // outer loop (backward)
for(in=0; in<out; in++) // inner loop (forward)
if( a[in] > a[in+1] ) // out of order?
swap(in, in+1); // swap them
} // end bubbleSort()
//--------------------------------------------------------------
private void swap(int one, int two)
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
//--------------------------------------------------------------
} // end class ArrayBub
////////////////////////////////////////////////////////////////
class BubbleSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ArrayBub arr; // reference to array
arr = new ArrayBub(maxSize); // create the array

arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);

arr.display(); // display items

arr.bubbleSort(); // bubble sort them

arr.display(); // display them again
} // end main()
} // end class BubbleSortApp
////////////////////////////////////////////////////////////////

BB WorkshopApplets.ZIP - 20 x PoF data structures and_algor x Chegg.com -> L file:///C:/Users/Natalia/Downloads/WorkshopApplets/Chap03/Bubble/BubbleSort.html / D Bubble Sort Apps u Blackboard Learn 9. Technology Services... Lafore's Bubble Sort Bubble Sort Operation New creates new data and initializes sort; toggles between random and inverse order Size toggles between 10 bars and 100 bars; also creates new data and initializes sort. Draw Redraws bars. Run starts the sorting process running automatically. (Push Step to pause. Run to resume.) Step executes one step of sorting process. t O Ask me anything 12:14 PM 3/6/2016

Explanation / Answer

import java.util.Random; // 1st line of java program

////////////////////////////////////////////////////////////////
class ArrayBub
{
private long[] a;                 // ref to a a
private int nElems;               // number of data items
//--------------------------------------------------------------
public ArrayBub(int max)          // constructor
{
a = new long[max];                 // create the a
nElems = 0;                        // no items yet
}
//--------------------------------------------------------------
public void insert(long value)    // put element into a
{
a[nElems] = value;             // insert it
nElems++;                      // increment size
}
//--------------------------------------------------------------
public void display()             // displays a contents
{
for(int j=0; j<nElems; j++)       // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//--------------------------------------------------------------
public void bubbleSort()
{
int left = 0, right = nElems-1;
       while (left < right)
       {
          for (int pos = left; pos < right; pos++)
          {
             if (a[pos] > a[pos+1])
                swap(pos, pos+1);
          }
          right--;


          for (int pos = right; pos > left; pos--)
          {
             if (a[pos] < a[pos-1])
               swap(pos, pos-1);
          }
          left++;
       }
} // end bubbleSort()
//--------------------------------------------------------------

private void swap(int one, int two)
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
//--------------------------------------------------------------
} // end class aBub