Posts

Showing posts with the label Data Structures

Binary Search Algorithm

Image
Binary Search Figure [1] Binary Seach Algorithm applied to a descending order list Let's say we have an ascending-order sorted list of numbers as given below: number_list = [6, 12, 24, 39, 42, 43, 45, 89] Say we were tasked with finding whether a particular element was in the list or not, the simplest way that would occur to us is to use a 'for' loop.  target = 39 for num in number_list:   if num == target:     print(f"{num} is in number_list.") This works but say, we have a huge list of about a million or even a billion values, what would be the worst-case time complexity of such an operation? It could be that the target is the last item in the list, which results in the time complexity being O(n). So, let's think about a better way of solving this problem. Now, we know that the list is ascending-order sorted which means if we check a random number in the list and the number is bigger than our target , we can be sure that our target if it is in, will be to th...

July 2nd 2022 - Day 2 of 3 day study bender

Image
I'm happy to report day 2 was successful as well!  Start the day by watching CS50 Lecture number 5 titled 'Data Structures'. Key Takeaways: 1) Libraries are a collection of pre-written code to shorten your development time.  Framework includes a collection of libraries (among other things).  * Arrays have data in contiguous memory, this means if you need to extend an array but have the next memory location pre-allocated for something else, it causes an issue.  * When we dynamically allocate memory by declaring a variable, the memory is reserved from 'heap' whereas when we explicitly allocate memory for our variable, the memory allocated is from 'stack'. 2) There are 3 building blocks for data structures, first is the 'struct' keyword, second is the '*' operator for going to a memory address, & third is the '.' operator for getting the data at the address. '->' is used as an alternative to '* + .'. * We can use a...