简体   繁体   中英

Quicksort java arraylist implementation

I'm struggling with a really simple problem in java. I've implemented quicksort in java that works on arraylists and can take any value. The problem is that it works only for an arraylist lower than about 8000 size. Can anyone tell me what's wrong with my program? I think it might be connected with recursion depth limit but i'm not sure (because sometimes it works for larger sizes and sometimes not). How can I improve my quicksort implementation so it will work for much larger size of Arraylist like 100000?

import java.util.ArrayList;
import java.util.Random;


public class QuickSort {
Random gener;
int temporary,genertype,NInts,flag;
ArrayList<Integer> mylist;

public QuickSort(int type,int ilosc){
    gener = new Random();
    mylist= new  ArrayList<>();
    this.genertype=type;
    this.NInts=ilosc;

}

void generate(){
    if(genertype==0){
        for(int i=0;i<NInts;i++){
            mylist.add(gener.nextInt(100000));
        }
    }else {
        for(int i=0;i<NInts;i++){
            mylist.add(NInts-i);
        }
    }
}

int count1(ArrayList<Integer> list,int counter1,int counter2){
    while(list.get(0)<list.get(counter1)){

        if(counter1==counter2){
            flag=1;
            return counter1;
        }
        counter1++;
    }
    flag=0;
    return counter1;
}
int count2(ArrayList<Integer> list,int counter1,int counter2){
    while(list.get(0)>list.get(counter2)){
        if(counter1==counter2){
            flag=1;
            return counter2;
        }
        counter2--;
    }
    flag=0;
    return counter2;
}


public ArrayList<Integer> sorting(ArrayList<Integer> list) {
    ArrayList<Integer> left = new ArrayList<Integer>();
    ArrayList<Integer> right = new ArrayList<Integer>();
    int counter1,counter2;

    if (list.size() == 1) {
        return list;
    }else {
        counter1=1;
        counter2=list.size()-1;

        while(counter1!=counter2) {

            counter1=count1(list,counter1,counter2);
            if(flag==1)
                break;
            counter2=count2(list,counter1,counter2);
            if(flag==1)
                break;

            temporary = list.get(counter1);
            list.set(counter1, list.get(counter2));
            list.set(counter2, temporary);
        }

        for (int i = 0; i < counter1; i++) {
            left.add(list.get(i));
        }

        for (int i = counter1; i < list.size(); i++) {
            right.add(list.get(i));
        }

        left = sorting(left);
        right = sorting(right);
        list=merge(left, right);
    }
    return list;
}

ArrayList<Integer> merge(ArrayList<Integer> left, ArrayList<Integer> right) {

    if(left.get(0)>right.get(right.size()-1)){
    right.addAll(left);
        return right;
    }
    else{
        left.addAll(right);
        return left;
    }

}

void printing(){
    for(int k=0;k<NInts;k++){
        System.out.print(" "+mylist.get(k));
    }
}

public static void main(String[] args){

    QuickSort instance = new QuickSort(1,1000);
    instance.generate();
    instance.mylist=instance.sorting(instance.mylist);
    instance.printing();
    }
}

Ps.If you see anything wrong in my code, let me know so I can improve it :)

There could be many reasons why your code could not run for large number of inputs. Mostly it could be because the Heap Size capacity specified for your application is overflown. This can be resolved by increasing Heap Size of your application (check out this stackoverflow link on how to increase the heap size of your application)

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