Advertisement
satyaki30

searching

Nov 15th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.35 KB | None | 0 0
  1. //searching master program
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "rand_range.c"
  6. #include <time.h>
  7. #include <stdbool.h>
  8.  
  9.  
  10. #define True 1
  11. #define False 0
  12.  
  13. void swap(int *x, int *y)
  14. {
  15.     *x = *x ^ *y;
  16.     *y = *x ^ *y;
  17.     *x = *x ^ *y;
  18. }
  19.  
  20. int linear_search(int *ar, int len, int key)
  21. {
  22.     //naiive linear search
  23.     int i;
  24.  
  25.     for (i = 0; i < len; i ++)
  26.     {
  27.         if (ar[i] == key)
  28.             break;
  29.     }
  30.    
  31.     return i;
  32. }
  33.  
  34. int binary_search_iter(int *ar, int len, int key)
  35. {
  36.     //iterative binary search on a sorted list
  37.     int low, high, mid;
  38.     short int found = False;
  39.     int loop_ctr = 0;
  40.  
  41.     low = 0;
  42.     high = len;
  43.  
  44.     while (low <= high)
  45.     {
  46.         loop_ctr ++;
  47.  
  48.         mid = (low + high) / 2;
  49.         //printf("\nlow = %d, high = %d, mid = %d, ar[mid] = %d", low, high, mid, ar[mid]);
  50.         if (ar[mid] < key)
  51.             low = mid + 1;
  52.        
  53.         else if (ar[mid] > key)
  54.             high = mid - 1;
  55.  
  56.         else
  57.         {
  58.             break;
  59.         }
  60.     }
  61.  
  62.     return loop_ctr;
  63. }
  64.  
  65. int binary_search_rec_aux(int *ar, int len, int key, int low, int high)
  66. {
  67.     //auxilliary function for recursive binary search on a sorted list
  68.     int mid;
  69.     mid = (low + high) / 2;
  70.  
  71.     if (low > high)
  72.         return False;
  73.  
  74.     if (ar[mid] < key)
  75.         binary_search_rec_aux(ar, len, key, mid + 1, high);
  76.  
  77.     else if (ar[mid] > key)
  78.         binary_search_rec_aux(ar, len, key, low, mid - 1);
  79.  
  80.     else
  81.         return True;
  82. }
  83.  
  84. int binary_search_rec(int *ar, int len, int key)
  85. {
  86.     return binary_search_rec_aux(ar, len, key, 0, len);
  87. }
  88.  
  89. int interpolation_search1(int* sortedArray, int len, int key)
  90. {
  91.   // Returns index of key in sortedArray, or -1 if not found
  92.     int low = 0;
  93.     int mid;
  94.     int high = len - 1;
  95.     int loop_ctr = 0;
  96.  
  97.     while (sortedArray[low] <= key && sortedArray[high] >= key)
  98.     {
  99.         loop_ctr ++;
  100.         mid = low + (((key - sortedArray[low]) / (sortedArray[high] - sortedArray[low])) * (high - low));  //out of range is possible  here
  101.  
  102.         if (sortedArray[mid] < key)
  103.             low = mid + 1;
  104.    
  105.         else if (sortedArray[mid] > key)
  106.            // Repetition of the comparison code is forced by syntax limitations.
  107.             high = mid - 1;
  108.         else
  109.             return loop_ctr;
  110.     }
  111.  
  112.     return loop_ctr; // Not found
  113. }  
  114.  
  115. int interpolation_search(int *a, int bottom, int top, int item)
  116. {
  117.  
  118.     int mid;
  119.     int loop_ctr = 0;
  120.  
  121.     while (bottom <= top)
  122.     {
  123.         loop_ctr ++;
  124.         mid = bottom + (top - bottom) * ((item - a[bottom]) / (a[top] - a[bottom]));
  125.        
  126.         if (item == a[mid])
  127.             break;
  128.  
  129.         if (item < a[mid])
  130.             top = mid - 1;
  131.        
  132.         else
  133.             bottom = mid + 1;
  134.     }
  135.  
  136.     return loop_ctr;
  137. }
  138.  
  139. void array_printer(int *ar, int len)
  140. {
  141.     int i;
  142.     printf("\n");
  143.  
  144.     for (i = 0; i < len; i ++)
  145.         printf("%d\t", ar[i]);
  146.  
  147.     printf("\n");
  148. }
  149.  
  150.  
  151. int* random_gen(int len)
  152. {
  153.     bool *flag;
  154.     int *random_nos;
  155.     int i, r;
  156.  
  157.     flag = (bool *) calloc (2*len, sizeof(bool));
  158.     random_nos = (int *) calloc(len, sizeof(int));
  159.  
  160.     srand(time(NULL));
  161.     i = 0;
  162.  
  163.     while (i < len)
  164.     {
  165.         r = rand() % (2*len) + 1;
  166.  
  167.         if (!flag[r-1])
  168.         {
  169.             flag[r-1] = true;
  170.             random_nos[i ++] = r;
  171.         }
  172.     }
  173.  
  174.     return random_nos;
  175. }
  176.  
  177.  
  178.  
  179. int* merge(int *left, int len_left, int *right, int len_right)
  180. {
  181.     //merges the two sorted arrays into a single sorted array
  182.     int *merged_arr, len = 0;
  183.     int right_idx = 0, left_idx = 0;
  184.  
  185.     merged_arr = (int *) calloc(len_right + len_left, sizeof(int));
  186.    
  187.     while (len != len_right + len_left)
  188.     {
  189.         if ((left_idx >= len_left) && (right_idx < len_right)) //the left array has exhausted
  190.             merged_arr[len ++] = right[right_idx ++];
  191.  
  192.         else if ((right_idx >= len_right) && (left_idx < len_left)) //the right array has exhausted
  193.             merged_arr[len ++] = left[left_idx ++];
  194.  
  195.         else
  196.         {
  197.             if (left[left_idx] < right[right_idx])
  198.                 merged_arr[len ++] = left[left_idx ++];
  199.             else
  200.                 merged_arr[len ++] = right[right_idx ++];
  201.         }
  202.  
  203.         //array_printer(merged_arr, len_right + len_left);
  204.     }
  205.  
  206.     free(left);
  207.     free(right);
  208.  
  209.     return merged_arr;
  210. }
  211.  
  212. int* merge_sort(int *ar, int len)
  213. {
  214.     int *right, *left, i, j, len_left, len_right;
  215.  
  216.     if (len < 2)
  217.         return ar;
  218.     else
  219.     {
  220.         len_left = len / 2;
  221.         len_right = len / 2;
  222.  
  223.         if (len % 2 != 0)
  224.             len_left += 1;
  225.        
  226.         left = (int *) calloc(len_left, sizeof(int));
  227.         right = (int *) calloc(len_right, sizeof(int));
  228.  
  229.         for (i = 0; i < len_left; i ++)
  230.             left[i] = ar[i];
  231.  
  232.  
  233.         for (j = 0; j < len_right; j ++)
  234.             right[j] = ar[i++];
  235.  
  236.         //array_printer(left, len_left);
  237.         //array_printer(right, len_right);
  238.  
  239.         left  = merge_sort(left, len_left);
  240.         right = merge_sort(right, len_right);
  241.  
  242.         return merge(left, len_left, right, len_right);
  243.     }
  244. }
  245.  
  246. int main()
  247. {
  248.     int *ar, len = 5, key = rand() % len + 1;
  249.     int n, num_trials = 50;
  250.     int size;
  251.     float total_comparsions;
  252.  
  253.  
  254.     //ar = rand_range(len);
  255.     //ar = random_gen(len);
  256.     //ar = merge_sort(ar, len);
  257.     //array_printer(ar, len);
  258.  
  259.     /*
  260.    
  261.     n = 0;
  262.  
  263.     size = 10;
  264.  
  265.     while(size <= 10000000)
  266.     {
  267.         total_comparsions = 0.0;
  268.         num_trials = 3;
  269.         //ar = (size);
  270.         len = size;
  271.  
  272.         while (num_trials)
  273.         {
  274.             ar = random_gen(len);
  275.             key = rand() % (2*len) + 1;
  276.  
  277.             total_comparsions += linear_search(ar, len, key);
  278.             num_trials --;
  279.         }
  280.  
  281.         total_comparsions /= 3;
  282.         printf("\nLINEAR SEARCH %.2f comparisons for size %d", total_comparsions, size);
  283.         size *= 10;
  284.     }
  285.     /*
  286.     printf("\nLINEAR SEARCH\naverage no of comparisons = %d, total no of comparisons = %d\n", total_comparsions / num_trials, total_comparsions);
  287.  
  288.    
  289.     size = 10;
  290.  
  291.     while(size <= 1000000)
  292.     {
  293.         total_comparsions = 0.0;
  294.         num_trials = 3;
  295.         //ar = (size);
  296.         len = size;
  297.  
  298.         while (num_trials)
  299.         {
  300.             ar = random_gen(len);
  301.             ar = merge_sort(ar, len);
  302.  
  303.             key = rand() % (2*len) + 1;
  304.  
  305.             total_comparsions += binary_search_iter(ar, len, key);
  306.             num_trials --;
  307.         }
  308.  
  309.         total_comparsions /= 3;
  310.         printf("BINARY SEARCH %.2f comparisons for size %d\n", total_comparsions, size);
  311.         size *= 10;
  312.     }
  313.     */
  314.    
  315.     size = 10;
  316.  
  317.  
  318.     while(size <= 10000000)
  319.     {
  320.         total_comparsions = 0.0;
  321.         num_trials = 3;
  322.         //ar = (size);
  323.         len = size;
  324.  
  325.         while (num_trials)
  326.         {
  327.             ar = random_gen(len);
  328.             ar = merge_sort(ar, len);
  329.  
  330.             key = rand() % (2 * len) + 1;
  331.  
  332.             //total_comparsions += interpolation_search(ar, 0, len, key);
  333.             total_comparsions += interpolation_search1(ar, len, key);
  334.  
  335.             num_trials --;
  336.         }
  337.  
  338.         total_comparsions /= 3;
  339.         printf("INTERPOLATION SEARCH %.2f comparisons for size %d\n", total_comparsions, size);
  340.         size *= 10;
  341.     }
  342.    
  343.     return 0;
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement