Una QuickSelect con una mediana de cinco elementos

I have been trying to implement the quickselect algorithm as part of the homework. I have read up on the way quicksort works to know how the partition works and finding the median of five elements etc. But when it comes to the partitioning, I am in a soup.

I realize that the result of my partition does not contain the elements in the (less than pivot) | pivot | (greater than pivot) form. I have been breaking my head over this, but I can't seem to get the hang of it.

The quick select algorithm I have adapted from Wikipedia and I am using the iterative version (a requirement mentioned to me). My quickselect algorithm is as follows:

public static Numbers quickselect(Numbers[] list, int arraylength,
        int kvalue) {

    int start = 0, end = arraylength - 1;
    if (arraylength == 1)
        return list[start];
    while (true) {

        int newPivotIndex = partition(list, start, end);
        int pivotDist = newPivotIndex - start + 1;
        if (pivotDist == kvalue)
            return list[newPivotIndex];
        else if (kvalue < pivotDist)
            end = newPivotIndex - 1;
        else {
            kvalue = kvalue - pivotDist;
            start = newPivotIndex - 1;
        }
    }

My Partition Algorithm :

    private static int partition(Numbers[] list, int start, int end) {
        Numbers[] tempMedianArray = new Numbers[5];
        tempMedianArray[0] = list[start];
        tempMedianArray[1] = list[(start + end) / 4];
        tempMedianArray[2] = list[(start + end) / 12];
        tempMedianArray[3] = list[(start + end) / 2];
        tempMedianArray[4] = list[end];
        Numbers pivot = getmedian(tempMedianArray);
           int i = start, j = end;

          while (i <= j) {
                while (compare(list[i], pivot).equals(Order.LESSER)){
                      i++;
                }
                while (compare(list[j], pivot).equals(Order.GREATER)){
                    j--;
                      }
                if (i <= j) {
                    Numbers tmp = list[i];
                      list[i] = list[j];
                      list[j] = tmp;
                }
          };

Once the pivot is chosen I thought that the standard Hoare's algorithm would work. But when I perform the dry run itself, I know I am wrong.

Can some one help me get the proper implementation of the partition algorithm which works with a median of five pivot?

preguntado el 24 de agosto de 12 a las 20:08

2 Respuestas

You have to keep on adding the start while getting the temp array index.

            int localStart = 0;
    int localEnd = end - start;
    Number[] local = new Number[5];
    tempMedianArray[0] = list[localStart + start];
    tempMedianArray[1] = list[(localStart + localEnd) / 4 + start];
    tempMedianArray[2] = list[(localStart + localEnd) / 4 * 3 + start];
    tempMedianArray[3] = list[(localStart + localEnd) / 2 + start];
    tempMedianArray[4] = list[localEnd + start];

Respondido 26 ago 12, 06:08

Qué tal si:

tempMedianArray[0] = list[start];
tempMedianArray[1] = list[(start + end) / 4];
tempMedianArray[3] = list[(start + end) / 2];
tempMedianArray[2] = list[3 * (start + end) / 4];
tempMedianArray[4] = list[end];

Respondido 25 ago 12, 00:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.