Thread: Insertion Sort and Linked List

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    91

    Insertion Sort and Linked List

    Hi, I'm having a hard time getting through this and was wondering if you guys can guide me or lead me in the right direction.

    So we have a insertion sort code, something like, a variation

    Code:
    void stackSort (int n, double * data) {
    	double x;
    	struct dyArray stk;
    	int i = 0;
    	
    	dyArrayInit(&stk, 3);
    	
    	while (i < n) {
    		x = data[i++];
    		while ((!dyArrayIsEmpty(&stk)) && dyArrayTop(&stk) > x) {
    			data[--i] = dyArrayTop(&stk);
    			dyArrayPop(&stk);
    		}
    		dyArrayPush(&stk, x);
    	}
    	
    	/* now everything should be on the stack in reverse order */
    	while (i > 0) {
    		data[--i] = dyArrayTop(&stk);
    		dyArrayPop(&stk);
    	}
    }
    or something like pseudocode

    Code:
    insertionSort(array A)
    begin
        for i := 1 to length[A]-1 do
        begin
            value := A[i];
            j := i - 1;
            while A[j] > value do
            begin
                A[j + 1] := A[j];
                A[j] := value;
                j := j - 1;
                if j < 0 then exit while;
            end;
        end;
    end;
    How would I get that to work with a linked list?

    Code:
    struct link {
    	EleType value;
    	
    	struct link * next;
    	struct link * prev;
    };
    
    struct linkedList {
    	int size;
    	
    	struct link * frontSentinel;
    	struct link * backSentinel;
    };
    Also I have methods written out (assuming they work), that add to the front and to the back and remove from the front and the back. that free space up, etc.
    Just kinda confused. Thanks
    Last edited by dlwlsdn; 01-25-2010 at 08:17 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for n = list; n&& n->value < insert->value; n = n->next
        ...do nothing but loop...
    if n == null || n->prev == NULL
        insert->next = root
        root = insert
    else
        n->prev->next = insert
        insert->next = n
    That looks about right.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Quote Originally Posted by quzah View Post
    Code:
    for n = list; n&& n->value < insert->value; n = n->next
        ...do nothing but loop...
    if n == null || n->prev == NULL
        insert->next = root
        root = insert
    else
        n->prev->next = insert
        insert->next = n
    That looks about right.


    Quzah.
    Ahh, thank you. I wrote something similar, except with two while loops.

    Code:
    //make a random temp variable and a couple new links
    newLink = list->front->next->next
    while(newLink != list->back) {
          while((newLink->value < newLink->prev->value) {
                 tempValue = newLink->value;
                 tempLink = newLink->prev;
                 removeLink(list, newLink);
                 addLink(list, tempLink, tempValue);
           }
          newLink = newLink->next;
    }
    something like that, not word for word, but yeah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Linked List Insertion Sort
    By The Brain in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2005, 07:20 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM