kevin-wayne/algs4

QuickX.sort(Comparable[] a, int lo, int hi)

Closed this issue · 8 comments

Hey,

Could it be possible to make QuickX.sort(Comparable[] a, int lo, int hi) public instead of having it private?

Just by looking at the code I don't see any reason why it couldn't be public, but perhaps I am missing something here?

-niklas

@nikkekin
Private method means it cannot be accessed by any other class. If you were to have a class that wanted to use a private method in another class, it would not be allowed. You would have to make that method public, or protected if the class that wants to access the data is a subclass.
I think you are missing OOP concepts so please make sure clear.
It will give you more idea about it.

               | Class | Package | Subclass    | Subclass  | World
               |            |              | (same pkg) | (diff pkg)| 

public        |   +   |    +    |    +     |     +    |   +     

protected     |   +   |    +    |    +     |     +    |   o     

no modifier   |   +   |    +    |    +     |     o    |   o

private       |   +   |    o    |    o     |     o    |   o

+  :  accessible
o  :  not accessible

Still want to more Official java

@nikkekin I think you got your answer so please close the issue.
If you are still not get the you can ping me. :)

Hey,

The question was motivated by the fact that the particular method can be used from my point of view just fine outside the class, and it would add more possibilities to use the class.

Sometimes it can be very useful to only sort a certain subset of an array :)

-niklas

@nikkekin
It depends on the scope. Would you want to use outside of the class then only you should use public otherwise does not make sense even you can use it.
You are taking about this.Am I right?

private static void sort(Comparable[] a, int lo, int hi) { 
        int n = hi - lo + 1;

        // cutoff to insertion sort
        if (n <= INSERTION_SORT_CUTOFF) {
            insertionSort(a, lo, hi);
            return;
        }

        // use median-of-3 as partitioning element
        else if (n <= MEDIAN_OF_3_CUTOFF) {
            int m = median3(a, lo, lo + n/2, hi);
            exch(a, m, lo);
        }

        // use Tukey ninther as partitioning element
        else  {
            int eps = n/8;
            int mid = lo + n/2;
            int m1 = median3(a, lo, lo + eps, lo + eps + eps);
            int m2 = median3(a, mid - eps, mid, mid + eps);
            int m3 = median3(a, hi - eps - eps, hi - eps, hi); 
            int ninther = median3(a, m1, m2, m3);
            exch(a, ninther, lo);
        }

        // Bentley-McIlroy 3-way partitioning
        int i = lo, j = hi+1;
        int p = lo, q = hi+1;
        Comparable v = a[lo];
        while (true) {
            while (less(a[++i], v))
                if (i == hi) break;
            while (less(v, a[--j]))
                if (j == lo) break;

            // pointers cross
            if (i == j && eq(a[i], v))
                exch(a, ++p, i);
            if (i >= j) break;

            exch(a, i, j);
            if (eq(a[i], v)) exch(a, ++p, i);
            if (eq(a[j], v)) exch(a, --q, j);
        }


        i = j + 1;
        for (int k = lo; k <= p; k++)
            exch(a, k, j--);
        for (int k = hi; k >= q; k--)
            exch(a, k, i++);

        sort(a, lo, j);
        sort(a, i, hi);
    }

As I wrote, I was just asking if it would be possible to add signature

QuickX.sort(Comparable[] a, int lo, int hi)

To the public api of the class. It is already a static function and based on a short read of the source none class invariants are broken in case you make it public.

I can also just fork your codebase (already did it) but at the same time it would be a lot nicer for future maintenance in case it was just part of the api.

I understand if you don't want to support it.

-niklas

Your algorithm is well implemented and I don't really see anything wrong with it.

Also, compared to the standard Java implementation, yours doesn't allocate but sorts in place.

I just think you've done a good job, and I have a use case for it.

Do as you want of course :)

-niklas

This issue may be closed now.