Clasificación rápida en Java

I'm supposed to do a quicksort algorithm in java to sort the array {50, 20, 65, 30, 75, 25, 90}. Here is what I have so far:

public class QuickSort {
public static int partition(int arrayName[], int down, int up){
    int i = down, j = up;
    int temp;
    int pivot = arrayName[(down + up) / 2];

    while (i <= j){
        while (arrayName[i] < pivot)
        while (arrayName[j] > pivot)
        if (i <= j){
            temp = arrayName[i];
            arrayName[i] = arrayName[j];
            arrayName[j] = temp;

    return i;

public static void main(String[] args) {
    int [] arrayName = {50, 20, 65, 30, 75, 25, 90};

    System.out.println(partition(arrayName, down, up)); 


I'm getting an error on the print statement (seem to have a lot of trouble with these) that says down and up cannot be resolved to variables. How can I fix it so I can successfully print the sorted list?

preguntado el 01 de febrero de 12 a las 22:02

Cual es el error? -

you're getting an error on that line because the variables "down" and "up" are not defined in the static scope of the class. They are defined only within the "partition" method. You'll need to pass actual values into the method, or define local variables in the "main" method called "down" and "up". -

El muy primero your fingers should do after typing "I get an error" or "I get an exception" is to start typing the exacto error message you're getting, along with any other information like line numbers or memory addresses. If you don't do so, it makes it much harder for others to spot the error in your code, and making it harder for us makes it slower for you to get your answer. Please remember we can't see your screen or read your mind from here; it's much easier if you provide us with the information. :) -

You're right. my bad. Wasn't really thinking. I edited my OP to fix this. -

3 Respuestas

It's because you haven't defined any variables named down y la up en tu main method. You should specify values instead of those names.

Respondido 02 Feb 12, 02:02

Thank you, it's working now. How would I go about printing out the whole sorted array instead of just the i? - Bryan

No probs. To print your array, check out Arrays.toString (int[]) - hablar

I put: System.out.println(Arrays.toString(partition(arrayName, down, up))); but I get an error that says: "The method toString(long[]) in the type Arrays is not applicable for the arguments (int)" - Bryan

Trata System.out.println (Arrays.toString(arrayName)); ; o) - hablar

If this solutions works for you, you should mark it as the 'best answer'. Not only is it the nice thing to do, but it also means that other users are more likely to help you if they know you will give them upvotes in return. - Aurora

Tus partition el método devuelve un int. Instead, change your method body so that it returns the newly sorted array (and make sure you change the return type in the method declaration too, else you'll get an error). Furthermore, you need to define up y la down en el objeto main método.

Por ejemplo:

public static int[] partition(...)
return arrayname;

Edit: furthermore, you may need to use Arrays.toString() to output the array correctly (it's been a while since I used Java). eg:

System.out.println(Arrays.toString(partition(arrayName, up, down)));

Respondido 02 Feb 12, 02:02

I don't think this solves the problem. Even though the method returns an int, it performs sorting on the reference of the array, so he should be able to print the array after partition is called. Although the implementation of partition is incorrect. - triángulo

Thank you for the print statement, however I am getting an error when trying to use it. It underlines the toString part and says: The method toString(long[]) in the type Arrays is not applicable for the arguments (int) - Bryan

Ahh, I see. That print statement would have been based on the changes to your partition method I mentioned earlier. To get around this, simply remove the method call from inside the .toString method, and instead just put the instance of arrayName. System.out.println(Arrays.toString(arrayName)); The reason it's throwing an error is because your partition method returns an int, where Arrays.toString requires an array. Hope this helps. - TeaPow

you are getting index out of bound ,

porque up y la down are non initialized and in java it makes them 0

so down goes at j :

while(arrayName[j]<pivot){ //<--- this will thow exception as j starts at 0

which leads to -1 and accessing the array at arrayName[-1] is out of bound.

Respondido 02 Feb 12, 03:02

This isn't necessary in Java; the language and libraries will handle this for you. - templatetypedef

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