Frage

I have to implement four sorting algorithms(Insertion, Selection, Shell, Quicksort) with doubly linked list as a homework, but I'm completely lost because all the explanations of those sorting algorithms I found online require the use of arrays. I tried to use this code as a pseudo index for my DLL:

public DoubleNode this[int num]
    {
        get
        {
            DoubleNode x = head;
            for(int k = 0; k < num; k++)
                x = x.Next;

            return x;
        }
    }

But it's not enough, cause it's not a setter. Any ideas guys/girls?

War es hilfreich?

Lösung

OK (based on my comment) one example is insert - see: http://en.wikipedia.org/wiki/Insertion_sort#List_insertion_sort_code_in_C.2B.2B - insert sort is actually a bit more suitable to list than to arrays (because the insert operation in an array means moving elements).

The same goes to quicksort http://en.wikipedia.org/wiki/Quicksort#Simple_version

Selection sort is trivial to implement over a list - you use two pointers, one (I'll call that the head) pointing to the next unsorted position (starting from the start of the list and moving one step ahead each time) and the other searching for a minimal element from the head to the end of the list.

Shell sort is based on insertion sort, and shouldn't be too hard to implement based on that idea.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top