Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

INFORMATICS

Search
3.9 SORTING
ALGORITHMS 1
Home

Library

You will:
 implement sorting algorithms to solve
practical problems.
3.9 SORTING ALGORITHMS
1
01 Bubble 02 Merge
Sort Sort
Insertion Selectio
03 04
Sort n Sort
Bubble Sort
It is a comparison-based algorithm in which
each pair of adjacent elements is compared and the
elements are swapped if they are not in order.
Bubble Sort
def bubblesort(list):
for i in range(0,len(list)-1):
for j in range(i + 1, len(list)):
if list[i]>list[j]:
list[i], list[j]= list[j], list[i]
list = [19,2,31,45,6,11,121,27]
bubblesort(list)
print(list)
Merge Sort
Merge sort first divides the array into
equal halves and then combines them in a sorted
manner.
def merge_sort(unsorted_list):
if len(unsorted_list) <= 1:
return unsorted_list
middle = len(unsorted_list) // 2
left_list = unsorted_list[:middle]
right_list = unsorted_list[middle:]
left_list = merge_sort(left_list)
right_list = merge_sort(right_list)
return list(merge(left_list, right_list))
def merge(left_half,right_half ):
res = []
while len(left_half ) != 0 and len(right_half ) != 0:
if left_half[0] < right_half[0]:
res.append(left_half[0])
left_half.remove(left_half[0])
else:
res.append(right_half[0]) right_half.remove(right_half[0])
if len(left_half ) == 0:
res = res + right_half
else: res = res + left_half
return res
unsorted_list = [64, 34, 25, 12, 22, 11, 90]
print(merge_sort(unsorted_list))
Step of a merge sort algortihm:

Left/Right: [34] [25]

Res : [25, 34]

Left/Right: [64] [25, 34]

Res : [25, 34, 64]

Left/Right: [12] [22]

Res : [12, 22]

Left/Right: [11] [90]

Res : [11, 90]

Left/Right: [12, 22] [11, 90]

Res : [11, 12, 22, 90]

Left/Right: [25, 34, 64] [11, 12, 22, 90]

Res : [11, 12, 22, 25, 34, 64, 90]


INFORMATICS
Search
3.10 COMPARING
ALGORITHMS
Home

Library

You will:
 compare an algorithms.
Algorithm 1 Algorithm 2 Algorithm 3
arr = [2, 4, -3, 5, 2, -5, 2] arr = [2, 4, -3, 5, 2, -5, 2] arr = [2, 4, -3, 5, 2, -5, 2]
best = 0 best = 0 best = 0
n = len(arr) n = len(arr) n = len(arr)
for a in range(n):
for a in range(n): s=0
for b in range(n):
s=0 for k in range(n):
s = 0;
for b in range(n): s = max(arr[k], s + arr[k])
for k in range(a, b):
s += arr[b] best = max(best, s)
s += arr[k]
best = max(best, s) print(best)
best = max(best, s)
print(best)
print(best)
3.10 COMPARING
ALGORITHMS
The comparison shows that all algorithms are efficient when the input size is small, but larger inputs
bring out remarkable differences in the running times of the algorithms. Algorithm 1 becomes slow when n
= 104, and Algorithm 2 becomes slow when n = 105. Only Algorithm 3 is able to process even the largest
inputs instantly.
3.11 GRAPHS
YOU WILL:
 implement search algorithms on
graphs to solve practical problems.
3.11 GRAPHS
A graph data structure is a collection of nodes that have data and are connected to other nodes. More
precisely, a graph is a data structure (V,E) that consists of
 A collection of vertices V
 A collection of edges E, represented as ordered pairs of vertices (u,v)
Graph Operations
The most common graph operations are:
• Check if element is present in graph
• Graph Traversal
• Add elements(vertex, edges) to graph
• Finding path from one vertex to another
Glossary
1. Vertex (node): used to represent a single data point
2. Edge: a connection between a pair of vertices
3. Neighbor: a neighbor node is a node that is directly connected to another node by an edge
4. Directed graph: a graph in which all edges have direction
5. Undirected graph: a graph in which all edges have no direction
6. Path: a sequence of vertices connected by edges
7. Cycle: a path that begins and ends at the same vertex
8. Cyclic graph: a graph which contains at least one cycle
9. Acyclic graph: a graph which does not contain a cycle
10. Adjacency list: an approach to representing graphs in which each node stores a list of its adjacent vertices
11. Edge set/list: an approach to representing graphs in which a graph is a collection of all its edges
12. Adjacency matrix: an approach to representing graphs in which a graph with n nodes is storeed as an n by n boolean
matrix, where matrix[u][v] is true if an edge exists between node u to node v.
3.12 DFS
ALGORITHM
YOU WILL:
 implement search algorithms on
graphs to solve practical problems.
3.12 DFS ALGORITHM
Traversal means visiting all the nodes of a graph. Depth first traversal or Depth first Search is a recursive
algorithm for searching all the vertices of a graph or tree data structure.
A standard DFS implementation puts each vertex of the graph into one of two categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles. The DFS algorithm
works as follows:
1. Start by putting any one of the graph's vertices on top of a stack.
2. Take the top item of the stack and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the top of
stack.
4. Keep repeating steps 2 and 3 until the stack is empty.
3.13 BFS ALGORITHM
YOU WILL:
 implement search algorithms on
graphs to solve practical problems.
3.13 BFS ALGORITHM
Traversal means visiting all the nodes of a graph. Breadth first traversal or Breadth first Search is a recursive
algorithm for searching all the vertices of a graph or tree data structure.
A standard DFS implementation puts each vertex of the graph into one of two categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
The algorithm works as follows:
1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the back of the queue.
4. Keep repeating steps 2 and 3 until the queue is empty. The graph might have two different disconnected parts so to
make sure that we
3.13 BFS ALGORITHM
Traversal means visiting all the nodes of a graph. Breadth first traversal or Breadth first Search is a recursive
algorithm for searching all the vertices of a graph or tree data structure.
A standard DFS implementation puts each vertex of the graph into one of two categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
The algorithm works as follows:
1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the back of the queue.
4. Keep repeating steps 2 and 3 until the queue is empty. The graph might have two different disconnected parts so to
make sure that we cover every vertex, we can also run the BFS algorithm on every node

You might also like