Thread: sort linked list using quick sort in C

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    sort linked list using quick sort in C

    hi I am trying to sort a linked list using quick sort in C. Here is my code--Actually, first I am inserting data in the list from a file. For a small file, it's working fine. But for large file it's just not working. Please help me out with this. What I need to add??Thanks

    Code:
    struct node
    {
        int data;
        struct node *link;
        struct node *plink;
    };
    struct node *first=NULL, *last=NULL;
    void swap(struct node* a,struct node* b)
    {
        int temp;
        temp=a->data;
        a->data=b->data;
        b->data=temp;
    }
    
    void qsort(struct node *low, struct node *high)
    {
        if(low==NULL || high==NULL || low == high || low->plink == high)
        return ;
    
        struct node *pivot=low, *tmp=low->link;
        while(tmp != high->link)
        {
                if(tmp->data < low->data)
                {
                        swap(pivot->link, tmp);
                        pivot = pivot->link;
                }
                tmp = tmp->link;
        }
        swap(low, pivot);
        qsort(low, pivot->plink);
        qsort(pivot->link, high);
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    You should avoid calling your function qsort as there is a standard C function with that name. Try quicksort instead.

    Also, posting enough code for us to compile, run and test your algorithm, and the large input file for which it fails, would be very beneficial. I really don't want to write a main function and linked list insert function for your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick sort using linked list
    By TehBlue in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2013, 07:25 PM
  2. Replies: 11
    Last Post: 02-20-2012, 10:38 PM
  3. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  4. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM

Tags for this Thread