簡體   English   中英

使用氣泡排序對鏈表進行排序

[英]Linked List Sorting Using Bubble Sort

我正在嘗試使用冒泡排序對鏈接列表進行排序。但是我寫的算法不起作用。有人可以幫助我嗎?

還有一個鏈接類。

連結類別

public class Link {
    public int iData;
    public String sData;
    public Link next;

    public Link(int id,String sd)
    {
    iData =id;
    sData =sd;
    next = null;
    }
    public void displayLink()
    {
    System.out.println(iData+""+sData); 
    }
    }

LinkKist類包括排序算法。

public class LinkedList {

    private Link first;

    public void LinkList() {

        first = null;

    }

  public void insertFirst(int idata, String sdata) {
        Link nl1 = new Link(idata, sdata);

        nl1.next = first;
        first = nl1;
    }

public void displayList() {
    System.out.println("List : ");
    Link current = first;
    while (current != null) {
        current.displayLink();
        current = current.next;
    }
    System.out.println("");
}

    public void sortll(){


        Link current = first;
        Link nextLink = first.next;

        while(current.next != null){

            while(nextLink.next != null)

            if(nextLink.iData < current.iData){

                Link temp = nextLink;
                nextLink = current;
                current = temp;

                nextLink = nextLink.next;
                current = current.next;

            }

        current = current.next;
        }


    }
}

測試應用程序。

public class LLtest {

    public static void main(String[] args) {

        LinkedList ll1 = new LinkedList();

        ll1.insertFirst(11, "UWU0011");
        ll1.insertFirst(3, "UWU0003");
        ll1.insertFirst(1, "UWU0001");
        ll1.insertFirst(4, "UWU0004");
        ll1.insertFirst(5, "UWU0005");
        ll1.insertFirst(6, "UWU0006");
        ll1.insertFirst(7, "UWU0007");
        ll1.insertFirst(10, "UWU0010");
        ll1.insertFirst(9, "UWU0009");
        ll1.insertFirst(2, "UWU0002");
        ll1.insertFirst(8, "UWU0008");



        ll1.sortll();


        ll1.displayList();
    }
}

誰能幫幫我嗎?????

您的排序算法有問題,我感覺您沒有在進行冒泡排序。我添加了sortll()方法,該方法交換鏈接列表數據,但不交換鏈接。 我認為您正在嘗試交換鏈接,如果這樣做,則在交換鏈接時需要更加小心,因為您需要仔細考慮最終情況。 替換sortll()方法並檢查它

public void sortll(){


    Link current = first;
     System.out.println(first.iData);
    Link nextLink = first.next;
    /*while(current.next != null){

        while(nextLink.next != null)

        if(nextLink.iData < current.iData){

            Link temp = nextLink;
            nextLink = current;
            current = temp;

            nextLink = nextLink.next;
            current = current.next;

        }

    current = current.next;
    }*/
    int length=0;
    while(current!=null)
    {
        length++;
        current=current.next;
    }
    System.out.println(length);

    for(int i=0;i<length;i++)
    {
         Link temp=first;
        for(int j=0;j<length-i-1;j++)
        {
            if(temp.iData>temp.next.iData)
            {
                int tempiData = temp.iData;
                String tempsData =temp.sData;
                temp.iData =temp.next.iData;
                temp.sData =temp.next.sData;
                temp.next.iData=tempiData;
                temp.next.sData=tempsData;
            }
            temp=temp.next;
        }
    }


}
}

這適用於上述問題。

public class LinkedList {

        private Link first;

        public void LinkList() {

            first = null;

        }

    public void sortingLinkList(){   //working 

        boolean flag = true;
        while (flag) {
            flag = false;

            Link position = first;
            Link positionNext = position.next;
            Link positionPrev = null;

            while (positionNext != null) {
                if(position.iData > positionNext.iData) {

                    Link temp = position;
                    Link tempNextNext = positionNext.next;
                    position = positionNext;
                    position.next = temp;
                    positionNext = temp;
                    positionNext.next = tempNextNext;

                    if (positionPrev == null) { // position is head
                        first = position;
                    } else {
                        positionPrev.next = position;
                    }

                    flag = true;
                }
                positionPrev = position;
                position = position.next;
                positionNext = position.next;
            }
        }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM