0% found this document useful (0 votes)
16 views11 pages

877b345e Dcf0 4f13 9be7 47ac2daceb92 Algorithm

The document outlines various searching and sorting algorithms, including Linear Search, Binary Search, Bubble Sort, and Merge Sort, detailing their mechanisms, advantages, and disadvantages. It also covers fundamental programming concepts such as data types, arithmetic operations, relational operations, and boolean operations, providing examples for clarity. Comparisons between algorithms highlight their efficiency and use cases, emphasizing the importance of choosing the right approach based on data characteristics.

Uploaded by

totofoc204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

877b345e Dcf0 4f13 9be7 47ac2daceb92 Algorithm

The document outlines various searching and sorting algorithms, including Linear Search, Binary Search, Bubble Sort, and Merge Sort, detailing their mechanisms, advantages, and disadvantages. It also covers fundamental programming concepts such as data types, arithmetic operations, relational operations, and boolean operations, providing examples for clarity. Comparisons between algorithms highlight their efficiency and use cases, emphasizing the importance of choosing the right approach based on data characteristics.

Uploaded by

totofoc204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Algorithm:

3.1.3 Searching Algorithms

Linear Search
How it works:
Linear search checks each element of a list one by one to find a target value.

Steps:

1. Start with the first element.

2. Compare the target value with the current element.

3. If they match, the target is found.

4. If not, move to the next element.

5. Continue until the end of the list.

Example (Searching for 5 in [1, 2, 3, 5, 7]):

Compare 5 with 1 → No match.

Compare 5 with 2 → No match.

Compare 5 with 3 → No match.

Compare 5 with 5 → Match found!

Advantages:

Simple to implement.

Works for unsorted lists.

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:

1. Start with the middle element of the list.

2. Compare the target value with the middle element.

3. If they match, the target is found.

4. If the target is smaller, search the left half of the list.

5. If the target is larger, search the right half of the list.

6. Repeat until the target is found or the range becomes empty.

Example (Searching for 5 in [1, 2, 3, 5, 7]):

Middle is 3 → Compare 5 with 3 → 5 is larger → Search right half [5, 7].

Middle is 5 → Match found!

Advantages:

Very efficient for large sorted lists (reduces search space by half in each
step).

Disadvantages:

Requires the list to be sorted beforehand.

Comparison: Linear Search vs. Binary Search


Feature Linear Search Binary Search

Works on unsorted list? Yes No

Efficiency Slower for large lists Faster for large sorted lists

Implementation Simple Requires more steps (sorting)

3.1.4 Sorting Algorithms

Bubble Sort
How it works:

Algorithm: 2
Bubble sort repeatedly compares adjacent elements and swaps them if they are in
the wrong order.

Steps:

1. Start with the first element.

2. Compare it with the next element.

3. Swap them if they are in the wrong order.

4. Repeat for all elements.

5. Repeat the process until no swaps are needed.

Example (Sorting [5, 3, 8, 4]):

Pass 1: [3, 5, 4, 8] → Swapped 5 and 3, then 8 and 4.

Pass 2: [3, 4, 5, 8] → Swapped 5 and 4.

Advantages:

Simple to implement.

Works well for small lists.

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:

1. Divide the list into two halves.

2. Keep dividing each half until each sublist has one element.

3. Merge the sublists back together in sorted order.

Example (Sorting [5, 3, 8, 4]):

Divide: [5, 3] and [8, 4].

Algorithm: 3
Sort each half: [3, 5] and [4, 8].

Merge: [3, 4, 5, 8].

Advantages:

Efficient for large lists.

Works faster than bubble sort in most cases.

Disadvantages:

Requires additional memory for merging.

Comparison: Bubble Sort vs. Merge Sort

Feature Bubble Sort Merge Sort

Efficiency (large list) Inefficient Efficient

Memory use Minimal Requires extra memory

Implementation Simple More complex

Best for Small lists Large lists with lots of data

3.2.1 Data Types

Understanding Data Types


A data type defines the kind of value a variable can hold. Using the correct data
type helps optimize memory and ensures that operations are valid.

Common Data Types

Data Type Description Example

Integer Whole numbers (positive, negative, zero) 5 , -20 , 0

Real Numbers with fractional parts (float/double) 3.14 , -5.67

Boolean True/False values True , False

Character Single characters enclosed in quotes 'A' , '1'

String Sequence of characters enclosed in quotes "Hello" , "12345"

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!

3.2.2 Programming Concepts

Common Statement Types


1. Variable Declaration and Assignment:

Assign values to variables.

Example:

= 10 # Variable declaration and assignment

2. Constant Declaration:
Assign values that should not change.

Example:

PI = 3.14 # Constant declaration

3. Iteration:

Repeating code using loops.


Example:

Definite (Count Controlled):

python
CopyEdit

Algorithm: 6
for i in range(1, 6): # Runs 5 times
print(i)

Indefinite (Condition Controlled):

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):

Break code into reusable parts.


Example:

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}")

Nested Selection Example:

python
CopyEdit
if game_won:
if score > high_score:
print("Congratulations!")

Meaningful Identifier Names


Use clear and descriptive names for variables, constants, and functions to make
code understandable.

Bad:

python
CopyEdit
x = 10

Good:

python
CopyEdit

Algorithm: 8
age = 10

3.2.3 Arithmetic Operations

Basic Operations
Operation Symbol Example Output

Addition + 5 + 3 8

Subtraction - 5 - 3 2

Multiplication * 5 * 3 15

Real Division / 5 / 2 2.5

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

3.2.4 Relational Operations

Operation Symbol Example Output

Equal to == 5 == 5 True

Not equal to != 5 != 3 True

Algorithm: 9
Less than < 3 < 5 True

Greater than > 5 > 3 True

Less than or equal to <= 3 <= 3 True

Greater than or equal to >= 5 >= 3 True

Example

python
CopyEdit
a = 10
b = 5
if a > b:
print("a is greater than b")

3.2.5 Boolean Operations

Operators
Operation Symbol Example Output

NOT not not True False

AND and True and False False

OR or True or False True

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy