简体   繁体   中英

Getting wrong output for quicksort algorithm

I'm trying to implement quicksort and the output isn't correct. I know ArrayList s are referenced by value, yet I don't understand why it's not giving me the correct output. Can anyone tell me what I'm doing wrong?

import java.util.ArrayList;

public class Quicksort {

static ArrayList<Integer> quicksort(ArrayList<Integer> place){
    if(place.size()<=1)
        return place;
    int median=place.size()/2;

    int pivot=place.get(median);
    place.remove(median);

    ArrayList<Integer> place2=new ArrayList<>();
    ArrayList<Integer> place3=new ArrayList<>();

    for(int i=0;i<place.size();i++){
        if(place.get(i)<pivot){
            place2.add(place.get(i));
        }else if(place.get(i)>pivot){
            place3.add(place.get(i));
        }else if(place.get(i)==pivot){
            place3.add(place.get(i));
        }
    }
    return concatenate(quicksort(place2),pivot,quicksort(place3));
}

static ArrayList<Integer> concatenate( ArrayList<Integer> first, int pivot, ArrayList<Integer> second){
    ArrayList<Integer> third=new ArrayList<>();

    for(int i=0;i<first.size();i++){
        third.add(first.get(i));
    }

    third.add(pivot);
    for(int i=0;i<second.size();i++){
        third.add(second.get(i));
    }

    return third;
}

public static void main(String[] args) {

ArrayList<Integer> arraylist=new ArrayList<>();

arraylist.add(20);
arraylist.add(3);
arraylist.add(8);
arraylist.add(4);
arraylist.add(7);
arraylist.add(1);
arraylist.add(9);
arraylist.add(13);

    quicksort(arraylist);
    for(int n:arraylist){
        System.out.println(n);
    }
}
}

Your quicksort method returns the sorted array but you don't assign it. Change it to

arraylist = quicksort(arraylist);

and it works just fine.


It is true that you can modify the arraylist in your quicksort method, so there would be no need to assign it, but that's not what you're doing. All you do is delete the pivot and then return a new list created by concatenate . That's why in your algorithm you get the original list with 7 missing.

If you don't want to assign the list again, you need to do something like:

private static void quicksort(List<Integer> place) {
    if (place.size() <= 1)
        return;

    int median=place.size()/2;
    int pivot=place.get(median);
    place.remove(median);

    List<Integer> place2 = new ArrayList<>();
    List<Integer> place3 = new ArrayList<>();

    Iterator<Integer> it = place.iterator();
    while (it.hasNext()) {
        int i = it.next();
        if (i < pivot) {
            place2.add(i);
        } else {
            place3.add(i);
        }
        it.remove();
    }

    quicksort(place2);
    quicksort(place3);

    for (Integer i : place2) {
        place.add(i);
    }
    place.add(pivot);
    for (Integer i : place3) {
        place.add(i);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM