Question

write a completed JAVA program (Main included) and make it not interactive but straight to the...

write a completed JAVA program (Main included) and make it not interactive but straight to the output

Write a method called partition that accepts a list of integers and an integer value E as its parameter, and rearranges (partitions) the list so that all the elements with values less than E occur before all elements with values greater than E The exact order of the elements is unimportant, so long as all elements less than E appear before all elements greatr bau E. For example, for the linked list [15, 1, 6, 12, -3, 4, 8, 21, 2, 30, -1, 9], one acceptable ordering of the list after a call of partition(list, 5) would be [-1, 1, 2, 4, -3, 12, 8, 21, 6, 30, 15, 9]. You may assume that the list contains no duplicates and does not contain the element value E.

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

This problem is similar to partitioning an array in quick sort .

only difference between quicksort partition function and this problem's partiton method is that in this method, pivot is given in addition to the list, whereas in quicksort partiton we take one element of array to be pivot.

we place pivot element at a position such that position previous to pivot have values smaller than pivot, and position ahead of pivot have values larger than pivot

(order is not important)

Below is the java code for implementing partition function:

import java.util.ArrayList;
import java.util.Scanner;

public class partition {

   public static void main(String args[]){
       ArrayList<Integer> list=new ArrayList<Integer>();
       int size,element,E;
       Scanner sr= new Scanner(System.in);
       size=sr.nextInt(); //input size of list
       for(int i=0;i<size;++i){
           element=sr.nextInt(); //inputing the list element by element
           list.add(element);
       }
       E=sr.nextInt(); //input pivot value to partition the list

         for(int i=0;i<list.size();++i)// printing original list

       {
           System.out.print(list.get(i)+" ");
       }

        System.out.println();


       partitionlist(list,E); //function call


       for(int i=0;i<list.size();++i) //printing list after partition is done

        {
           System.out.print(list.get(i)+" ");
       }
       System.out.println();
       sr.close();
   }

   static void partitionlist(ArrayList<Integer> list, int E)
   {
      
       int i=-1,j,temp;
       for(j=0;j<list.size();++j)
       {
           if(list.get(j)<=E)
           {
               ++i;
               temp=list.get(j);
               list.set(j,list.get(i));
               list.set(i,temp);
           }
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
write a completed JAVA program (Main included) and make it not interactive but straight to the...
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
  • JAVA write a completed program (must include MAIN) and straight to the output on Q1 e...

    JAVA write a completed program (must include MAIN) and straight to the output on Q1 e following exercises is a method to be added to the ArrayIntlist class from this chapter Each of the a method called lastIndexof that accepts an integer as a parameter and returns the index in the list of ce of that value, or -1 if the value is not found in the list. For example, if the list stores 1 the 40], then the last...

  • JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main...

    JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main if possible. those are just the sample HashIntSet and HashMain (don't have to be the same but follow the Q requirement. thanks HashIntSet.java public class HashIntSet { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry[] elementData; private int size; // Constructs an empty set. public HashIntSet() { elementData = new HashEntry[10]; size = 0; } // Adds the given element to...

  • JAVA (implementing a collection class) write a completed program (must include MAIN) and straight to the output based on 4. Based on the implementation of ArrayIntlist or ArrayList, write a class Sor...

    JAVA (implementing a collection class) write a completed program (must include MAIN) and straight to the output based on 4. Based on the implementation of ArrayIntlist or ArrayList, write a class SortedIntList or SortedList that provides most of the same operations but maintains its elements in sorted order. When a new value is added to the sorted list rather than appending it to the end of the list, it is placed in the appropriate index to maintain sorted order of...

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

  • Suppose the quicksort program shown uses a partition function that always picks a[m] as the 5B....

    Suppose the quicksort program shown uses a partition function that always picks a[m] as the 5B. 2M separator v. When the array a[m],....a[n] is reordered, assume that the order is preserved as much as possible. That is, first come all the elements less than v, in their original order, then all elements equal to v, and finally all elements greater than v, in their original order. int a[11]; void readArray0 {/* Reads 9 integers into int I; void quicksort( int...

  • Write a Python program that tests the function main and the functions discussed in parts a...

    Write a Python program that tests the function main and the functions discussed in parts a through g. Create the following lists: inStock - 2D list (row size:10, column size:4) alpha - 1D list with 20 elements. beta - 1D list with 20 elements. gamma = [11, 13, 15, 17] delta = [3, 5, 2, 6, 10, 9, 7, 11, 1, 8] a. Write the definition of the function setZero that initializes any one-dimensional list to 0 (alpha and beta)....

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

  • JAVA (implementing a collection class) write a completed program using ArrayIntList and Client ArrayIntList.java public...

    JAVA (implementing a collection class) write a completed program using ArrayIntList and Client ArrayIntList.java public class ArrayIntList { private int[] elementData; // list of integers private int size; // current number of elements in the list public static final int DEFAULT_CAPACITY = 100; // pre : capacity >= 0 // post: constructs an empty list with the given capacity public ArrayIntList(int capacity) { elementData = new int[capacity]; size = 0; } // post: constructs an empty list of default capacity...

  • Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file...

    Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file name, your name, and problem description) that solves the following problem with the main function: Problem Specification: The following code reads values from the file object infile and stores them in the 2d list table2: (It assumes that each line contains values of elements in each row and the values are whitespace separated.) table2 = [] for line in infile: row=line.split() intRow = []...

  • 2. Write a C++ program, that generates a set of random integers and places them in...

    2. Write a C++ program, that generates a set of random integers and places them in an array. The number of values generated and their range are entered by the user. The program then continually prompts the user for one of the following character commands: a: display all the values I: display the values less than a given value g display the values greater than a given value e: display all odd values o: display all even values q: quit...

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
ADVERTISEMENT