Advertisement
noob339

Untitled

Dec 27th, 2021
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.83 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define SIZE 7
  6.  
  7. void printArr(int arr[], int size);
  8. void findMin(int arr[], int size);
  9. void findMax(int arr[], int size);
  10. void bubbleSort(int arr[], int size);
  11. void selectionSort(int arr[], int size);
  12. void binarySearch(int arr[], int size, int search);
  13. void linearSearch(int arr[], int size, int searchKey);
  14.  
  15. //call the functions you wish to invoke as you'd like and test with your own arrays please
  16.  
  17. int main(void)
  18. {
  19.     int arr[SIZE] = {4, 3, 1, 7, 5, 9, 2};
  20.     int sortedArr[SIZE] = {1, 2, 3, 4, 5, 6, 7};
  21.    
  22.     /*
  23.     findMin(arr, SIZE);
  24.     findMin(sortedArr, SIZE);
  25.     findMax(arr, SIZE);
  26.     findMax(sortedArr, SIZE);
  27.     */
  28.    
  29.     //bubbleSort(arr, SIZE);
  30.    
  31.     //selectionSort(arr,SIZE);
  32.    
  33.     //printArr(arr, SIZE);
  34.    
  35.     //int x = get_int("Enter the integer you wish you find: ");
  36.    
  37.     //binarySearch(arr, SIZE, x);
  38.     //linearSearch(arr, SIZE, x);
  39.  
  40.     printf("Hello World\n"); //ignore, just a habit i have
  41. }
  42.  
  43. void findMin(int arr[], int size) //should work for sorted or unsorted
  44. {
  45.     int min = arr[0];
  46.  
  47.     for (int i = 0; i < 7; i++)
  48.     {
  49.         if(arr[i] < min)
  50.         {
  51.             min = arr[i];
  52.         }
  53.     }
  54.     printf("This: %i is the min \n", min);
  55. }
  56.  
  57. void findMax(int arr[], int size) //for sorted or unsorted
  58. {
  59.     int max = 0;
  60.  
  61.     for (int i = 0; i < size; i++)
  62.     {
  63.         if(arr[i] > max)
  64.         {
  65.             max = arr[i];
  66.         }
  67.     }
  68.  
  69.     printf("This: %i is the max \n", max);
  70. }
  71.  
  72. void bubbleSort(int arr[], int size)
  73. {  
  74.     int temp;
  75.     bool flag = false;
  76.    
  77.     do
  78.     {   flag = false;
  79.        
  80.         for(int i = 0; i < size; i++)
  81.         {
  82.             if(arr[i] > arr[i + 1])
  83.             {
  84.                 flag = true;
  85.                 temp = arr[i];
  86.                 arr[i] = arr[i + 1];
  87.                 arr[i + 1] = temp;            
  88.             }
  89.         }
  90.     }
  91.     while(flag == true);
  92. }
  93.  
  94. void linearSearch(int arr[], int size, int searchKey)
  95. {
  96.     int position = -1;
  97.     bool flag = false;
  98.    
  99.     for(int i = 0; i < size; i++)
  100.     {
  101.         if(arr[i] == searchKey)
  102.         {
  103.             flag = true;
  104.             position = i; //you can return this position by giving the function a data type  or print it you want
  105.             printf("Key found at position: %i \n", position);
  106.             break;
  107.         }
  108.     }
  109.     //the following isn't necessary but just in case the key does not exists (I will use a boolean value, which was not used before)
  110.     if (flag == false)
  111.     {
  112.         printf("Key not found\n");
  113.     }
  114. }
  115.  
  116. void selectionSort(int arr[], int size)
  117. {
  118.     int temp = 0;
  119.    
  120.     for(int i = 0; i < size; i++)
  121.     {
  122.         for(int j = i + 1; j < size; j++)
  123.         {
  124.             if (arr[i] > arr[j])
  125.             {
  126.                 temp = arr[i];
  127.                 arr[i] = arr[j];
  128.                 arr[j] = temp;
  129.             }
  130.         }
  131.     }
  132. }
  133.  
  134. void binarySearch(int arr[], int size, int search)
  135. {
  136.     int i = 0; //index set to 0
  137.     int lastI = size - 1; //last index or last element
  138.     bool found = false; //our flag
  139.     int position = -1; //in case index isn't found
  140.    
  141.     while (found == false && i <= lastI)
  142.     {
  143.         int mid = (i + lastI)/ 2; //setting the mid point
  144.        
  145.         if(arr[mid] == search)
  146.         {
  147.             found = true;
  148.             position = mid;
  149.             printf("Key found at position: %i \n", position); //I didn't add a if NOT found case
  150.         }
  151.         else if (arr[mid] > search)
  152.         {
  153.             lastI = mid - 1;
  154.         }
  155.         else
  156.         {
  157.             i = mid + 1;
  158.         }
  159.     }
  160. }
  161.  
  162. /*
  163.  
  164. void mergeSort()
  165. {
  166.  
  167. }
  168.  
  169. */
  170.  
  171. void printArr(int arr[], int size)
  172. {
  173.     for(int i = 0; i < size; i++)
  174.     {
  175.         printf("%i ", arr[i]);
  176.     }
  177.     printf("\n");
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement