877b345e Dcf0 4f13 9be7 47ac2daceb92 Algorithm
877b345e Dcf0 4f13 9be7 47ac2daceb92 Algorithm
Linear Search
How it works:
Linear search checks each element of a list one by one to find a target value.
Steps:
Advantages:
Simple to implement.
Disadvantages:
Inefficient for large lists (takes time proportional to the size of the list).
Binary Search
How it works:
Algorithm: 1
Binary search works only on sorted lists by repeatedly dividing the search range in
half.
Steps:
Advantages:
Very efficient for large sorted lists (reduces search space by half in each
step).
Disadvantages:
Efficiency Slower for large lists Faster for large sorted lists
Bubble Sort
How it works:
Algorithm: 2
Bubble sort repeatedly compares adjacent elements and swaps them if they are in
the wrong order.
Steps:
Advantages:
Simple to implement.
Disadvantages:
Inefficient for large lists (takes time proportional to the square of the list size).
Merge Sort
How it works:
Merge sort divides the list into smaller sublists, sorts them, and then merges them
back together in sorted order.
Steps:
2. Keep dividing each half until each sublist has one element.
Algorithm: 3
Sort each half: [3, 5] and [4, 8].
Advantages:
Disadvantages:
Algorithm: 4
Examples
1. Integer Example:
age = 25 # Integer
print(age + 5) # Output: 30
2. Real Example:
pi = 3.14 # Real
radius = 5
area = pi * radius ** 2
print(area) # Output: 78.5
3. Boolean Example:
python
CopyEdit
is_raining = False
print(is_raining) # Output: False
4. Character Example:
grade = 'A'
print(f"Your grade is {grade}") # Output: Your grade is A
5. String Example:
Algorithm: 5
name = "Alice"
print(f"Hello, {name}!") # Output: Hello, Alice!
Example:
2. Constant Declaration:
Assign values that should not change.
Example:
3. Iteration:
python
CopyEdit
Algorithm: 6
for i in range(1, 6): # Runs 5 times
print(i)
python
CopyEdit
while not_solved:
print("Working...")
not_solved = False
4. Selection:
Making decisions with if statements.
Example:
python
CopyEdit
if score > high_score:
print("New high score!")
5. Subroutines (Procedures/Functions):
python
CopyEdit
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
Algorithm: 7
Nested Selection and Iteration
Nested Iteration Example:
python
CopyEdit
for i in range(1, 4):
for j in range(1, 4):
print(f"i={i}, j={j}")
python
CopyEdit
if game_won:
if score > high_score:
print("Congratulations!")
Bad:
python
CopyEdit
x = 10
Good:
python
CopyEdit
Algorithm: 8
age = 10
Basic Operations
Operation Symbol Example Output
Addition + 5 + 3 8
Subtraction - 5 - 3 2
Multiplication * 5 * 3 15
Integer Division // 5 // 2 2
Modulus (Remainder) % 5 % 2 1
Examples
python
CopyEdit
# Arithmetic operations
a = 11
b = 2
print(a + b) # 13
print(a / b) # 5.5
print(a // b) # 5
print(a % b) # 1
Equal to == 5 == 5 True
Algorithm: 9
Less than < 3 < 5 True
Example
python
CopyEdit
a = 10
b = 5
if a > b:
print("a is greater than b")
Operators
Operation Symbol Example Output
Examples
# NOT
is_raining = False
print(not is_raining) # True
# AND
has_umbrella = True
print(is_raining and has_umbrella) # False
Algorithm: 10
# OR
print(is_raining or has_umbrella) # True
Algorithm: 11