Question



3, Modify the splitting process in quicksort: randomly select 3 Numbers, and then select the middle number of 3 Numbers as th
0 0
Add a comment Improve this question Transcribed image text
Answer #1
package com.hackerrank.arraysorting;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class QuickSort2_Sorting {

        public QuickSort2_Sorting() {
        }

        public List<Integer> partition(List<Integer> list) {

                int pivot = list.get(0);
                List<Integer> result;
                List<Integer> leftSide = new ArrayList<>();
                List<Integer> rightSide = new ArrayList<>();

                for (int i = 0; i < list.size(); i++) {
                        if (list.get(i) < pivot) {

                                leftSide.add(list.get(i));
                        } else if (list.get(i) > pivot) {
                                rightSide.add(list.get(i));
                        }

                }

                if (leftSide.size() > 1) {
                        result = this.partition(leftSide);
                        leftSide = result;
                }
                if (rightSide.size() > 1) {
                        result = this.partition(rightSide);
                        rightSide = result;
                }
                List<Integer> combined = new ArrayList<>();
                combined.addAll(leftSide);
                combined.add(pivot);
                combined.addAll(rightSide);
                // print out
                for (int j : combined) {
                        System.out.print(j + " ");
                }
                System.out.println();

                return combined;
        }

        public static void main(String[] a) {
                Scanner in = new Scanner(System.in);
                QuickSort2_Sorting qs = new QuickSort2_Sorting();
                List<Integer> list = new ArrayList<>();
                int n = in.nextInt();
                for (int i = 0; i < n; i++) {
                        list.add(in.nextInt());
                }
                List<Integer> sorted = qs.partition(list);
                sorted.size();
                in.close();
        }
}
Add a comment
Know the answer?
Add Answer to:
3, Modify the splitting process in quicksort: randomly select 3 Numbers, and then select the midd...
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
  • 5 numbers chosen randomly without replacement. "B" represents number of even numbers, this random variable has...

    5 numbers chosen randomly without replacement. "B" represents number of even numbers, this random variable has this probability: x 0 1 2 3 4 5 p(B=x) 0.02693 0.15989 .33858 .31977 .13464 .02020 number of odd #s chosen would then be 5-x, if x is even #s chosen. "C" represents difference b/w # of even and # of odd chosen, --> C= 2B-5 a. probability that exactly 1 even # chosen? b. probability at most 1 even # chosen? c. prob....

  • 9. Births in a hospital occur randomly at an average rate of 1.8 births per hour. process is a Po...

    please solve all questions... emergency 9. Births in a hospital occur randomly at an average rate of 1.8 births per hour. process is a Poisson random variable. Assume that birth (3 pts) What about the probability of observing more than or equal to 2 births in a given hour at the hospital? b. e opo Wahat he probabiliy h dly 10 biths n day t d. What is the expected number of births in two hours? 10. Suppose that on...

  • We have a standard deck of 52 French (Bridge) cards, from which we randomly select a...

    We have a standard deck of 52 French (Bridge) cards, from which we randomly select a hand of 5. What is the probability that we get a “street”, i.e. a sequence of 5 consecutive numbers/figures? (The suits are irrelevant. The streets are NOT cyclic, i.e. the ”A” is not followed by ”2”.)

  • 2) (5pts) A 3-digit number will be formed using the digits 1, 2, 3, 4, 5;...

    2) (5pts) A 3-digit number will be formed using the digits 1, 2, 3, 4, 5; using each digit only once. a) How many possible 3-digit numbers are possible? TOTAL: b) Assume three of the digits are randomly chosen and randomly permuted in order to form the 3-digit number. Then each of the possible 3-digit numbers in part a) are equally likely. Find the probability that the 3-digit number ends up being an even number greater than or equal to...

  • C++ Quesetion In this exercise, you are to modify the Classify Numbers programming example in this...

    C++ Quesetion In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an unspecified length; that is, the program does not know in advance how many...

  • A lottery is conducted in which 7 winning numbers are randomly selected from a total of...

    A lottery is conducted in which 7 winning numbers are randomly selected from a total of 62 numbers (1-62). In addition, the Powerball, a single winning number, is selected from an independent pool of 26 numbers (1-26). You select 7 numbers from the pool of 62 numbers. What is the probability that you selected 4 winning numbers?

  • 3. A new artist stands in the middle of the Hollywood Walk of Fame and randomly...

    3. A new artist stands in the middle of the Hollywood Walk of Fame and randomly asks tourists to buy his/her new CD. The probability of successfully selling a CD is 0.10. Let X be the number of tourists asked to purchase a CD when the first CD is sold. (a) What distribution does X follow? State the name and parameters. (c) What is the variance and standard deviation of the number of attempts needed before selling a CD? (d)...

  • Please respond both parts (ASAP) month is normally distributed with a mean of $3000 and The...

    Please respond both parts (ASAP) month is normally distributed with a mean of $3000 and The average spending by middle-income family in a standard deviation 600. (Example 6, 268) 3+5 8 What is the probability that a randomly selected family in that income group will be spending less than $2670 a. b. Now, you randomly select 20 familles. What is the probability that a person spends more than $28702

  • A population consists of 3 numbers: 2, 10 and 6. You randomly select a sample of...

    A population consists of 3 numbers: 2, 10 and 6. You randomly select a sample of 2 observations without replacement and calculate the sample mean. The variance of the sample mean is _____. Group of answer choices 4 8/3 4/6

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

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