diff --git a/Spring-2023/CS-2124/Lab-Work-1/CMakeLists.txt b/Spring-2023/CS-2124/Lab-Work-1/CMakeLists.txt new file mode 100644 index 0000000..56649be --- /dev/null +++ b/Spring-2023/CS-2124/Lab-Work-1/CMakeLists.txt @@ -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}) diff --git a/Spring-2023/CS-2124/Lab-Work-1/README.org b/Spring-2023/CS-2124/Lab-Work-1/README.org new file mode 100644 index 0000000..4b9cae7 --- /dev/null +++ b/Spring-2023/CS-2124/Lab-Work-1/README.org @@ -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 + diff --git a/Spring-2023/CS-2124/Lab-Work-1/bin/labone b/Spring-2023/CS-2124/Lab-Work-1/bin/labone new file mode 100755 index 0000000..afc436d Binary files /dev/null and b/Spring-2023/CS-2124/Lab-Work-1/bin/labone differ diff --git a/Spring-2023/CS-2124/Lab-Work-1/src/lib.c b/Spring-2023/CS-2124/Lab-Work-1/src/lib.c new file mode 100644 index 0000000..b8485d0 --- /dev/null +++ b/Spring-2023/CS-2124/Lab-Work-1/src/lib.c @@ -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; + } + } + } +} diff --git a/Spring-2023/CS-2124/Lab-Work-1/src/lib.h b/Spring-2023/CS-2124/Lab-Work-1/src/lib.h new file mode 100644 index 0000000..0165c39 --- /dev/null +++ b/Spring-2023/CS-2124/Lab-Work-1/src/lib.h @@ -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 diff --git a/Spring-2023/CS-2124/Lab-Work-1/src/main.c b/Spring-2023/CS-2124/Lab-Work-1/src/main.c new file mode 100644 index 0000000..0941a8e --- /dev/null +++ b/Spring-2023/CS-2124/Lab-Work-1/src/main.c @@ -0,0 +1,24 @@ +#include "./lib.h" +#include + +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); +} diff --git a/Spring-2023/CS-2124/Lectures.org b/Spring-2023/CS-2124/Lectures.org index 7e054b7..05eba6f 100644 --- a/Spring-2023/CS-2124/Lectures.org +++ b/Spring-2023/CS-2124/Lectures.org @@ -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= + +