CherMi

Algorithms lab 1 version 1.0

Oct 31st, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.07 KB | None | 0 0
  1. #define CRT_SECURE_NO_WARNINGS
  2. #include "pch.h"
  3. #include <iostream>
  4. #include <limits.h>
  5.  
  6. using namespace std;
  7.  
  8. struct Result *s_search(int arr[], int K);
  9. struct Result *q_search(int arr[], int K);
  10. struct Result *t_search(int arr[], int K);
  11. struct Result *bin_search(int arr[], int N, int K);
  12. void sort(int arr[]);
  13. void initialize_result(struct Result *result);
  14.  
  15. struct Result
  16. {
  17.     int count = 0;
  18.     int index[21];
  19.     int oper_count[21];
  20. };
  21.  
  22. int main()
  23. {
  24.     int i;
  25.     int N = 21;
  26.     int arr[] = { 23, 74, 52, 11, 23, 9, 67, 48, 12, 23, 64, 73, 6, 29, 97, 34, 76, 23, 15, 49, 5 };
  27.     int K;
  28.     K = 23;
  29.     struct Result result;
  30.  
  31.     result = *s_search(arr, K);
  32.     if (result.count > 0)
  33.     {
  34.         for (i = 0; i <= result.count; i++)
  35.         {
  36.             printf("Algorithm S: found at position %d, %d operations were required.\n", result.index[i], result.oper_count[i]);
  37.         }
  38.     }
  39.     else
  40.     {
  41.         printf("Algorithm S: not found, %d operations were required.\n", result.oper_count[0]);
  42.     }
  43.  
  44.     result = *q_search(arr, K);
  45.     if (result.count >= 0)
  46.     {
  47.         for (i = 0; i <= result.count; i++)
  48.         {
  49.             printf("Algorithm Q: found at position %d, %d operations were required.\n", result.index[i], result.oper_count[i]);
  50.         }
  51.     }
  52.     else
  53.     {
  54.         printf("Algorithm Q: not found, %d operations were required.\n", result.oper_count[0]);
  55.     }
  56.  
  57.     sort(arr);
  58.     printf("Array was sorted.\n");
  59.    
  60.     result = *t_search(arr, K);
  61.     if (result.count > 0)
  62.     {
  63.         for (i = 0; i <= result.count; i++)
  64.         {
  65.             printf("Algorithm T: found at position %d, %d operations were required.\n", result.index[i], result.oper_count[i]);
  66.         }
  67.     }
  68.     else
  69.     {
  70.         printf("Algorithm T: not found, %d operations were required.\n", result.oper_count[0]);
  71.     }
  72.  
  73.     result = *bin_search(arr, N, K);
  74.  
  75.     if (result.count >= 0)
  76.     {
  77.         for (i = 0; i <= result.count; i++)
  78.         {
  79.             printf("Algorithm B: found at position %d, %d operations were required.\n", result.index[i], result.oper_count[i]);
  80.         }
  81.     }
  82.     else
  83.     {
  84.         printf("Algorithm B: not found, %d operations were required.\n", result.oper_count[0]);
  85.     }
  86. }
  87.  
  88. struct Result *s_search(int arr[], int K)
  89. {
  90.     struct Result result;
  91.     int i;
  92.     int oper_count = 0;
  93.     initialize_result(&result);
  94.     for (i = 0; i < 21; i++)
  95.     {
  96.         oper_count += 2; //Проверка условия и увеличение i
  97.         if (arr[i] == K)
  98.         {
  99.             oper_count++; //Проверка условия
  100.             result.count++;
  101.             result.oper_count[result.count] = oper_count;
  102.             result.index[result.count] = i;
  103.         }
  104.     }
  105.     if (result.count < 0) //Если не найдено
  106.     {
  107.         result.oper_count[0] = oper_count;
  108.     }
  109.     return &result;
  110.  
  111. }
  112. struct Result * q_search(int arr[], int K)
  113. {
  114.     int i;
  115.     int oper_count = 0;
  116.     struct Result result;
  117.     initialize_result(&result);
  118.  
  119.     int farr[22]; //Фиктивный массив
  120.     for (i = 0; i < 21; i++) //Добавление фиктивной записи
  121.     {
  122.         farr[i] = arr[i];
  123.     }
  124.     farr[21] = K;
  125.     i = 0;
  126.     while (1)
  127.     {
  128.         if (farr[i] == K)
  129.         {
  130.         oper_count++; //Проверка
  131.             if (i <= 20)
  132.             {
  133.                 oper_count++; //Проверка
  134.                 result.count++;
  135.                 result.oper_count[result.count] = oper_count;
  136.                 result.index[result.count] = i;
  137.             }
  138.             else
  139.             {
  140.                 if (result.count < 0) //Если не найдено
  141.                 {
  142.                     result.oper_count[0] = oper_count;
  143.                 }
  144.                 return &result; //Пройдены все элементы массива
  145.             }
  146.         }
  147.         i++;
  148.         oper_count++; //Увеличение i
  149.     }
  150. }
  151.  
  152. struct Result *t_search(int arr[], int K)
  153. {
  154.     struct Result result;
  155.     int oper_count = 0;
  156.     int i;
  157.     initialize_result(&result);
  158.     result.count = -1;
  159.     result.oper_count[result.count] = 0;
  160.     int farr[22]; //Фиктивный массив
  161.     for (i = 0; i < 21; i++) //Добавление фиктивной записи
  162.     {
  163.         farr[i] = arr[i];
  164.     }
  165.     farr[21] = INT_MIN;
  166.     i = 0;
  167.     while (1)
  168.     {
  169.         //TODO: посмтореть, что с числом операций, оно странно подсчитывается
  170.         if (K >= farr[i])
  171.         oper_count++; //Проверка
  172.         {
  173.             if (K == farr[i]){
  174.                 oper_count++; //Проверка
  175.                 result.count++;
  176.                 result.oper_count[result.count] = oper_count;
  177.                 result.index[result.count] = i;
  178.             }
  179.             //Нужно какое-то условие выхода из цикла. Оно замедлит алгоритм, т.к. оно в цикле.
  180.             //TODO: подумать, что с этим можно сделать
  181.             if (i == 22)
  182.             {
  183.                 oper_count++; //Проверка
  184.                 break;
  185.             }
  186.         }
  187.         i++;
  188.         oper_count++; //Увеличение i
  189.     }
  190.     if (result.count < 0) //Если не найдено
  191.     {
  192.         result.oper_count[0] = oper_count;
  193.     }
  194.     return &result;
  195. }
  196.  
  197. struct Result *bin_search(int arr[], int N, int K)
  198. {
  199.     int first_upper_index = -1;
  200.     int first_left_index = -1;
  201.     int oper_count;
  202.     int flag = 0; //0 - не найдено, 1 - найдено
  203.     int mid, left = 0, right = N - 1;
  204.     struct Result result;
  205.     initialize_result(&result);
  206.  
  207.     oper_count = 0;
  208.  
  209.     //Поиск самого левого подходящего элемента
  210.     while (left <= right)
  211.     {
  212.         oper_count++; //Проверка условия цикла
  213.         mid = (left + right) / 2;
  214.         oper_count++; //Операция присваивания
  215.         if (K >= arr[mid])
  216.         {
  217.             right = mid - 1;
  218.             oper_count += 2; //Проверка условия и операция присваивания
  219.         }
  220.         else //K < arr[mid]
  221.         {
  222.             left = mid + 1;
  223.             oper_count += 2; //Проверка условия и операция присваивания
  224.         }
  225.     }
  226.     first_left_index = mid;
  227.     result.count = 0;
  228.     result.oper_count[0] = oper_count;
  229.     result.index[0] = first_left_index;
  230.     if (first_left_index < 0) //Если не найдено
  231.     {
  232.         result.count = -1;
  233.         return &result;
  234.     }
  235.  
  236.     //Поиск первого неподходящего элемента справа
  237.     left = 0;
  238.     right = N - 1;
  239.    
  240.     while (left <= right)
  241.     {
  242.         oper_count++; //Проверка условия цикла
  243.         mid = (left + right) / 2;
  244.         oper_count++; //Операция присваивания
  245.         if (K <= arr[mid])
  246.         {
  247.             left = mid + 1;
  248.             oper_count += 2; //Проверка условия и операция присваивания
  249.         }
  250.         else //K > arr[mid]
  251.         {
  252.             right = mid - 1;
  253.             oper_count += 2; //Проверка условия и операция присваивания
  254.         }
  255.     }
  256.     first_upper_index = left; //TODO: понять, почему left, а не mid
  257.  
  258.    
  259.     result.count = (first_upper_index - first_left_index - 1);
  260.     for (int i = 1; i <= result.count; i++)
  261.     {
  262.         result.index[i] = first_left_index + i;
  263.         result.oper_count[i] = oper_count + i; //Операция присваивания
  264.     }
  265.     return &result;
  266. }
  267.  
  268. void sort(int arr[]) //Сортировка по убыванию пузырьком
  269. {
  270.     int i, j, temp;
  271.     for (i = 0; i < 21; i++)
  272.     {
  273.         for (j = i; j < 21; j++)
  274.         {
  275.             if (arr[i] < arr[j])
  276.             {
  277.                 temp = arr[i];
  278.                 arr[i] = arr[j];
  279.                 arr[j] = temp;
  280.             }
  281.         }
  282.     }
  283. }
  284.  
  285. void initialize_result(struct Result *result)
  286. {
  287.     int i;
  288.     result->count = -1;
  289.     for (i = 0; i < 21; i++)
  290.     {
  291.         result->index[i] = 0;
  292.         result->oper_count[i] = 0;
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment