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.

Answers

Answer 1

Answer:

See Explaination

Explanation:

public class Partitioner {

public static int partition(int[] values){

if(values==null || values.length==0)return 0;

// storing the pivot value

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

//sorting the array

for(int i=0;i<values.length-1;i++){

int m_index = i;

for (int j=i+1;j<values.length;j++)

if(values[j]<values[m_index])

m_index = j;

int tmp = values[m_index];

values[m_index] = values[i];

values[i] = tmp;

}

int i = 0;

// first finding the index of pivot

// value in sorted order and recording index in i

while (i<values.length){

if(pivot==values[i]){

if(i==values.length-1)break;

int j=0;

// finding the location for inserting the

while (j<values.length){

if(pivot<=values[j]){

// inserting the values

values[i] = values[j];

values[j] = pivot;

break;

}

j++;

}

break;

}

i++;

}

return i;

}

// main method for testing can be removable

public static void main(String[] args) {

int a[] = {4,1,6,2};

System.out.println(partition(a));

}// end of main

}


Related Questions

Create a public non-final class named Larger parameterized by a type T that implements Comparable. (Please use T or the test suite will fail.) You should provide a single instance method named larger that accepts an array of the parameterized type as the first argument and a single value of the parameterized type as the second argument. larger should return true if the second argument is larger than or equal to every value of the array and false otherwise. If either the array or the value are null you should throw an IllegalArgumentException. As an ungraded bonus challenge, see if you can make the compiler warning about unchecked operations go away…​ (Note that normally we would write this as a class method. Java does support type parameters for static methods, but we aren’t going to cover that in class. So we’ll use an instance method here instead.) Note also that this homework is not due until Friday but was accidentally released Thursday. It does rely on material we will cover Friday. Feel free to wait to complete it then.

Answers

Answer:

see explaination

Explanation:

class Larger<T extends Comparable<T>> {

public boolean larger(T[] arr, T item) {

if (arr == null || item == null)

throw new IllegalArgumentException();

for (int i = 0; i < arr.length; i++) {

if (item.compareTo(arr[i]) < 0) {

return false;

}

}

return true;

}

}

Which of the following is a possible disadvantage of recursion? Question 10 options: Recursive solutions can be less efficient than their iterative counterparts Recursive solutions tend to be longer than their iterative counterparts Recursive solutions are more likely to go into infinite loops than their iterative counterparts Recursive solutions tend to have more local variables than their iterative counterparts

Answers

Answer:

Recursive solutions can be less efficient than their iterative counterparts

Explanation:

Recursion can be defined or described as a method of solving a problem where the solution depends on solutions to smaller instances of the same problem.

It entails using iteration to ensure that smaller parts of a solution are satisfied towards solving thw overall problem.

Ita major disadvantage seems to be that it seem to be less efficient than their iterative counterparts. This is as a result of concentrating on trying to solve a smaller instances.

The electric company gives a discount on electricity based upon usage. The normal rate is $.60 per Kilowatt Hour (KWH). If the number of KWH is above 1,000, then the rate is $.45 per KWH. Write a program (L4_Ex1.cpp) that prompts the user for the number of Kilowatt Hours used and then calculates and prints the total electric bill. Please put comment lines, same as in Lab3, at the beginning of your program. According to your program in Lab 4.1, how much will it cost for: 900 KWH? 1,754 KWH? 10,000 KWH?

Answers

Answer:

The cpp program for the given scenario is shown below.

#include <stdio.h>

#include <iostream>

using namespace std;

int main()

{

   //variables to hold both the given values

   double normal_rate = 0.60;

   double rate_1000 = 0.45;

   //variable to to hold user input

   double kwh;

   //variable to hold computed value

   double bill;

   std::cout << "Enter the number of kilowatt hours used: ";

   cin>>kwh;

   std::cout<<std::endl<<"===== ELECTRIC BILL ====="<< std::endl;

   //bill computed and displayed to the user

   if(kwh<1000)

   {

       bill = kwh*normal_rate;

       std::cout<< "Total kilowatt hours used: "<<kwh<< std::endl;

       std::cout<< "Rate for the given usage: $"<<normal_rate<< std::endl;

       std::cout<< "Total bill: $" <<bill<< std::endl;

   }

   else  

   {

       bill = kwh*rate_1000;

       std::cout<< "Total kilowatt hours used: "<<kwh<< std::endl;

       std::cout<< "Rate for the given usage: $"<<rate_1000<< std::endl;

       std::cout<< "Total bill: $" <<bill<< std::endl;

   }

   std::cout<<std::endl<< "===== BILL FOR GIVEN VALUES ====="<< std::endl;

   //computing bill for given values of kilowatt hours

   double bill_900 = 900*normal_rate;

   std::cout << "Total bill for 900 kilowatt hours: $"<< bill_900<< std::endl;

   double bill_1754 = 1754*rate_1000;

   std::cout << "Total bill for 1754 kilowatt hours: $"<< bill_1754<< std::endl;

   double bill_10000 = 10000*rate_1000;

   std::cout << "Total bill for 10000 kilowatt hours: $"<< bill_10000<< std::endl;

   return 0;

}

OUTPUT

Enter the number of kilowatt hours used: 555

===== ELECTRIC BILL =====

Total kilowatt hours used: 555

Rate for the given usage: $0.6

Total bill: $333

===== BILL FOR GIVEN VALUES =====

Total bill for 900 kilowatt hours: $540

Total bill for 1754 kilowatt hours: $789.3

Total bill for 10000 kilowatt hours: $4500

Explanation:

1. The program takes input from the user for kilowatt hours used.

2. The bill is computed based on the user input.

3. The bill is displayed with three components, kilowatt hours used, rate and the total bill.

4. The bill for the three given values of kilowatt hours is computed and displayed.

A user in the accounting department reports he or she cannot access the invoices that the sales department has placed on the shared drive. This points toward a possible problem with which component of the computer’s operating system? Networking Time-sharing Interrupts Device Driver

Answers

Answer:

Networking.

Explanation:

An operating system which was developed in the 1950s, is a software which acts as an intermediary between the computer hardware and end users.

The functions of an Operating System are; Memory, Device, Process, File, Secondary-Storage and Input/Output management.

The networking component of the computer's operating system ensures that a group of processors don't share memory, clock and hardware devices, instead the processors communicate with each other through the network.

Basically, the network Operating System (OS) runs on a server and provides the capability to serve to manage groups, user, application or program, data, security and any other networking functions.

Hence, the accountant couldn't access the invoices that the sales department placed on the shared drive because of a networking component problem of the computer’s operating system.

When composing an email message:a.ideas should be organized inductively when the message contains good news or routine information.b.just be direct, since such communications are routine.c.present the information in the order it is likely needed or will be best received.d.avoid repeating information that is in the subject line in the opening sentence.

Answers

Question:

When composing an email message:

A) ideas should be organized inductively when the message contains good news or routine information.

B) just be direct, since such communications are routine.

C) present the information in the order it is likely needed or will be best received.

D) avoid repeating information that is in the subject line in the opening sentence.

Answer:

The correct answer is C)

When writing emails, it helps to put ones self in the shoes of the recipient. This helps us to present our thoughts in the way that the recipient will best receive them.

In addition to the above, one must ensure that they go directly to the point, use a courteous tone, and ensure that the message is free from typographical errors whenever he or she is writing an email.

Cheers!

Other Questions
Direct Materials Variances De Soto Inc. produces tablet computers. The company uses Thin Film Crystal (TFC) LCD displays for its products. Each tablet uses one display. The company produced 770 tablets during July. However, due to LCD defects, the company actually used 800 LCD displays during July. Each display has a standard cost of $12.50. Eight hundred LCD displays were purchased for July production at a cost of $9,400. Determine the price variance, quantity variance, and total direct materials cost variance for July. What is the basic difference between the two forms that society takes in Durkheim? As the word implies, societies that exhibit organic solidarity have a simpler division of labor and a group conscience. Societies that exhibit a mechanical solidarity have machine-like divisions of labor and increased individualism. Organic societies are collectives, cohesive, and communal, whereas mechanical societies are dehumanizing, deleterious, and capitalistic. Mechanical societies are those that came with the Industrial Revolution. Organic, that is, agrarian societies represent what came before. Societies that exhibit mechanical solidarity have a simpler division of labor and a group conscience. Societies that exhibit organic solidarity have complex divisions of labor and increased individualism. g Exhibit 31-3 Costs of Eliminating: Firm A Firm B Firm C 1st unit of pollution $ 20 $ 50 $ 500 2nd unit of pollution $ 60 $100 $ 700 3rd unit of pollution $120 $180 $1,000 4th unit of pollution $200 $350 $1,500 5th unit of pollution $300 $500 $2,500 6th unit of pollution $400 $600 $4,000 Refer to Exhibit 31-3. What is the cost to Firm A of eliminating 4 units of pollution JK has endpoints at J(0, 7) and K(10, 10). Find the midpoint M of JK. Choose the best Spanish word to complete the sentence.El doctor estla temperatura.O tometomamostomotomando Two protons are 0.00500 m apart. Find the force between them. what wrestling faction were titus O'neil, Apollo crews, akira tozawa, and ana brooke in The 24th Amendment to the Constitution made __________ illegal in the United States. What is the measure of an angle whose sine is3/2 ? 60 45 30please I need entire explanation, and how do I use my calculator to fin the answer. A politician is ordering T-shirts for his upcoming campaign. The politician can spend up to $588 on the T-shirts. If each T-shirt costs $7, how many T-shirts will the politician be able to buy?581845954,116 Number five Please I really need it O Football players are 33% more likely to get injured than baseball players.O Baseball is a fun sport.Football is more dangerous than baseball. The hair colors of the members of a family are listed below.mother - brown hairfather - blond hairolder son - brown hairyounger son-blond hairThe hair colors of the sons are most likely a direct result of1natural selection in males2heredity3.evolution4.environmental influences What fraction is equivalent to 5.1? is a rationalk number? Explain. A federally funded research study involving children 8 to 12 years old involves collecting a single voided urine sample to assess the frequency of asymptomatic proteinuria (higher amounts of protein in the urine without any signs or symptoms of illness or infection). According to 45 CFR 46, an IRB's risk assessment would likely conclude that this study involves:__________A. No risk to the child and no further IRB review is requiredB. No more than minimal risk to the childC. More than minimal risk with prospect of direct benefit to the childD. More than minimal risk with no prospect of direct benefit to the child Rate the following bank accounts from most to least liquid: CD, savings account, checking account, money market account.a CD, savings account, checking account, money market accountb. Savings account, checking account, CD, money market accountC. CD, money market account, savings account, checking accountd. Checking account, savings account, money market account, CDPlease select the best answer from the choices providedMark this and returnSave and ExitNextSubmit Which of the following people live around Mount Everest and help people climb the mountain? why did the civil rights legislation realized the potential of the equal protection clause YO Gang i need yo help pleaseeeeee!!1. What is 5/6 of an hour?2. What is 2/3 of an hour?3. What is 0.25 of an hour? Which conclusion is true based on the information in this passage?Changing one organism in a food web would not have any effect on any other food web.oIt is easy to see all the ways that food webs are connected.Food chains are always separate from each other.Food webs are connected to each other and sensitive to any changes, Steam Workshop Downloader