Advertisement
Guest User

Untitled

a guest
May 25th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.24 KB | None | 0 0
  1. /**
  2.  * helpers.c
  3.  *
  4.  * Helper functions for Problem Set 3.
  5.  */
  6.  
  7. #include <cs50.h>
  8.  
  9. #include "helpers.h"
  10. #include "stdio.h"
  11.  
  12. /**
  13.  * Returns true if value is in array of n values, else false.
  14.  */
  15.  
  16. bool search(int value, int values[], int n)
  17. {
  18.     // declare search variables
  19.     int start_point = 0; // start point on where to begin search
  20.     int search_phase = 0; // whether we are starting a search or searching through the array now
  21.     int current_search = 0; // the current search index we are on.
  22.     int search_direction = 0; // the current direction we jump when searching the array. 0 = right. 1 = left
  23.     // negative number passed through somehow - return false.
  24.     if(n<0)
  25.     {
  26.         return false;
  27.     }
  28.     // get minimum point
  29.     if(start_point==0)
  30.     {
  31.         start_point=n/2;
  32.     }
  33.     while(value<values[start_point] && start_point > 1)
  34.     {
  35.         start_point=start_point/2;
  36.     }
  37.     //Set up current point.
  38.     if(search_phase==0)
  39.     {
  40.         if(value>values[start_point] || start_point <= 1)
  41.         {
  42.             if(current_search==0)
  43.             {
  44.                 current_search=start_point;
  45.             }
  46.         }
  47.         search_phase=1;
  48.     }
  49.     // set the direction on where to jump.
  50.     // 0 = right, 1 = left.
  51.     if(search_phase==1)
  52.     {
  53.         if(values[current_search] < value)
  54.         {
  55.             search_direction=0;
  56.         }
  57.         else
  58.         {
  59.             search_direction=1;
  60.         }
  61.    
  62.             // scale the current search upward (right) as we haven't found
  63.             // the needle yet.
  64.             while(current_search<n && search_direction == 0)
  65.             {
  66.                 if(values[current_search]!=value)
  67.                 {
  68.                     current_search++;
  69.                 }
  70.                 else
  71.                 {
  72.                     // We found the needle!
  73.                     return true;
  74.                 }
  75.             }
  76.            
  77.             // scale the current search downward (left) as we haven't found
  78.             // the needle yet.
  79.             while(current_search>=0 && search_direction == 1)
  80.             {
  81.                 if(values[current_search]!=value)
  82.                 {
  83.                     current_search--;
  84.                 }
  85.                 else
  86.                 {
  87.                     // We found the needle!
  88.                     return true;
  89.                 }
  90.             }
  91.     }
  92.     // return false so we don't get in a forever loop.
  93.     return false;
  94. }
  95.  
  96. /**
  97.  * Sorts array of n values.
  98.  */
  99. void sort(int values[], int n)
  100. {
  101.     // declare search position variables
  102.     int position;
  103.     int swap;
  104.     // search and sort the array.
  105.     for(int i = 0; i < (n - 2); i++)
  106.     {
  107.         for(int check = 0, look = 1; look < n; look++, check++)
  108.         {
  109.             position = check;
  110.             if(values[position]>values[look])
  111.             {
  112.                 position=look;
  113.             }
  114.             // swap the values around per selected position.
  115.                 if(position != check)
  116.                 {
  117.                     swap = values[check];
  118.                     values[check] = values[position];
  119.                     values[position] = swap;
  120.                 }
  121.            
  122.         }
  123.     }
  124.     return;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement