Prince Assignment
Prince Assignment
FACULTY OF EDUCATION
{Int I ;
For ( I = 0; I < n – 1 ; I + + )
{If ( x = = a [ I ] )
Retur
n I ;}
Retur
n -1 ;}
If C(n) is the number of comparisons, describe and find C(n) for the worst case,
best case and average case complexity if the element is
. Linear Search
Description: Linear search checks each element of the array sequentially until the
desired element is found or the end of the array is reached.
Time Complexity:
Worst Case: \(O(n)\) (Element is not present or is at the last
position) Best Case:\(O(1)\) (Element is at the first position)
Average Case:/(O(n)\)
Space Complexity: \(O(1)\) (constant space)
Merits
- Simple to implement.
- No requirement for the data to be sorted.
- Works well with small datasets.
Demerits
- Inefficient for large datasets due to its linear time complexity.
- On average, it requires \(n/2\) comparisons, which is not optimal for large \(n\)
. Binary Search
Description :Binary search is a more efficient algorithm that works by repeatedly
dividing
the sorted array in half and comparing the middle element with the target value. The
search continues in the half where the element could be.
Time complexity:
Time complexity:
Merits:
Sorting Algorithms
Sorting algorithms are used to arrange the elements of a dataset in a particular
order (usually ascending or descending). Sorting is a fundamental operation
that is often required before performing other tasks like searching.
Bubble sort:
Description: Bubble sort repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order. This process is repeated
until the list is sorted.
Time complexity:
Best Case: \(O(n)\) (the list is already sorted, with an optimized version that checks if
swaps were made)
Average Case:\(O(n^2)\)
Merits:
Demerits:
. Insertion Sort:
Description: Insertion sort builds the final sorted array one element at a time. It picks
elements and inserts them into their correct position by comparing them with elements
in the sorted portion of the array.
Time Complexity
sorting) Merits:
Description: Merge sort is a divide-and-conquer algorithm that divides the array into
two halves, recursively sorts them, and then merges the two sorted halves.
Time complexity:
\log n)\)
Merits:
Demerits:
- Requires additional space for merging, making it less space-efficient than in-
place sorts.
- More complex to implement than simple algorithms like bubble sort or insertion
sort.
s
Ǫuick sort:
Time complexity
Worst case*: \(O(n^2)\) (when the smallest or largest element is always chosen as
the pivot)
n)\)
Space complexity: \(O(\log n)\) for the stack space in recursive calls
- Merits**:
- Very efficient on average for large datasets.
- Works well with in-place partitioning.
- Has a small overhead, so it’s faster in practice than merge sort in many cases.
- **Demerits**:
- Worst-case performance can be quadratic if poor pivot choices are made.
- Not a stable sort.
- Recursive, which could lead to stack overffow on very large arrays.
- **Description**: Heap sort builds a binary heap from the array and
repeatedly extracts the maximum (or minimum) element from the
heap, rebuilding the heap as necessary until the array is sorted.
- **Time Complexity**:
- **Worst Case**: \(O(n \log n)\)
- **Best Case**: \(O(n \log n)\)
- **Average Case**: \(O(n \log n)\)
- **Space Complexity**: \(O(1)\) (in-place sorting)
- **Merits**:
- Guarantees \(O(n \log n)\) time complexity regardless of input.
- In-place sorting algorithm with no need for additional memory.
- Efficient for large datasets.
- **Demerits**:
- Not a stable sort.
- Slightly slower than quick sort on average due to the overhead of maintaining the
heap structure.
- More complex to implement compared to simpler sorting algorithms.
Divide: Split the problem into smaller sub problems. Conquer: Solve the sub
problems recursively. If the sub problems are small enough, solve them directly.
Combine: Combine the solutions of the sub problems to form the solution to the
original problem.
The Merge Sort algorithm divides the dataset into smaller parts, recursively sorts each
part, and then merges the sorted parts to form the final sorted dataset. The dataset
8, 2, S, 7, 1, 5, 4 is sorted into 1, 2, 4, 5, 7, 8, S using the divide and conquer strategy
of the Merge Sort algorithm.
St->op1->op2->e
8. Write an algorithm and the corresponding flowchart to
compute the root of a quadratic equation
.A: 70
– 100
B: c0
– cS
C: 50
– 5S
D: 45
– 4S
E: 40
– 44
F: 0 – 3S
Algorithm:
.Start
. Input: Number of students N.
.Loop: For I = 1 to N, do the following:
.Input the student’s name and marks obtained.
.Determine the grade based on the marks:
If marks >= 70 then grade = ‘A’
Else If marks >= c0 then grade
= ‘B’ Else If marks >= 50 then
grade = ‘C’ Else If marks >= 45
then grade = ‘D’ Else If marks
>= 40 then grade = ‘E’ Else
grade = ‘F’
Store the student's name, marks, and grade.
.Output: Display the list of students with their corresponding marks and
grades.
End.
11. Write a comprehensive note on how the various symbols of the ffowchart are
used in modeling developed systems in question 7, 8 3 and 10.
.
.Oval (Terminator) – Start/End:Usage: The oval symbol is used to
indicate the beginning and the end of the process.
. Application:Ǫuestion 7 (Summing and Averaging Numbers): The process
starts with initializing the sum and ends after calculating the average and
displaying the result.
. Ǫuestion 8 (Finding Maximum and Minimum of N Numbers): The process
starts with input and ends after displaying the maximum and minimum
values.
. Application: This symbol may not be necessary for simpler flowcharts like the
ones in questions 7-10 but is crucial for more complex systems where the
flowchart might be broken up.
Question 7 (Summing and Averaging Numbers): The flowchart starts with an oval
for “Start,” uses parallelograms for inputs and outputs, rectangles for summing
and averaging processes, and ends with an oval.
. Question 9 (Computing Squares): The flowchart uses ovals for start and end,
parallelograms for inputting numbers and outputting results, and rectangles for the
process of squaring each number.
Syntax Errors
• Description: These errors occur when the code violates the grammatical rules of the
programming language. For example, missing a semicolon in languages like C++ or
Java, or incorrect indentation in Python.
Logic Errors
• Description: These errors occur when the program runs without crashing but
produces incorrect results. Logic errors happen due to mistakes in the algorithm or
incorrect assumptions in the code.
• Description: Time complexity is the upper bound on the time an algorithm takes to
run as the size of the input grows. This is usually expressed using Big-O notation
(e.g., O(n), O(n²), O(log n), etc.). It represents the worst-case scenario for how long
an algorithm will take to complete.
• Description: Space complexity refers to the upper bound on the memory usage of an
algorithm. It describes how much additional memory the algorithm requires in
relation to the size of the input.
ANSWER:
a. Pass by Value
• Description: In this method, a copy of the actual parameter's value is passed to the
function. Changes made to the parameter inside the function do not affect the original
variable outside the function.
• Behavior: The function receives a copy of the argument, so any modifications to the
parameter inside the function are local to that function.
b. Pass by Reference
• Description: In this method, the reference (or address) of the actual parameter is
passed to the function. Changes made to the parameter inside the function affect the
original variable because both the original variable and the parameter point to the
same memory location.
• Behavior: The function modifies the original object or variable directly.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
print(row)
OUTPUT;
1 [1, 2, 3]
2 [4, 5, 6]
3 [7, 8, 9]
5. With the aid of python program differentiate between a
function and recursion
ANSWER:
A function and recursion are two fundamental concepts in programming, and they
are often confused with each other. In this response, I'll explain the difference
between a function and recursion in Python, with examples to illustrate each
concept.
A Function in Python
A function in Python is a block of code that can be executed multiple times from
different parts of a program. It is a self-contained piece of code that takes in
arguments, performs some operations, and returns a value. A function has a name,
and it can be called multiple times in a program.
def greet(name):
ANSWER:
In Python, variables can be classified into different types based on their data type (like
integers, floats, strings, lists, dictionaries, etc.), scope (local vs. global), and mutability
(mutable vs. immutable). Here's how you can demonstrate the usage of different types of
variables in a Python program:
• Global variables are declared outside of functions and can be accessed inside any function.
• Local variables are declared inside functions and are only accessible within that function.
• Mutable variables (like lists and dictionaries) can be modified after their creation.
• Immutable variables (like integers, strings, and tuples) cannot be modified once created.
• Variables can store data of different types, such as integers, floats, strings, lists, tuples,
dictionaries, and sets.
# Global Variable
global_var = 100
# Function demonstrating Local vs. Global variables
def show_local_global():
local_var = 50 # Local Variable
print("Inside function (local_var):", local_var)
print("Inside function (global_var):", global_var) # Accessing global variable inside
function
# Mutable Variable (List)
mutable_list = [1, 2, 3]
# Function demonstrating mutable variable behavior
def modify_mutable():
mutable_list.append(4) # Modifying the global mutable list
print("Inside function (modified mutable_list):", mutable_list)
# Immutable Variable (Integer)
immutable_var = 10
data_types()
OUTPUT:
Global Variable (global_var): 100
Inside function (local_var): 50
Inside function (global_var): 100
OUTPUT:
Enter the first number: 10
Enter the second number: 20
Enter the third number: 30
The average of 10.0, 20.0, and 30.0 is: 20.0
ANSWER:
# Function to print the days of the week
def print_days_of_week():
days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"]
for day in days_of_week:
print(day)
OUTPUT:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
10. Write an extensive notes on various control structure
in python support with python code
ANSWER:
a. . Conditional Statements
Conditional statements allow you to execute certain code blocks based on conditions.
a. if Statement
b. if-else Statement
• Description: Executes one block of code if its condition evaluates to True, otherwise
executes another block of code.
if-elif-else Statement
2. Loops
a. for Loop
• Description: Iterates over a sequence (such as a list, tuple, or string) and executes a
block of code for each item in the sequence.
b. while Loop
c. break Statement
d. continue Statement
• Description: Skips the remaining code inside the nearest enclosing loop for the
current iteration and proceeds to the next iteration.
• Description: Executes a block of code after the loop completes, but not if the loop
was terminated by a break statement.
3. Exception Handling
Exception handling allows you to manage errors gracefully during program execution.
a. try-except Block
• Description: Catches and handles exceptions that occur in the try block.
b. try-except-else Block
c.try-except-finally Block