Modify the BubbleSort() method so that it\'s bidirectional. Meaning the in index
ID: 3551233 • Letter: M
Question
Modify the BubbleSort() method so that it's bidirectional. Meaning the in index will 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; j1; out--) // outer loop (backward) for(in=0; 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; } //-------------------------------------------------------------- public class BidirectionalBubbleSort { /** * The bidirectional bubble sort method that takes * an array of unsorted integers, sorts them and * returns a sorted array. */ public static int[] bidirectionalBubbleSort(int array[]) { int length = array.length; int j; int st = -1; while (st;lt;length) { st++; length--; for (j = st; j < length; j++) { if (array[j] > array[j + 1]) { swap(array, j, j + 1); } } for (j = length; --j >= st;) { if (array[j] > array[j + 1]) { swap(array, j, j + 1); } } } return array; } /** * Swapping The Elements at two different indexes of an Arary */ private static void swap(int[] anArray, int firstIndex, int secondIndex) { int temp = anArray[firstIndex]; anArray[firstIndex] = anArray[secondIndex]; anArray[secondIndex] = temp; } /** * Method to print the content of any array. */ public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) System.out.print(array[i] + " "); System.out.println(); } /** * Testing the bidirectional bubble sort with some sample unsorted array of * integers */ public static void main(String a[]) { int unsortedArray[] = { 121, 79, 24, 190, 203, 1, 33, 110 }; System.out.print("Unsorted: "); printArray(unsortedArray); int[] sortedArray = bidirectionalBubbleSort(unsortedArray); System.out.print("Sorted: "); printArray(sortedArray); } } // 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 //////////////////////////////////////////////////////////////// Where do I insert?? public static int[] bidirectionalBubbleSort(int array[]) { int length = array.length; int j,i; for(i = 0; i < len; i ++) { for(j = i+1; j< len ;j++) { if( arr[i] > arr[j]) swap(arr, i , j ); } for(j = len -1 ; j >i ; j--) { if( arr[i] < arr[j]) swap(arr, i , j ); } len -- ; } return array; }Explanation / Answer
public static int[] bidirectionalBubbleSort(int array[]) {
int length = array.length;
int j;
int st = -1;
while (st < length) {
st++;
length--;
for (j = st; j < length; j++) {
if (array[j] > array[j + 1]) {
swap(array, j, j + 1);
}
}
for (j = length; --j >= st;) {
if (array[j] > array[j + 1]) {
swap(array, j, j + 1);
}
}
}
return array;
}
/**
* Swapping The Elements at two different indexes of an Arary
*/
private static void swap(int[] anArray, int firstIndex, int secondIndex) {
int temp = anArray[firstIndex];
anArray[firstIndex] = anArray[secondIndex];
anArray[secondIndex] = temp;
}
/**
* Method to print the content of any array.
*/
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
System.out.println();
}
/**
* Testing the bidirectional bubble sort with some sample unsorted array of
* integers
*/
public static void main(String a[]) {
int unsortedArray[] = { 121, 79, 24, 190, 203, 1, 33, 110 };
System.out.print("Unsorted: ");
printArray(unsortedArray);
int[] sortedArray = bidirectionalBubbleSort(unsortedArray);
System.out.print("Sorted: ");
printArray(sortedArray);
}
/*
Sample Output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.