Guest User

Untitled

a guest
Jan 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. void list_ins_aft ( node_t *old ,node_t *new )
  2. {
  3.  
  4. if ( old->next != NULL )
  5. {
  6. old->next->prev = new;
  7. }
  8. new->next = old->next;
  9. new->prev = old ;
  10.  
  11. old->next = new ;
  12. }
  13.  
  14. void list_detach( node_t *n , dlist_t *nlst)
  15. {
  16.  
  17. if (n->prev == NULL)
  18. {
  19. nlst->head = n->next;
  20. }
  21. else
  22. {
  23. n->prev->next = n->next;
  24. }
  25.  
  26. if(n->next == NULL )
  27. {
  28. nlst->tail = n->prev;
  29. }
  30. else
  31. {
  32. n->next->prev = n->prev;
  33. }
  34. }
  35.  
  36.  
  37. void qsort_segment ( node_t *t1 , node_t *t2 , dlist_t *lst)
  38. {
  39.  
  40. /* skip 0 or 1 lengh segment */
  41. if ( t1 == t2 || t1->next == t2 )
  42. return;
  43. /*define pivot and make sure its the first element
  44. * so put the less before pivot and bigger after pivot
  45. * */
  46. node_t *piv;
  47. piv = t1->next;
  48. node_t *s,*b,*temp = piv , *x = t2 ? t2->next : NULL ;
  49.  
  50. for ( s = piv->next ; s != x ; s = b )
  51. {
  52.  
  53. b = s->next ;
  54.  
  55. list_detach ( s ,lst ) ;
  56.  
  57. if ( s->value < piv->value )
  58. {
  59. list_ins_aft(t1 , s );
  60. }
  61. else
  62. {
  63. list_ins_aft ( piv , temp == piv ? ( temp = s ) : s );
  64. }
  65. }
  66.  
  67. /* now sort new segments on right and left sides of pivot the same way */
  68. qsort_segment ( piv , temp ,lst );
  69. qsort_segment ( t1 , piv->prev , lst);
  70.  
  71. }
  72.  
  73. /*define pivot and make sure its the first element
  74. * so put the less before pivot and bigger after pivot
  75. * */
  76. node_t *piv;
  77. piv = t1->next;
  78.  
  79. if ( s->value < piv->value )
  80. {
  81. list_ins_aft(t1 , s );
  82. }
Add Comment
Please, Sign In to add comment