Hacks
- Why do you think a programmer should care about space and time complexity?
- A programmer should care about space and time complexity because this is what creates and what is shown on your website and like for example, the bigger the image the more space it takes up.
- Do you think this is a time complexity or space complexity or both problem?
- This is a time and spcae problem because the bigger the photo, the longer it will take to load / process. If it is too big then there is not space for photo. You increase the bandwidth to increase the time. Small photo loads very fast while larger photo takes longer.
- Record your findings when testing the time elapsed of the different algorithms.
- Although we will go more in depth later, time complexity is a key concept that relates to the different sorting algorithms. Do some basic research on the different types of sorting algorithms and their time complexity.
- Answer: Bubble Sort: Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.
Selection Sort: Selection sort is another simple sorting algorithm that sorts an array by repeatedly finding the minimum element from unsorted part and putting it at the beginning.
Insertion Sort: Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time. It is much less efficient on large lists than other sorting algorithms such as quicksort, heapsort, or merge sort.
-
Why is time and space complexity important when choosing an algorithm?
-
Answer: time and space complexity are important considerations when choosing an algorithm because they directly impact the performance and efficiency of your program, and can affect scalability and resource constraints.
-
Should you always use a constant time algorithm / Should you never use an exponential time algorithm? Explain?
-
No, you should not always use a constant time algorithm, nor should you never use an exponential time algorithm. The choice of algorithm depends on the specific requirements and constraints of the problem you are trying to solve.
-
What are some general patterns that you noticed to determine each algorithm’s time and space complexity?
-
Counting basic operations: One common method for analyzing an algorithm’s time complexity is to count the number of basic operations it performs, such as arithmetic operations, comparisons, and assignments. This approach works well for simple algorithms where each operation takes roughly the same amount of time.
Big O notation: Big O notation is a mathematical notation that describes the upper bound of an algorithm’s time complexity in terms of the input size. It provides a way to express the algorithm’s growth rate and allows for easy comparison of different algorithms. For example, an algorithm with O(n) time complexity means that its runtime grows linearly with the input size.
Complete the Time and Space Complexity analysis questions linked below. Practice