0% found this document useful (0 votes)
0 views

Chapter 3.1-3.2 Simple Algorithms

Capitulo completo en español

Uploaded by

landalalo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Chapter 3.1-3.2 Simple Algorithms

Capitulo completo en español

Uploaded by

landalalo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction to Computer Science and Programming Using Python

Chapter 3.1-3.2: Simple Algorithms

3.1 Simple Algorithms: Introduction

In this section, we explore the concept of simple algorithms. An algorithm is a step-by-step

procedure for solving a problem or accomplishing a task. We will discuss the following:

1. Definition of an algorithm

2. Characteristics of a good algorithm

3. Examples of simple algorithms

4. Implementing algorithms in Python

Example: Finding the Maximum of a List

One of the simplest algorithms is finding the maximum value in a list of numbers. Here is a

step-by-step algorithm to achieve this:

1. Initialize a variable 'max_value' to the first element of the list.

2. Iterate through each element in the list.

3. For each element, check if it is greater than 'max_value'.

4. If it is, update 'max_value' with this element.

5. After the loop ends, 'max_value' will hold the maximum value in the list.

Python implementation:

def find_max(numbers):

max_value = numbers[0]

for number in numbers:

if number > max_value:

max_value = number
return max_value

# Example usage

numbers = [3, 41, 12, 9, 74, 15]

print("The maximum value is:", find_max(numbers))

3.2 More on Simple Algorithms

In this section, we delve deeper into simple algorithms, including:

1. Sorting algorithms (e.g., bubble sort)

2. Searching algorithms (e.g., linear search)

3. Analyzing the efficiency of algorithms (Big O notation)

Example: Bubble Sort Algorithm

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. The pass through the list is repeated until

the list is sorted.

Python implementation:

def bubble_sort(numbers):

n = len(numbers)

for i in range(n):

for j in range(0, n-i-1):

if numbers[j] > numbers[j+1]:

numbers[j], numbers[j+1] = numbers[j+1], numbers[j]

# Example usage

numbers = [64, 34, 25, 12, 22, 11, 90]


bubble_sort(numbers)

print("Sorted list is:", numbers)

Graphical Representation of Bubble Sort

The following graph represents the number of operations (swaps) performed by the Bubble Sort

algorithm as the list size increases. It demonstrates the algorithm's O(n^2) time complexity, meaning

the time it takes to sort the list grows quadratically as the list size increases.

You might also like