简体   繁体   中英

Merge Sort on Linked List giving me stack overflow

Thanks for reading.I basically have a singly linked list with generic elements(ended up passing in Strings)...I attempted to sort them with merge sort but I am receiving stack overflow error for some reason. It seems like my recursions goes on endlessly but I do have a condition to stop it...Here is what I have, the linkedlist class is just generic linkedlist class with insertfirst and insertlast. `public class app {

public static void main(String[] args) {
    sLinkedList newList = new sLinkedList();
    newList.insertLast("APPLE");
    newList.insertLast("BPPLE");
    newList.insertLast("DPPLE");
    newList.insertLast("CPPLE");
    newList.insertLast("GPPLE");

    newList.insertLast("FPPLE");
    newList.insertLast("ZPPLE");
    newList.insertLast("RPPLE");
    sort(newList);
    newList.displayList();
}
public static void sort(sLinkedList list) {
    sort(0,list.length-1,list);

}

public static void sort(int beginning, int end,sLinkedList list) {
    if(end <= beginning) {
        return;
    }

    int mid = (beginning+end)/2;
    sort(beginning,mid,list);
    sort(mid+1,end,list);
    merge(beginning,mid,end,list);
}

public static void merge(int start, int mid, int end,sLinkedList list) {
    sLinkedList tempList = new sLinkedList();
    int left = start;
    int right = mid+1;
    int k = 0;

    while(left<mid && right<end) {
        if(list.get(left).compareTo(list.get(right))==-1) {
            tempList.insertLast(list.get(left));
            left = left+1;

        }else {
            tempList.insertLast(list.get(right));

            right++;                    

        }
        k++;
    }
    if(left<=mid) {
        while(left<=mid) {
            tempList.insertLast(list.get(left));
            left++;
            k++;

        }


    }else if(right<=end) {
        while(right<=end) {
            tempList.insertLast(list.get(right));
            right++;
            k++;                    

        }


    }
    sllNode current = list.first;
    for(int i=0;i<start;i++) {
        current = current.next;
    }
    for(int i=0;i<tempList.length;i++) {        
        current.data = tempList.first.data;
        current = current.next;
        tempList.first = tempList.first.next;
    }
}

}`

Again, thank you very much. I am trying to get the list to be in alphabetical order so basically Apple - Bpple - Cpple - Dpple - ......etc

Error occurs at line 30

I updated your code and modified some of it. it might fix your problem. I once worked on a problem like that but it was on c++ it might still work with a java code... let me know if it worked and fixed your problem.

public static void merge (int start, int mid, int end, sLinkedList list) {

sLinkedList tempList = new sLinkedList();
int left = start;    
int right = mid+1; 
int k = start;

while((left<=mid) && (right<=end)) 
{
    if(list.get(left) < (list.get(right)) // compare your if the item on the left is less than the one on the right
    {
        tempList.insertLast(list.get(left));
        left = left+1;
        }
        else {
        tempList.insertLast(list.get(right));

        right++;    

    }
    k++;
}

 for ( int i = left; i <= mid; i++ )
        {
        tempList.insertLast(list.get(i));
            k++;        //moves remaining right list value to temp list
        }

for ( int i = right; i <= end; i++ )
        {
         tempList.insertLast(list.get(i));
            k++;    //moves sorted temp list back to original list
        }

for ( int i = start; i <= end; i++ )

        tempList.insertLast(list.get(i));
        delete tempList;

}

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