Ask Question
17 March, 12:26

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.

+2
Answers (1)
  1. 17 March, 15:31
    0
    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
    int m_index = i;

    for (int j=i+1; j
    if (values[j]
    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
    if (pivot==values[i]) {

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

    int j=0;

    / / finding the location for inserting the

    while (j
    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

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Create a public non-final class named Partitioner. Implement a public static method int partition (int[] values) that returns the input ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers