cs-2124(lab): add first lab assignment

This commit is contained in:
Price Hiller 2024-02-01 10:45:55 -06:00
parent 9795a45502
commit eb1c09ab49
Signed by: Price
SSH Key Fingerprint: SHA256:Y4S9ZzYphRn1W1kbJerJFO6GGsfu9O70VaBSxJO7dF8
7 changed files with 128 additions and 6 deletions

View File

@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.25)
set(SOURCES src/lib.h src/lib.c src/main.c)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
project(LabOne LANGUAGES C)
add_executable(labone ${SOURCES})

View File

@ -0,0 +1,13 @@
* Lab Work - Sorting and Binary Search
Get an array of integer, the target integer, then sort the array and find the target using binary
search
- Create a library for binary search and a sorting algorithm to sort the array and use it in main
file
- Input:
- First line contains one integer $n$, the size of the array
- Second line contains $n$ integers $a_i$, elements of the array
- Third line contains the search integer/key $m$
- Output:
- Print the location of the search integer $m$ in the given array. If not found, print =-1=.
Submit the zipped folder

Binary file not shown.

View File

@ -0,0 +1,28 @@
int binary_search(int arr[], int m, int size) {
int low = 0;
int high = size - 1;
while (low != high) {
int mid = (low + high) / 2;
if (m == arr[mid]) {
return mid;
} else if (m > arr[mid]) {
low = mid + 1;
} else {
high = mid - 1;
};
}
return -1;
}
void bubble_sort(int a[], int n) {
int i = 0, j = 0, tmp;
for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j+1]) {
tmp = a[j];
a[j] = a[j+1];
a[j + 1] = tmp;
}
}
}
}

View File

@ -0,0 +1,5 @@
#ifndef CUST_LIB
#define CUST_LIB
int binary_search(int arr[], int m, int size);
void bubble_sort(int a[], int n);
#endif // !CUST_LIB

View File

@ -0,0 +1,24 @@
#include "./lib.h"
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Enter size of array: ");
int arr_size = -1;
scanf("%d", &arr_size);
int arr[arr_size];
printf("Enter elements:\n");
for (int i = 0; i < arr_size; i++) {
scanf("%d", &arr[i]);
}
int m = 0;
printf("Enter search element: ");
scanf("%d", &m);
bubble_sort(arr, arr_size);
printf("Sorted elements to search in: ");
for (int i = 0; i < arr_size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
int location = binary_search(arr, m, arr_size);
printf("Location of %d was found at %d\n", m, location);
}

View File

@ -397,10 +397,10 @@ raise(f"Unable to find {term} in array!")
*** Algorithm Comparison
| Bubble Sort | Selection Sort | Insertion Sort |
|-------------------------------|--------------------------------------------------------|--------------------------|
| Simple Sorting Algorithm | Simple Sorting Algorithm | Simple Sorting Algorithm |
| Compares Neighboring Elements | Takes the smallest element and moves it into its place | Transfer one element at a time to its
| Bubble Sort | Selection Sort | Insertion Sort |
|-------------------------------|--------------------------------------------------------|---------------------------------------|
| Simple Sorting Algorithm | Simple Sorting Algorithm | Simple Sorting Algorithm |
| Compares Neighboring Elements | Takes the smallest element and moves it into its place | Transfer one element at a time to its |
*** Merge Sort
@ -433,6 +433,49 @@ raise(f"Unable to find {term} in array!")
- Steps:
1. Pick an element from the array as the pivot
2. Divide the unosrted array of elements in two arrays
a) Values less than the pivot come in the first sub array
b) Values greater than the pivot come in the second sub-array
a) Values less than the pivot come in the first sub array
b) Values greater than the pivot come in the second sub-array
3. Recursively repeat step ~2~ (until the sub-arrays are sorted)
* Lecture 4
** Methods for Reducing Complexity
- Goes along with the *Simplicity* Design Principle
- They are:
1. Abstraction
2. Modularity
3. Layering
4. Hierarchy
** Stack
- What is a stack
- A data structure.
- New items are inserted on the "top" and deleted or removed from the top. Top is the most
recent item inserted.
- A stack is a Last In First Out (LIFO) or First In Last Out (FILO) data structure. This means
that the last item to get stored on the stack (often called Push operation) is the first one
to get out of it (often called as Pop operation).
** Stack Operations
1. *push*: Adds an element to the top of the stack
2. *pop*: Removes the topmost element from the stack
3. *isEmpty*: Checks whether the stack is empty
4. *isFull*: checks whether the stack is full
5. *top*: Displays the topmost element of the stack
6. *Peek*: View the top element on the stack
- *Important* things to remember when wokring with stacks:
- Initially, a pointer (top) is set to keep track of the topmost itemi n the stack. The stack
is initialized to -1
- Then, a check is performed to determine if the stack is empty by comparing top to -1
- As elements are added to the stack, the position of the top is updated
- As soon as elements are popped or deleted, the topmost element is removed and the position of
top is updated
- There are *two ways to implement* a stack:
1. Using a =array=
2. Using a =linked list=