Advertisement
Fructus

Untitled

Apr 8th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. int Length(list* h)
  2. {
  3.     int count = 0;
  4.     list* current = h;
  5.     while (current != NULL)
  6.     {
  7.         count++;
  8.         current = current->next;
  9.     }
  10.     return(count);
  11. }
  12.  
  13.  
  14.  
  15. void FrontBackSplit(list* source, list** frontRef, list** backRef)
  16. {
  17.  
  18.     int len = Length(source);
  19.     int i;
  20.     list* current = source;
  21.     if (len < 2)
  22.     {
  23.         *frontRef = source;
  24.         *backRef = NULL;
  25.     }
  26.     else
  27.     {
  28.         int hopCount = (len-1)/2;
  29.         for (i = 0; i<hopCount; i++)
  30.         {
  31.             current = current->next;
  32.         }
  33.         // исходный список разбивается на два подсписка
  34.         *frontRef = source;
  35.         *backRef = current->next;
  36.         current->next = NULL;
  37.     }
  38. }
  39.  
  40. list* SortedMerge_Name(list* a, list* b)
  41. {
  42.     list* result = NULL;
  43.     if (a==NULL) return(b);
  44.     else if (b==NULL) return(a);
  45.     if (strcmp(a->info.name,b->info.name)<0)
  46.     {
  47.         result = a;
  48.         result->next = SortedMerge_Name(a->next, b);
  49.     }
  50.     else
  51.     {
  52.         result = b;
  53.         result->next = SortedMerge_Name(a, b->next);
  54.     }
  55.     return(result);
  56. }
  57.  
  58. void MergeSort_Name(list** hRef)
  59. {
  60.     list* h = *hRef;
  61.     list* a;
  62.     list* b;
  63.     // вырожденный случай – длина списка равно 0 или 1
  64.     if ((h == NULL) || (h->next == NULL))
  65.     {
  66.         return;
  67.     }
  68.     FrontBackSplit(h, &a, &b);
  69.     MergeSort_Name(&a); // рекурсивная сортировка подсписков
  70.     MergeSort_Name(&b);
  71.     *hRef  = SortedMerge_Name(a, b);
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement