Code-Demons / miniproject

Apache License 2.0
0 stars 1 forks source link

Work I have done in Mini-Project #32

Open nikhilc3 opened 9 months ago

nikhilc3 commented 9 months ago

Track Event

https://github.com/Code-Demons/miniproject/assets/111464993/ec4d05af-00ae-4add-a992-15fbd07d04d3

Created the race simulation where users can input an array and watch different sorting algorithms (Bubble Sort, Insertion Sort, Merge Sort, Selection Sort) race against each other. Each person represents each algorithm's performance on a canvas, displaying sorting speeds and the final results, fetched from the backend.

Swimming Event

https://github.com/Code-Demons/miniproject/assets/111464993/5835a7ef-4bfb-4164-9fbd-1fe7cfdacf09

Created an Olympic-style race simulation where users can input a Fibonacci number to see different programming methods (For Loop, Recursion, While Loop, Dynamic Programming) compete in a race. Each runner on the canvas represents a different method's performance, showcasing execution speeds and results, all of which are fetched from backend.

Calculation for Fibonacci

Fibonacci Dynamic

Screenshot 2023-12-12 at 1 48 38 PM

Explanation: In this code, I have implemented a dynamic method for calculating Fibonacci numbers. When the fibo function is called with an integer n, it first checks if n is 1 or less. If n is greater than 1, the function initializes an array fib with a length of n + 1, where each index of the array represents a Fibonacci sequence number. The function then iteratively fills this array, starting with fib[0] = 0 and fib[1] = 1. From the third element onwards (index 2 to n), each element fib[i] is assigned the sum of the two preceding elements (fib[i - 1] + fib[i - 2]).

Fibonnaci For loop

Screenshot 2023-12-12 at 1 48 55 PM

Explanation: In this code, I created a for loop to find Fibonacci numbers. It starts by checking if the given number n is 1 or less. If it is, the code just returns n because the first two numbers in the Fibonacci series are 0 and 1. But if n is more than 1, I set up two long variables, fib and prevFib, both starting at 1. These keep track of the latest numbers in the Fibonacci sequence. Then, in a for loop that goes from 2 to n, the code keeps updating fib to be the sum of itself and prevFib, and prevFib becomes the old fib.


Fibonnaci Recursive

Screenshot 2023-12-12 at 1 49 11 PM

Explanation: In this code, I used a recursive method to find Fibonacci numbers. The code works by checking if the number n is 1 or less. If it is, it just returns n, because the first two Fibonacci numbers are 0 and 1. But if n is more than 1, the function calls itself twice – once with n-1 and once with n-2. These calls keep breaking down the problem into smaller pieces until they reach the base case of 0 or 1. Then, it adds up these results to get the Fibonacci number for position n.


Fibonnaci While loop

Screenshot 2023-12-12 at 1 49 39 PM

Explanation: In this code, I've used a while loop to calculate Fibonacci numbers. It starts by checking if the number n is 1 or less. If yes, the code returns n directly because the first two numbers in the Fibonacci sequence are 0 and 1. For n greater than 1, I set up three long variables: a, b, and c, with a starting at 0 and b at 1. These variables help in calculating the next number in the sequence. The while loop runs as long as i, which starts at 2, is less than or equal to n. Inside the loop, c is assigned the sum of a and b, and then a and b are updated to the next two numbers in the sequence. When the loop ends, b holds the Fibonacci number at position n.

Sorting

Bubble Sort

Screenshot 2023-12-12 at 2 18 58 PM

Explanation: I wrote code that goes through the array multiple times, each time comparing adjacent elements and swapping them if they are in the wrong order. This process repeats until the array is sorted. It's a simple method where each pass pushes the largest element to the end, like bubbles rising to the surface.

Insertion Sort

Screenshot 2023-12-12 at 2 19 18 PM

Explanation: The code starts from the second element and compare it with the elements before it, moving each one up to make space for the new element if it's greater. This is repeated for each element, inserting it into its correct position, like sorting a hand of cards.

MergeSort

https://github.com/Code-Demons/miniproject/assets/111464993/9665d914-1c06-41e4-a7b6-26d4ba4da6d1

Explanation: I implemented a divide-and-conquer strategy. The array is recursively split into two halves until each subset has a single element, then these subsets are merged in a sorted manner. This method efficiently combines the elements back into a sorted array.

Selection Sort

Screenshot 2023-12-12 at 2 22 16 PM

Explanation: The code repeatedly finds the minimum element and move it to the start of the array. This is done by selecting the smallest unsorted element and swapping it with the element at the beginning of the unsorted section, gradually moving the boundary between sorted and unsorted parts of the array.

Controllers

Fibonacci Controller

https://github.com/Code-Demons/miniproject/assets/111464993/803da155-d3c8-4de1-8e3c-fa10afa2dfa4

Explanation: Contains the endpoints from the result of the calculations of the fibonacci and the nanoseconds are calculated with system.nanoTime from start to end through each fibonacci type of solving.

Sorting Controller

https://github.com/Code-Demons/miniproject/assets/111464993/fe24acd8-7481-44b6-8e33-bc6b232820ff

Explanation: Contains the endpoints from the result of the different methods of sorting and the nanoseconds are calculated with system.nanoTime from start to end through each type of sorting.

Key Commits (Committed everything at the end, with fully functional code and throughout development during 3 weeks, was testing through local host) :

Some Improvements: