Question

Below I have done a bubble sort on an array. I need to figure out how...

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 for odd then another pass to sort even... I cannot sort the array first then just take odd and even,

example I was working on is from this

        while (!isSorted) {

            isSorted = true;

            int temp = 0;

for (int i = 1; i <= n - 2; i = i + 2) {
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
isSorted = false;
}

then second run

  for (int i = 0; i <= n - 2; i = i + 2) {

                if (arr[i] > arr[i + 1]) {

                    temp = arr[i];

                    arr[i] = arr[i + 1];

                    arr[i + 1] = temp;

                    isSorted = false;

                }

Perform Bubble sort on odd indexed element and Perform Bubble sort on even indexed element, then show sorted array.

BELOW IS THE WORKING CODE SO FAR

class ArrayBub
{
   private long[] a;
   private int nElems;
  
   public ArrayBub(int max)
   {
       a = new long[max];
       nElems = 0;
   }
  
   public void insert(long value)
   {
       a[nElems] = value;
       nElems++;
   }
  
   public void display()
   {
       for(int j=0; j            System.out.print(a[j] + " ");
       System.out.println("");
   }
  
   public void bubbleSort()
   {
       int out, in;
       for(out=nElems-1; out>1; out--)
           for(in=0; in                if(a[in] > a[in+1] )
                   swap(in, in+1);
   }
  
   private void swap(int one, int two)
   {
       long temp = a[one];
       a[one] = a[two];
       a[two] = temp;
   }
  
}

class BubbleSortApp
{
public static void main(String[] args)
   {
   int maxSize = 100;
   ArrayBub arr;
   arr = new ArrayBub(maxSize);
  
   arr.insert(77);
   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();
  
   arr.bubbleSort();
  
   arr.display();
  
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

class ArrayBub
{
private long[] a;
private int nElems;
  
public ArrayBub(int max)
{
a = new long[max];
nElems = 0;
}
  
public void insert(long value)
{
a[nElems] = value;
nElems++;
}
  
public void display()
{
for(int j=0; j<nElems;j++)
System.out.print(a[j] + " ");
System.out.println("");
}
  
public void oddEvenSort()
{
int n=nElems;
boolean isSorted = false; // Initially array is unsorted
  
while (!isSorted)
{
isSorted = true;
long temp =0;
  
// Perform Bubble sort on odd indexed element
for (int i=1; i<=n-2; i=i+2)
{
if (a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
isSorted = false;
}
}
  
// Perform Bubble sort on even indexed element
for (int i=0; i<=n-2; i=i+2)
{
if (a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
isSorted = false;
}
}
}
}
  
  
}

public class Main
{
public static void main(String[] args)
{
int maxSize = 100;
ArrayBub arr;
arr = new ArrayBub(maxSize);
  
arr.insert(77);
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();
  
arr.oddEvenSort();
  
arr.display();
  
}
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Below I have done a bubble sort on an array. I need to figure out how...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the a...

    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...

  • // ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; //...

    // 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,...

  • Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This...

    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;...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    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...

  • To the HighArray class in the highArray.java, add a method called getMax() that returns the value...

    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...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    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...

  •   Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first...

      Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first iteration? Given an insertion sort and the following array: [50,5,35,70,6] What does the array look like after the first insertion:    Fill in the missing code for InsertionSort method // Insertion Sort method //sorts an array of Auto objects public static void insertionSort(Auto [] arr) { Auto temp; int j; for (int i=1; i<arr.length; i++) {     j=i;     temp=arr[i];                while (j!=0 &&(temp.getModel().compareTo(arr[[j-1].getModel())<0)...

  • Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

    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...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT