CH #2 Solved Exercise
CH #2 Solved Exercise
1. Differentiate between
a. Clarity vs Efficiency
Clarity and efficiency in code are crucial but often opposing qualities. Clarity refers to how
understandable and readable code is, making it easier to debug, maintain, and modify, with well-
organized structure, descriptive names, and meaningful comments. Efficiency, on the other hand,
focuses on optimal resource usage, such as time and memory, requiring optimized algorithms and
data structures. Balancing the two involves trade-offs: clearer code can be less efficient, while highly
efficient code can be harder to understand. The ideal approach depends on the project's
requirements, prioritizing clarity in small projects and efficiency in performance-critical applications.
b. Abstraction vs Pattern Recognition
Abstraction and pattern recognition are key in computational thinking. Abstraction simplifies complex
systems by focusing on essential details and ignoring irrelevant ones, enabling easier management and
problem-solving. Pattern recognition identifies similarities, trends, and regularities in data, aiding in
predicting outcomes and optimizing solutions. Together, they streamline problem-solving by
simplifying tasks and highlighting essential features.
c. Pseudocode vs flowcharts
Pseudocode and flowcharts are tools for planning and visualizing algorithms. Pseudocode uses plain
language to describe the steps of an algorithm, making it easy to write and understand without strict
syntax rules. Flowcharts, on the other hand, use symbols and arrows to represent the flow of a process
visually, providing a clear picture of the algorithm's structure. Both help in designing and
communicating solutions, with pseudocode focusing on detailed step-by-step instructions and
flowcharts emphasizing the overall process flow.
d. Data structures and control structures
Data structures and control structures are fundamental concepts in programming. Data structures
organize and store data efficiently, such as arrays, linked lists, and trees, enabling effective data
manipulation and retrieval. Control structures, like loops, conditionals, and branches, determine the
flow of execution in a program, dictating how and when certain operations are performed. While data
structures manage and hold data, control structures guide the sequence of actions and decisions based
on that data.
e. Algorithm vs Pseudocode
An algorithm is a step-by-step procedure or set of rules designed to perform a specific task or solve a
problem, often described in precise, formal terms. Pseudocode, on the other hand, is a high-level,
informal way of representing an algorithm using plain language and simple notation, making it easier
to understand and communicate the logic without adhering to strict programming syntax. While an
algorithm outlines the overall process, pseudocode provides a readable, intermediary representation
of that process, bridging the gap between conceptual design and actual code implementation.
2. Write note on working of Bubble Sort?
Ans: Bubble sort is a straightforward sorting algorithm that repeatedly steps through the list to be
sorted, compares adjacent elements, and swaps them if they are in the wrong order. The process
continues until the entire list is sorted. Here’s a brief overview of how it works:
1. Initial Pass: Starting from the beginning of the list, compare the first two elements. If the first
element is greater than the second, swap them.
2. Subsequent Passes: Move to the next pair of adjacent elements and repeat the comparison and
swapping process. Continue this process for each pair until the end of the list is reached.
3. End of Pass: After completing a pass through the list, the largest element will be in its correct
position at the end.
4. Repeating: Repeat the process for the remaining unsorted portion of the list (excluding the last
sorted elements). Each subsequent pass will place the next largest element in its correct position.
5. Termination: The algorithm terminates when no more swaps are needed, indicating that the list is
fully sorted.
3. Write names of commonly used computational artifacts made during computational thinking.
4. Where we prefer to use binary search algorithm rather than linear search algorithm.
We prefer to use the binary search algorithm over the linear search algorithm in scenarios where:
1. The Data is Sorted: Binary search requires that the data be sorted, as it works by repeatedly dividing
the search interval in half. Linear search doesn't have this requirement but is less efficient in such
cases.
2. Large Data Sets: For large datasets, binary search is significantly faster than linear search. Binary
search has