- Discuss the concept of recursion in programming. How does it work, and in what scenarios might recursion be more advantageous than iterative solutions?
- Explain the difference between time complexity and space complexity in algorithms. Why is it important to consider both when evaluating the efficiency of an algorithm?
- What distinguishes linear data structures from non-linear data structures? Provide examples and use cases where one might be preferred over the other.
- Discuss difference between linear and binary search
- In your own words explain the following data structures:
- Linked List
- Stack
- Queues
Group Names: 1.Nkosi Ntokozo
2.Sakhile Motha
3.Gerald Mutswi
1.Recursion in JavaScript is a powerful technique where a function calls itself repeatedly to solve a problem.
Advantage: Recursion can be advantageous for simplifying complex problems, like those with tree-like structures or that can be divided into smaller parts. However, it can also lead to stack overflow errors if the recursion goes too deep, and it might be less efficient than iterative solutions in some cases.
2.Time complexity refers to how the runtime of an algorithm grows as the size of the input increases. Space complexity refers to how much memory an algorithm uses as the input size increases. It's important to consider both because an algorithm might be fast but use a lot of memory, or it might be memory-efficient but slow. By understanding both, we can choose algorithms that balance speed and memory usage, optimizing overall performance.
3. linear data structures: elements are arranged in a sequential order, like a train with connected cars, each element has a well defined predecessor.... non linear: elements are not in a sequential order, but rather in a hierarchical or web like structure. elements can have multiple connections.
4.Linear search, also known as sequential search, scans through each element in a list or array one by one until it finds the target value.
It starts from the beginning and compares each element with the target.
#Binary search operates on sorted arrays. It repeatedly divides the search interval in half and compares the middle element with the target.
If the middle element is equal to the target, the search is successful. Otherwise, it narrows down the search to the left or right half.
5.#Linked List: Imagine a chain of paperclips where each paperclip knows where the next one is. That's a linked list! It's like a chain letter, but less annoying. You can add or remove paperclips easily without disturbing the others in the chain.
#Stack: Picture a stack of pancakes. You can only add or remove a pancake from the top. So, the last pancake you put on is the first one you'll take off. It's like a pancake tower where you can only eat from the top down.
#Queue: Think of a queue like waiting in line for your favorite roller coaster. The first person to join the line is the first one to ride the coaster. No cutting! It's like being at a buffet where you patiently wait your turn to scoop up the mashed potatoes.