1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
dybincka [34]
3 years ago
13

Create a public non-final class named Partitioner. Implement a public static method int partition(int[] values) that returns the

input array of ints partitioned using the last array value as the pivot. All values smaller than the pivot should precede it in the array, and all values larger than or equal to should follow it. Your function should return the position of the pivot value. If the array is null or empty you should return 0. You will probably want to review the array partitioning algorithm presented in class. Note that we are asking you to partition based on the last value in the array, rather than the first. However, you can address this difference with a small adjustment before you begin.
Computers and Technology
1 answer:
Nuetrik [128]3 years ago
8 0

Answer:

Check the explanation

Explanation:

class Partioner {

   public static int partition(int[] values) {

       if (values == null || values.length < 1)

           return 0;

       // getting pivot value

       int pivot = values[values.length - 1];

       // pivot index

       int pivot_index = values.length - 1;

       // looping through values

       for (int i = values.length - 2; i >= 0; i--) {

         

           if (values[i] == pivot) {

               pivot_index--;

               continue;

           }

            // if immediate number is greater than pivot

           if (values[i] > pivot) {

               if (pivot_index - i == 1) {

                   int temp = values[i];

                   values[i] = pivot;

                   values[pivot_index] = temp;

                   pivot_index--;

               } else {

                   int temp = values[i];

                   values[i] = values[pivot_index - 1];

                   values[pivot_index - 1] = temp;

                   temp = values[pivot_index - 1];

                   values[pivot_index] = temp;

                   values[pivot_index - 1] = pivot;

                   pivot_index--;

               }

           }

       }

       // returning pivot index

       return pivot_index;

   }

   // testing the function

   public static void main(String[] args) {

       int values[] = { 11, 9, 2, 7, 21, 6, 10, 10, 10 };

       System.out.println("pivot index : " + Partioner.partition(values));

       System.out.println();

   }

}

// OUTPUT

pivot index : 4

You might be interested in
• Open your Netbeans IDE and answer the following question
VARVARA [1.3K]

Answer:

public static void main(String[] args)

   {

       int cdCount;

       double cdCountAfterDiscount;

       DecimalFormat df = new DecimalFormat("$##.##"); // Create a Decimal Formatter for price after discount

       System.out.println("Enter the amount of CD's bought");

       Scanner cdInput = new Scanner(System.in);  // Create a Scanner object

       cdCount = Integer.parseInt(cdInput.nextLine());  // Read user input

       System.out.println("CD Count is: " + cdCount);  // Output user input

       if(cdCount <= 14 )

       {

           System.out.println("There is no discount");

           System.out.println("The final price is " + cdCount*3.5);

       }

       if(cdCount >= 15 && cdCount<=50)

       {

           System.out.println("You have a 1% discount.");

           cdCountAfterDiscount = (cdCount *3.5)-(3.5*.01);

           System.out.println("The final price is " + df.format(cdCountAfterDiscount));

       }

       if(cdCount >= 51 && cdCount<120)

       {

           System.out.println("You have a 5% discount.");

           cdCountAfterDiscount = (cdCount *3.5)-(3.5*.05);

           System.out.println("The final price is " + df.format(cdCountAfterDiscount));

       }

       if(cdCount >= 120)

       {

           System.out.println("You have a 10% discount.");

           cdCountAfterDiscount = (cdCount *3.5)-(3.5*.1);

           System.out.println("The final price is " + df.format(cdCountAfterDiscount));

       }

   }

8 0
3 years ago
Name an analog quantity other than temperature and sound
liq [111]
I know that this might be wrong but is it light?
6 0
4 years ago
You can put a small level on your camera to help keep the horizons straight? TRUE OR FALSE
anastassius [24]
The answer is true and i believe it is best to put it at the 4 intersecting points<span />
5 0
3 years ago
Read 2 more answers
A user is having an issue updating an app from Windows Store. Where you go to find more information on the error?
rusak2 [61]

Answer:

A d depends on computer

Explanation:

5 0
3 years ago
Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print
ANEK [815]

Answer:

First for loop is for printing the rows.

Then we set currColumnLetter to 'A' for first time

Second for loop is for printing the columnletter with each row

Explanation:

4 0
3 years ago
Read 2 more answers
Other questions:
  • Which item is essential to know before sketching a navigation menu flowchart
    15·2 answers
  • The utilization of a subset of the performance equation as a performance metric is a pitfall. To illustrate this, assume the fol
    13·1 answer
  • You are designing a wireless network for a client. your client needs the network to support a data rate of at least 54 mbps. in
    10·1 answer
  • How to divert all calls &amp; sms from other phone to my phone? is it possible?
    14·1 answer
  • Which of the following statements will ivoke the function get_course and store the reutrn values correctly? A. get_course(course
    8·1 answer
  • What are the basic steps in creating a new program
    5·1 answer
  • Which statements identify what astronomers currently know and think will happen with our universe? Check all that apply.
    15·2 answers
  • Which of the following statements about cover letters is false?
    14·2 answers
  • Why would an information systems security practitioner want to see network traffic on both internal and external network traffic
    5·1 answer
  • The rules that govern the correct order and usage of the elements of a language are called the of the language:.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!