Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- #define SIZE 7
- void printArr(int arr[], int size);
- void findMin(int arr[], int size);
- void findMax(int arr[], int size);
- void bubbleSort(int arr[], int size);
- void selectionSort(int arr[], int size);
- void binarySearch(int arr[], int size, int search);
- void linearSearch(int arr[], int size, int searchKey);
- //call the functions you wish to invoke as you'd like and test with your own arrays please
- int main(void)
- {
- int arr[SIZE] = {4, 3, 1, 7, 5, 9, 2};
- int sortedArr[SIZE] = {1, 2, 3, 4, 5, 6, 7};
- /*
- findMin(arr, SIZE);
- findMin(sortedArr, SIZE);
- findMax(arr, SIZE);
- findMax(sortedArr, SIZE);
- */
- //bubbleSort(arr, SIZE);
- //selectionSort(arr,SIZE);
- //printArr(arr, SIZE);
- //int x = get_int("Enter the integer you wish you find: ");
- //binarySearch(arr, SIZE, x);
- //linearSearch(arr, SIZE, x);
- printf("Hello World\n"); //ignore, just a habit i have
- }
- void findMin(int arr[], int size) //should work for sorted or unsorted
- {
- int min = arr[0];
- for (int i = 0; i < 7; i++)
- {
- if(arr[i] < min)
- {
- min = arr[i];
- }
- }
- printf("This: %i is the min \n", min);
- }
- void findMax(int arr[], int size) //for sorted or unsorted
- {
- int max = 0;
- for (int i = 0; i < size; i++)
- {
- if(arr[i] > max)
- {
- max = arr[i];
- }
- }
- printf("This: %i is the max \n", max);
- }
- void bubbleSort(int arr[], int size)
- {
- int temp;
- bool flag = false;
- do
- { flag = false;
- for(int i = 0; i < size; i++)
- {
- if(arr[i] > arr[i + 1])
- {
- flag = true;
- temp = arr[i];
- arr[i] = arr[i + 1];
- arr[i + 1] = temp;
- }
- }
- }
- while(flag == true);
- }
- void linearSearch(int arr[], int size, int searchKey)
- {
- int position = -1;
- bool flag = false;
- for(int i = 0; i < size; i++)
- {
- if(arr[i] == searchKey)
- {
- flag = true;
- position = i; //you can return this position by giving the function a data type or print it you want
- printf("Key found at position: %i \n", position);
- break;
- }
- }
- //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)
- if (flag == false)
- {
- printf("Key not found\n");
- }
- }
- void selectionSort(int arr[], int size)
- {
- int temp = 0;
- for(int i = 0; i < size; i++)
- {
- for(int j = i + 1; j < size; j++)
- {
- if (arr[i] > arr[j])
- {
- temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
- }
- void binarySearch(int arr[], int size, int search)
- {
- int i = 0; //index set to 0
- int lastI = size - 1; //last index or last element
- bool found = false; //our flag
- int position = -1; //in case index isn't found
- while (found == false && i <= lastI)
- {
- int mid = (i + lastI)/ 2; //setting the mid point
- if(arr[mid] == search)
- {
- found = true;
- position = mid;
- printf("Key found at position: %i \n", position); //I didn't add a if NOT found case
- }
- else if (arr[mid] > search)
- {
- lastI = mid - 1;
- }
- else
- {
- i = mid + 1;
- }
- }
- }
- /*
- void mergeSort()
- {
- }
- */
- void printArr(int arr[], int size)
- {
- for(int i = 0; i < size; i++)
- {
- printf("%i ", arr[i]);
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement