【问题标题】:Linked list insertion sort链表插入排序
【发布时间】:2011-04-23 12:20:47
【问题描述】:

我在编程的排序部分还不是很先进,所以我在寻找算法方面的帮助。

void sortList()
{
    Item_PTR tmpNxt = current->nextItem;
    Item_PTR tmpPTR = current;
    int a, tmp;

    while(tmpNxt != NULL)
    {   
        a = tmpPTR->value;
        while(tmpNxt != tmpPTR && tmpNxt->value < a)
        {
            tmp = a;
            tmpPTR->value = tmpNxt->value;
            tmpNxt->value = tmp;
            tmpPTR = tmpPTR->nextItem;
        }
        tmpPTR = current;   
        tmpNxt = tmpNxt->nextItem;
    }

}

排序前的列表状态:9 8 7 6 5 4 3 2 1 排序后:1 9 8 7 6 5 4 3 2

我不知道为什么...我在纸上玩了很多电脑,我觉得它应该可以工作...但也许其他人会发现问题。

Current 是一个全局指针,它始终具有列表中第一个/顶部元素的位置。

【问题讨论】:

  • 你的意思是“9 8 7 6 5 4 3 2 1”是排序前的列表状态,“1 9 8 7 6 5 4 3 2”是排序后的状态吗?
  • 是的^^;抱歉,我会在第一篇文章中说明。
  • 为什么不使用调试器逐步完成?
  • 使用 txt 编辑器和终端... :\
  • 使用 gdb:gnu.org/software/gdb 这是一个命令行调试器,如果你在 linux 上,它可能已经安装了。

标签: c sorting linked-list


【解决方案1】:

除了@Arun Saha 建议的更改之外,似乎还有一些逻辑错误(交换后没有更新值),这就是为什么即使在排序函数内部,列表操作系统也没有按排序顺序打印。下面的代码应该可以解决这个问题。

void sortList()
{
    Item_PTR tmpNxt = current->nextItem;
    Item_PTR tmpPTR = current;

    while(tmpNxt != NULL)
    {   
        while(tmpNxt != tmpPTR && tmpNxt->value < tmpPTR->value)
        {
            int tmp = tmpPTR->value;
            tmpPTR->value = tmpNxt->value;
            tmpNxt->value = tmp;
            tmpPTR = tmpPTR->nextItem;
        }
        tmpPTR = current;   
        tmpNxt = tmpNxt->nextItem;
    }

}

【讨论】:

    【解决方案2】:

    这是因为 sortList() 函数没有改变 current,即“全局” 表示列表头的变量。

    请不要使用全局变量,当然也不要使用链表头。 (当你需要两个列表时你会怎么做?)

    我会将sortList() 函数重新设计为以下任一功能:

    /* sort the list pointed to by phead and return the new head after sorting */
    Item_PTR sortList( Item_PTR phead );
    
    /* sort the list pointed to by *pphead */
    void sortList( Item_PTR * pphead );
    

    另外,让自己熟悉(即使你不能在当前项目中使用它们)列表的 C++ 标准库接口,std::listlink

    【讨论】:

    • 我不能使用这个库 ;) 因为它是一个特殊的项目。我正在使用 tmpPTR 来比较直到 tmpNxt 的所有数字。我对 current 所做的只是使用它将 tmpPTR 重置为列表顶部,以便它可以将所有内容与 tmpNxt 进行比较。
    • 是的,我想到了,所以我说让你自己熟悉:-)。更多建议: (1) 编写一个函数来打印列表的内容,比如printList()。 (2) 对三元素列表的所有组合使用 sortList [即{1 2 3}、{1 3 2}、{2 1 3} ..} (3) 在调用sortList() 之前和之后都调用printList()。重要提示:当列表中的第一个元素不是最小值时,其排序后的位置需要更改,但除非current 更新,否则该更改不会生效。
    【解决方案3】:
             **Java code for insertion sort of linked list**
    
    
    
    
    
        package LinkedList;
    
    /**
     * Created by dheeraj on 5/1/15.
     */
    public class InsertionSortLinkedList {
    
        private static final class Node {
            int value;
            Node next;
    
            public Node(int d) {
                value = d;
                next = null;
            }
        }
    
        private Node root;
        private Node sortedHead;
    
        private void addData(int data) {
            if (root == null) {
                root = new Node(data);
            } else {
                Node temp = new Node(data);
                temp.next = root;
                root = temp;
            }
        }
    
        private void printList() {
            Node temp = root;
            while (temp != null) {
                System.out.print(temp.value + " ");
                temp = temp.next;
            }
            System.out.println();
        }
    
        private void printSortedList() {
            Node temp = sortedHead;
            while (temp != null) {
                System.out.print(temp.value + " ");
                temp = temp.next;
            }
            System.out.println();
        }
    
        private void insertionSort() {
            Node outer = root;
            Node resultRoot = null;
            if (outer == null) {
                return;
            }
            while (outer != null) {
                if (resultRoot == null) {
                    //System.out.println("null");
                    resultRoot = new Node(outer.value);
                } else {
                    Node t = resultRoot;
                    if (outer.value < t.value) {
                        //current outer is smallest
                        //System.out.println("smallest");
                        Node temp = new Node(outer.value);
                        temp.next = t;
                        resultRoot = temp;
                    } else {
                        boolean broken = false;
                        while (t.next != null) {
                            if (t.value < outer.value && outer.value <= t.next.value) {
                                Node temp = new Node(outer.value);
                                temp.next = t.next;
                                t.next = temp;
                                broken = true;
                                //System.out.println("middle");
                                break;
                            }
                            //
                            t = t.next;
                        }
                        if (!broken) {
                            //current outer is greatest
                            //System.out.println("largest");
                            t.next = new Node(outer.value);
                        }
                    }
                }
                outer = outer.next;
            }
            sortedHead = resultRoot;
        }
    
        public static void main(String[] args) {
            InsertionSortLinkedList insertionSortLinkedList = new InsertionSortLinkedList();
            insertionSortLinkedList.addData(5);
            insertionSortLinkedList.addData(30);
            insertionSortLinkedList.addData(1);
            insertionSortLinkedList.addData(18);
            insertionSortLinkedList.addData(19);
    
            insertionSortLinkedList.printList();
            insertionSortLinkedList.insertionSort();
            insertionSortLinkedList.printSortedList();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      相关资源
      最近更新 更多