Advertisement
Johnthomas

quicksort on a linked list in C w/ random pivot

Feb 20th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | None | 0 0
  1. #include <stdlib.h>     //rand, malloc
  2. #include <stdio.h>      //print
  3. #include <time.h>
  4.  
  5.  
  6. struct listnode {
  7.  
  8.     struct listnode *next;
  9.     long value;
  10. };
  11.  
  12. //Finds length of list, which is usefull in selecting a random pivot
  13. int ListLength (struct listnode *list)
  14. {
  15.     struct listnode *temp = list;
  16.    
  17.     int i=0;
  18.     while(temp!=NULL)
  19.     {
  20.         i++;
  21.         temp=temp->next;
  22.  
  23.     }
  24.     return i;
  25. }
  26.  
  27. // Prints list to help while working on homework
  28. void printList(struct listnode *list)
  29. {  
  30.     struct listnode *node;
  31.     node=list;
  32.     printf("\nList Values\n");
  33.     while(node!=NULL)
  34.     {
  35.         printf("%lo ", node->value);
  36.         node=node->next;
  37.     }
  38. }
  39. // Creates a list of desired length with some jumbled non random numbers
  40. struct listnode *createList(int lengthOfList)
  41. {
  42.     long i;
  43.     struct listnode *node, *space;
  44.     space =  (struct listnode *) malloc( lengthOfList*sizeof(struct listnode));
  45.     for( i=0; i< lengthOfList; i++ )
  46.     {  (space + i)->value = 2*((17*i)%lengthOfList);
  47.        (space + i)->next = space + (i+1);
  48.     }
  49.  
  50.     (space+(lengthOfList-1))->next = NULL;
  51.     node = space;
  52.  
  53.     return node;
  54. }
  55.  
  56. // Prof Brass's test
  57. void Brass_test(struct listnode *list)
  58. {
  59.     int i;
  60.     printf("\nChecking sorted list\n");
  61.     for( i=0; i < 1000000; i++)
  62.     {  
  63.         if( list == NULL )
  64.         {
  65.             printf("List ended early\n"); exit(0);
  66.         }
  67.         if( list->value != 2*i )
  68.         {  
  69.             printf("Node contains wrong value\n"); exit(0);
  70.         }
  71.         list = list->next;
  72.    }
  73.    printf("Sort successful\n");
  74. }
  75.  
  76. // Selects a random pivot point
  77. struct listnode *SelectPivot(struct listnode *list)
  78. {
  79.  
  80.     int k, n, i = 0;
  81.     n = ListLength(list);
  82.  
  83.  
  84.     struct listnode *pivot=list;
  85.  
  86.     k=rand()%n;
  87.  
  88.     for (; i < k; ++i)
  89.     {
  90.         pivot=pivot->next;
  91.     }
  92.  
  93.     return pivot;
  94. }
  95.  
  96. // Sorts a list using quicksort algo with random pivot point
  97. struct listnode *Quicksort(struct listnode *list)
  98. {
  99.     // Return NULL list
  100.     if (ListLength(list) <= 1) return list;
  101.     struct listnode *less=NULL, *more=NULL, *next, *end, *temp=NULL;
  102.    
  103.     // Select a random pivot point
  104.     struct listnode *pivot = SelectPivot(list);
  105.  
  106.     // Remove pivot from list
  107.     while(list !=NULL)
  108.     {
  109.         next = list->next;
  110.  
  111.         if(list->value != pivot->value)
  112.         {
  113.             list->next=temp;
  114.             temp = list;
  115.         }
  116.         list = next;
  117.     }
  118.  
  119.     // Divide & Conq
  120.     while(temp != NULL)
  121.     {
  122.        
  123.         next = temp->next;
  124.  
  125.         if(temp->value < pivot->value)
  126.         {
  127.             temp->next = less;
  128.             less = temp;
  129.         }
  130.         else
  131.         {
  132.             temp->next = more;
  133.             more = temp;
  134.        
  135.         }
  136.         temp = next;
  137.     }
  138.  
  139.     // Recursive Calls
  140.     less = Quicksort(less);
  141.     more = Quicksort(more);
  142.  
  143.     // Merge
  144.     if(less != NULL)
  145.     {
  146.         end = less;
  147.         while(end->next != NULL)
  148.         {
  149.             end=end->next;
  150.            
  151.         }
  152.         pivot->next=more;
  153.         end->next = pivot;
  154.        
  155.         return less;       
  156.     }
  157.     else
  158.     {
  159.        
  160.         pivot->next = more;
  161.  
  162.         return pivot;  
  163.     }
  164.  
  165. }
  166.  
  167. int main(void)
  168. {
  169.     // To seend rand()
  170.     srand(time(0));
  171.     struct listnode *node;
  172.  
  173.     node = createList(1000000);
  174.  
  175.  
  176.     node = Quicksort(node);
  177.  
  178.     Brass_test(node);
  179.  
  180.  
  181.  
  182.  
  183.     exit(0);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement