Advertisement
CherMi

Algorithms lab 1 version 0.1

Oct 31st, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.70 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[] = { 13, 74, 52, 11, 23, 9, 67, 48, 12, 23, 64, 73, 6, 29, 97, 34, 76, 23, 15, 49, 5 };
  27.     int K, s, q, t, bin;
  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++;
  97.         if (arr[i] == K)
  98.         {
  99.             result.count++;
  100.             result.oper_count[result.count] = oper_count;
  101.             result.index[result.count] = i;
  102.         }
  103.         oper_count++;
  104.         oper_count++;
  105.     }
  106.     if (result.count < 0) //Если не найдено
  107.     {
  108.         result.oper_count[0] = oper_count;
  109.     }
  110.     return &result;
  111.  
  112. }
  113. struct Result * q_search(int arr[], int K)
  114. {
  115.     int i;
  116.     int oper_count = 0;
  117.     struct Result result;
  118.     initialize_result(&result);
  119.  
  120.     int farr[22]; //Фиктивный массив
  121.     for (i = 0; i < 21; i++) //Добавление фиктивной записи
  122.     {
  123.         farr[i] = arr[i];
  124.     }
  125.     farr[21] = K;
  126.     i = 0;
  127.     while (1)
  128.     {
  129.         oper_count++;
  130.         if (farr[i] == K)
  131.         {
  132.             if (i <= 20)
  133.             {
  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++;
  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.         oper_count++;
  170.         if (K >= farr[i])
  171.         {
  172.             if (K == farr[i]){
  173.                 //TODO: не учтено в числе операций
  174.                 result.count++;
  175.                 result.oper_count[result.count] = oper_count;
  176.                 result.index[result.count] = i;
  177.             }
  178.             //Нужно какое-то условие выхода из цикла. Оно замедлит алгоритм, т.к. оно в цикле.
  179.             //TODO: подумать, что с этим можно сделать
  180.             //TODO: эта проверка не учтена в числе операций
  181.             if (i == 22)
  182.             {
  183.                 break;
  184.             }
  185.         }
  186.         oper_count++;
  187.         i++;
  188.     }
  189.     if (result.count < 0) //Если не найдено
  190.     {
  191.         result.oper_count[0] = oper_count;
  192.     }
  193.     return &result;
  194. }
  195.  
  196. struct Result *bin_search(int arr[], int N, int K)
  197. {
  198.     int first_upper_index = -1;
  199.     int first_left_index = -1;
  200.     int oper_count;
  201.     int flag = 0; //0 - не найдено, 1 - найдено
  202.     int mid, left = 0, right = N - 1;
  203.     struct Result result;
  204.     initialize_result(&result);
  205.  
  206.     oper_count = 0;
  207.  
  208.     //Поиск самого левого подходящего элемента
  209.     while (left <= right)
  210.     {
  211.         oper_count++; //Проверка условия цикла
  212.         mid = (left + right) / 2;
  213.         oper_count++; //Операция присваивания
  214.         if (K >= arr[mid])
  215.         {
  216.             right = mid - 1;
  217.             oper_count += 2; //Проверка условия и операция присваивания
  218.         }
  219.         else //K < arr[mid]
  220.         {
  221.             left = mid + 1;
  222.             oper_count += 2; //Проверка условия и операция присваивания
  223.         }
  224.     }
  225.     first_left_index = mid;
  226.     result.count = 0;
  227.     result.oper_count[0] = oper_count;
  228.     result.index[0] = first_left_index;
  229.  
  230.     //Поиск первого неподходящего элемента справа
  231.     left = 0;
  232.     right = N - 1;
  233.    
  234.     while (left <= right)
  235.     {
  236.         oper_count++; //Проверка условия цикла
  237.         mid = (left + right) / 2;
  238.         oper_count++; //Операция присваивания
  239.         if (K <= arr[mid])
  240.         {
  241.             left = mid + 1;
  242.             oper_count += 2; //Проверка условия и операция присваивания
  243.         }
  244.         else //K > arr[mid]
  245.         {
  246.             right = mid - 1;
  247.             oper_count += 2; //Проверка условия и операция присваивания
  248.         }
  249.     }
  250.     first_upper_index = mid;
  251.  
  252.     //TODO: если не найдено
  253.     result.count = (first_upper_index - first_left_index - 1);
  254.     for (int i = 1; i <= result.count; i++)
  255.     {
  256.         result.index[i] = first_left_index + i;
  257.         result.oper_count[i] = oper_count + i; //Операция присваивания
  258.     }
  259.     return &result;
  260. }
  261.  
  262. void sort(int arr[]) //Сортировка по убыванию пузырьком
  263. {
  264.     int i, j, temp;
  265.     for (i = 0; i < 21; i++)
  266.     {
  267.         for (j = i; j < 21; j++)
  268.         {
  269.             if (arr[i] < arr[j])
  270.             {
  271.                 temp = arr[i];
  272.                 arr[i] = arr[j];
  273.                 arr[j] = temp;
  274.             }
  275.         }
  276.     }
  277. }
  278.  
  279. void initialize_result(struct Result *result)
  280. {
  281.     int i;
  282.     result->count = -1;
  283.     for (i = 0; i < 21; i++)
  284.     {
  285.         result->index[i] = 0;
  286.         result->oper_count[i] = 0;
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement