Another simple sort is the odd-even sort. The idea is to
repeatedly make two
passes through the array. On the first pass you look at all the
pairs of items,
a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key
values are out of
order, you swap them. On the second pass you do the same for all
the even
values (j = 2, 4, 6, …). You do these two passes repeatedly until
the array is
sorted. Replace the bubbleSort() method in bubbleSort.java (Listing
3.1) with
an oddEvenSort() method. Make sure it works for varying amounts of
data.
You’ll need to figure out how many times to do the two passes.
LISTING 3.1 The bubbleSort.java Program
// 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()
Bubble Sort 85
{
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
86 CHAPTER 3 Simple Sorting
LISTING 3.1 Continued
arr.display(); // display them again
} // end main()
} // end class BubbleSortApp
////////////////////////////////////////////////////////////////
The main() routine inserts 10 items into the array in random order,
displays the
array, calls bubbleSort() to sort it, and then displays it again.
Here’s the output:
77 99 44 55 22 88 11 0 66 33
0 11 22 33 44 55 66 77 88 99
import java .util.*;
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();
}
//--------------------------------------------------------------
//oddEveNSort..................................................
public void oddEvenSort()
{
boolean isSorted = false; // Initially array is unsorted
int passes=0;
while (!isSorted)
{
isSorted = true;
// Perform Bubble sort on odd indexed element
for (int i=1; i<=nElems-2; i=i+2)
{
if (a[i] > a[i+1])
{
swap(i,i+1);
passes++;
isSorted = false;
}
}
// Perform Bubble sort on even indexed element
for (int i=0; i<=nElems-2; i=i+2)
{
if (a[i] > a[i+1])
{
swap(i,i+1);
passes++;
isSorted = false;
}
}
}
System.out.printf("Number of times passes
occurs: %d ", passes);
System.out.println();
}// end oddEvenSort()
//--------------------------------------------------------------
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.oddEvenSort(); // bubble sort them
arr.display(); // display them again
} // end main()
} // end class BubbleSortApp
Code
![1 import java .util.; 2 class ArrayBub 4 private long[] a; // ref to arraya 5 private int nElems; // number of data items 7 8](http://img.homeworklib.com/images/e25cd9e4-d1fe-430c-a933-c7f17d003d29.png?x-oss-process=image/resize,w_560)



Output

Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the a...
Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This method should return the median value in the array. (Recall that in a group of numbers half are larger than the median and half are smaller.) Do it the easy way. LISTING 3.3 The insertSort.java Program // insertSort.java // demonstrates insertion sort // to run this program: C>java InsertSortApp //-------------------------------------------------------------- class ArrayIns { private long[] a; // ref to array a private int nElems;...
Below I have done a bubble sort on an array. I need to figure out how to change the --- bubbleSort() method... with a new method called oddEvenSort() method.. I know I have to do two passes one for odd, one for even, can I have someone help me do this please I keep getting a lot of errors when I attempt to do it. Java code to change is below. I have to sort the array using one pass...
// ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; // ref to array a // number of data items public ArrayIns(int max) // constructor a = new long[max]; nElems - © // create the array // no items yet --- public void insert(long value) // put element into array a[nElems] = value; nElems++; // insert it // increment size public void display() // displays array contents for(int j=0; j<ntlems; 1++) 1/ for each element,...
To the HighArray class in the highArray.java, add a method called getMax() that returns the value of the highest key in the array, or -1 if the array is empty. Add some code in main() to exercise this method. You can assume all the keys are positive numbers. // highArray.java class HighArray { private long[] a; private int nElems; public HighArray(int max) { a = new long[max]; nElems = 0; } public boolean find(long searchKey) { int...
JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...
Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods. Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement Element to be found */ int findAll(int...
Aim To improve the algorithm provided in Snippet 2.3 by reducing the number of passes. public void sortImprovement1(int[] numbers) { for (int i = 1; i < numbers.length; i++) { for (int j = 0; j < numbers.length - i; j++) { if (numbers[j] > numbers[j + 1]) { swap(numbers, j, j + 1); } } } } Snippet 2.3: Bubble sort improvement 1 Change the bubble sort method so that it stops sorting if the array is untouched after...
Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter { /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort */ public static void sort(int[] arr) { // Your...
Write a method for the Queue class in the queue.java program (Listing 4.4) that displays the contents of the queue. Note that this does not mean simply displaying the contents of the underlying array. You should show the queue contents from the first item inserted to the last, without indicating to the viewer whether the sequence is broken by wrapping around the end of the array. Be careful that one item and no items display properly, no matter where front...
Make the bubble sort end if it makes a complete pass through the elements to be sorted that doesn’t make any swaps. Demonstrate that it works using SortDriver.java. (Hint: You can’t easily make a for loop end early. So you will want to convert the outermost for loop to a while, so that it can terminate early. Hint 2: You will need a variable to keep track of whether any swaps have been performed this time, and it needs to...