Advertisement
filashkov

Untitled

Nov 22nd, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. // T06-16.cpp : ���� ���� �������� ������� "main". ����� ���������� � ������������� ���������� ���������.
  2. //
  3.  
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <stdbool.h>
  7. #include <stdlib.h>
  8. #include <algorithm>
  9.  
  10. using namespace std;
  11.  
  12. struct LIST
  13. {
  14.     int VALUE;
  15.     int INDEX;
  16.     struct LIST* NEXT;
  17.     struct LIST* PREVIOUS;
  18. };
  19.  
  20. void swap(int* a, int* b)
  21. {
  22.     *a = *a + *b;
  23.     *b = *a - *b;
  24.     *a = *a - *b;
  25. }
  26.  
  27. void SWAP(struct LIST* a, struct LIST* b)
  28. {
  29.     swap(&a->VALUE, &b->VALUE);
  30. }
  31.  
  32. struct LIST* ADD(struct LIST* current_list, int value)
  33. {
  34.     struct LIST* temp = (struct LIST*)malloc(sizeof(struct LIST));
  35.     struct LIST* temp_next = current_list->NEXT;
  36.     current_list->NEXT = temp;
  37.     temp->VALUE = value;
  38.     temp->NEXT = temp_next;
  39.     temp->PREVIOUS = current_list;
  40.     if (temp_next != NULL)
  41.     {
  42.         temp_next->PREVIOUS = temp;
  43.     }
  44.     return (temp);
  45. }
  46.  
  47. struct LIST* DELETE(struct LIST* current_list)
  48. {
  49.     struct LIST* previous = current_list->PREVIOUS;
  50.     struct LIST* next = current_list->NEXT;
  51.     if (previous != NULL)
  52.     {
  53.         previous->NEXT = current_list->NEXT;
  54.     }
  55.     if (next != NULL)
  56.     {
  57.         next->PREVIOUS = current_list->PREVIOUS;
  58.     }
  59.     free(current_list);
  60.     return previous;
  61. }
  62.  
  63. struct LIST* partition(struct LIST* LEFT, struct LIST* RIGHT)
  64. {
  65.     struct LIST* PIVOT = LEFT;
  66.     int pivot_counter = fmin(2, RIGHT->INDEX - LEFT->INDEX - 1);
  67.     //int pivot_counter = rand() % (RIGHT->INDEX - LEFT->INDEX);
  68.     for (int i = 0; i < pivot_counter; i++)
  69.     {
  70.         PIVOT = PIVOT->NEXT;
  71.     }
  72.     cout << "CHOOSE = " << PIVOT->INDEX << " : " <<  PIVOT->VALUE << endl;
  73.     while (LEFT->INDEX <= RIGHT->INDEX)
  74.     {
  75.         while (LEFT->VALUE < PIVOT->VALUE)
  76.         {
  77.             LEFT = LEFT->NEXT;
  78.         }
  79.         while (RIGHT->VALUE > PIVOT->VALUE)
  80.         {
  81.             RIGHT = RIGHT->PREVIOUS;
  82.         }
  83.         if (LEFT->INDEX >= RIGHT->INDEX)
  84.         {
  85.             break;
  86.         }
  87.         SWAP(LEFT, RIGHT);
  88.         LEFT = LEFT->NEXT;
  89.         RIGHT = RIGHT->PREVIOUS;
  90.     }
  91.     return RIGHT;
  92. }
  93.  
  94. void quickSort(struct LIST* LEFT, struct LIST* RIGHT)
  95. {
  96.     if (LEFT->INDEX < RIGHT->INDEX)
  97.     {
  98.         struct LIST* BOUND = partition(LEFT, RIGHT);
  99.         cout << "starting SORT FROM = " << LEFT->INDEX << " : " << LEFT->VALUE << " to " << BOUND->INDEX << " : " << BOUND->VALUE << endl;
  100.         quickSort(LEFT, BOUND);
  101.         cout << "!ended! SORT FROM = " << LEFT->INDEX << " : " << LEFT->VALUE << " to " << BOUND->INDEX << " : " << BOUND->VALUE << endl;
  102.         LIST* temp = LEFT;
  103.         while (temp->PREVIOUS != NULL)
  104.         {
  105.             temp = temp->PREVIOUS;
  106.         }
  107.         while (temp != NULL)
  108.         {
  109.             cout << temp->VALUE << "\t";
  110.             temp = temp->NEXT;
  111.         }
  112.         cout << endl;
  113.         cout << endl;
  114.         cout << "starting SORT FROM = " << BOUND->NEXT->INDEX << " : " << BOUND->NEXT->VALUE << " to " << RIGHT->INDEX << " : " << RIGHT->VALUE << endl;
  115.         quickSort(BOUND->NEXT, RIGHT);
  116.         temp = LEFT;
  117.         while (temp->PREVIOUS != NULL)
  118.         {
  119.             temp = temp->PREVIOUS;
  120.         }
  121.         while (temp != NULL)
  122.         {
  123.             cout << temp->VALUE << "\t";
  124.             temp = temp->NEXT;
  125.         }
  126.         cout << endl;
  127.         cout << "!ended! SORT FROM = " << BOUND->NEXT->INDEX << " : " << BOUND->NEXT->VALUE << " to " << RIGHT->INDEX << " : " << RIGHT->VALUE << endl;
  128.         cout << endl;
  129.         cout << endl;
  130.     }
  131. }
  132.  
  133. int main(void)
  134. {
  135.     //freopen("input.txt", "r", stdin);
  136.     //freopen("output.txt", "w", stdout);
  137.     struct LIST* l = (struct LIST*)malloc(sizeof(struct LIST));
  138.     struct LIST* p = l;
  139.     l->VALUE = 0;
  140.     l->NEXT = NULL;
  141.     l->PREVIOUS = NULL;
  142.     int temp;
  143.     int size = 0;
  144.     while (scanf("%d", &temp))
  145.     {
  146.         size++;
  147.         l->INDEX = size;
  148.         l->VALUE = temp;
  149.         l = ADD(l, 0);
  150.     }
  151.     size--;
  152.     l = DELETE(l);
  153.     quickSort(p, l);
  154.     //SWAP(p, p->NEXT->NEXT);
  155.     while (p != NULL)
  156.     {
  157.         printf("%d ", p->VALUE);
  158.         //cout << p->VALUE << "\t";
  159.         p = p->NEXT;
  160.     }
  161.     //cout << endl;
  162.     return 0;
  163. }
  164.  
  165. /* 1 9 2 8 3 7 4 5 ^D */
  166.  
  167. /* 2 8 3 7 4 5 ^D */
  168.  
  169. /* 8 7 4 5 ^D */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement