Ladies_Man

#SORT__3_8 Insertion LISTsort (сорт списка вставками)

Dec 30th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5. int n, comp_sign, extra = INT_MAX;
  6.  
  7. struct Elem {
  8.   struct Elem *prev, *next;
  9.   int v;
  10. };
  11.  
  12. struct Elem *x, *t1, *el, *z, *dy, *element, *current, *dz, *l, *check, *start, *finish, *res, *elem, *temp1, *temp2, *print;
  13.  
  14. struct Elem* initlist()
  15. {
  16.   struct Elem *first=(struct Elem*)malloc(sizeof(struct Elem));
  17.   first->prev=first;
  18.   first->next=first;
  19.   return first;
  20. }
  21.  
  22. struct Elem* listsearch (struct Elem *ls, int v)
  23. {
  24.     x = ls->next;
  25.     while (x != ls && x->v != v)
  26.         x = x->next;
  27.     return x;
  28. }
  29.  
  30. struct Elem* listlength (struct Elem *ls) {
  31.     int len = 0;
  32.     x = ls;
  33.     while (x->next != ls) {
  34.         len++;
  35.         x = x->next;
  36.     }
  37.     if (len != n) printf("loss\n");
  38.     return len;
  39. }
  40.  
  41. void Insert(struct Elem *a, struct Elem *b){
  42.   b->next=a->next;
  43.   b->prev=a;
  44.   a->next=b;
  45.   b->next->prev=b;
  46. }
  47.  
  48. int compare (struct Elem *x, struct Elem *y)
  49. {
  50.     if (x->v < y->v) {
  51.         //comp_sign = 1;
  52.         return 1;
  53.     }
  54.     else return 0;
  55.     //comp_sign = 0;
  56.     //return 0;
  57. }
  58.  
  59. void swap(struct Elem *i,struct Elem *j)
  60. {
  61.   i->prev->next=i->next;
  62.   i->next->prev=i->prev;
  63.   i->next=j->next;
  64.   i->prev=j;
  65.   j->next=i;
  66.   i->next->prev=i;
  67. }
  68.  
  69. void insertsort(struct Elem *element)
  70. {
  71.   struct Elem *start;
  72.   struct Elem *current;
  73.   start=element->next->next;
  74.   while(start!=element){
  75.     current=start->prev;
  76.     while(current!=element && compare(start,current)==1) current=current->prev;
  77.     swap(start,current);
  78.     start=start->next;
  79.   }
  80.   start=start->next;
  81.  
  82. }
  83. /*
  84. void check_cond (void)
  85. {
  86.     int p = 0;
  87.     if (current->next->v < current->next->next->v) {
  88.         printf("%d ", current->next->v);
  89.         goto exitcond;
  90.     } else {
  91.         if (current->next->v > current->next->next->v) {
  92.             printf("%d ", current->next->next->v);
  93.             p = 1;
  94.             goto exitcond2;
  95.         } else {
  96.             p = 0;
  97.             goto exitcond2;
  98.         }
  99.     }
  100.     exitcond:;
  101.     printf("%d", t1->next->next->v);
  102.     return;
  103.     exitcond2:;
  104.     printf ("%d", t1->next->v);
  105.     if (1 == p) return;
  106.     printf(" %d", t1->next->next->v);
  107. }*/
  108.  
  109. void mem_salvation (int marker)
  110. {
  111.     int arr[marker];
  112.     int i;
  113.     if (1 == marker) {
  114.         free (current->next);
  115.         free (current);
  116.         return;
  117.     }
  118.     if (2 == marker) {
  119.         free (t1->next->next);
  120.         free (t1->next);
  121.         free (t1);
  122.         return;
  123.     }
  124.     if (2 < marker) {
  125.         for (i = 0; i < marker; i++) arr[i] = t1->next;
  126.         for (i = 0; i < marker; i++) free(arr[i]);
  127.         free (t1);
  128.     }
  129. }
  130.  
  131.  
  132. int main()
  133. {
  134.   int i, x;
  135.   scanf("%d", &n);
  136.   current = initlist();
  137.  
  138.   for(i = 0; i < n; i++){
  139.         element = (struct Elem*)malloc(sizeof(struct Elem));
  140.         scanf("%d", &x);
  141.         element->v = x;
  142.         Insert (current, element);
  143.     }
  144.  
  145.     switch (n){
  146.     case 0: return 0;
  147.     case 1: {
  148.         printf("%d",current->next->v);
  149.         mem_salvation (n);
  150.         return 0;
  151.     }
  152.     case 2: {
  153.       if (current->next->v < current->next->next->v) {
  154.             printf("%d ", current->next->v);
  155.             printf("%d", current->next->next->v);
  156.         } else {
  157.             printf("%d ", current->next->next->v);
  158.              printf("%d ", current->next->v);
  159.         }
  160.  
  161.       free(current->next->next);
  162.       free(current->next);
  163.       free(current);
  164.  
  165.       return 0;
  166.     }
  167.     default: {
  168.       insertsort(current);
  169.       for(i = 0, print = current->next; i < n; i++, print = print->next)
  170.                 printf("%d ",print->v);
  171.  
  172.       int f[n];
  173.  
  174.       for(i=0;i<n;i++) {
  175.             f[i]=current;
  176.             current=current->next;
  177.       }
  178.  
  179.       for(i=0;i<n;i++) free(f[i]);
  180.  
  181.       free(current);
  182.       return 0;
  183.     }
  184.   }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment