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

session 10,11 (algorithm)

The document discusses various algorithms, focusing on searching (linear and binary) and sorting (selection, insertion, bubble, and merge). It defines algorithms as systematic methods for problem-solving and contrasts them with programs, emphasizing the importance of Big-O notation for measuring algorithm complexity. Additionally, it provides examples and code snippets for each algorithm discussed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

session 10,11 (algorithm)

The document discusses various algorithms, focusing on searching (linear and binary) and sorting (selection, insertion, bubble, and merge). It defines algorithms as systematic methods for problem-solving and contrasts them with programs, emphasizing the importance of Big-O notation for measuring algorithm complexity. Additionally, it provides examples and code snippets for each algorithm discussed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

by

Eng.Alaa Osama
1.

Session 10,11

Algorithms

➢ Searching and Sorting.


➢ Linear search.
➢ Binary search.
➢ Selection sort.
➢ Insertion sort.
➢ Bubble sort.
• Algorithm
• An algorithm is a well-defined sequential computational technique that accepts a value or a

collection of values as input and produces the output(s) needed to solve a problem.
• Algorithms are used to solve problems or automate tasks in a systematic and efficient manner.

They are a set of instructions or rules that guide the computer or software in performing a
particular task or solving a problem.

• Algorithms vs. programs


▪ Algorithm:-
✓ Can be performed by humans or by machines

✓ Can be expressed in any suitable language


✓ Can be on any level of abstraction

▪ Program:-
✓ Must be performed by a machine

✓ Must be expressed in a programming language


✓ Must be detailed and specific to avoid any ambiguities

▪ To employ an algorithm on a machine, it has to be transcribed into a programming language (i.


e.,coded!).

• We calculate the complexity of code using Big-O notation


▪ Big O notation describes the complexity of your code using algebraic terms.
▪ Big O is a methodology to calculate the Step Execution for any algorithm.
Fundamentals:

1- int x; X

2- int y=20; 1

3- public void max (int a, int b) X

4- {} X

5- for (int i=1 ; i<n ; i++) n-1+1 = n rule: End-Start+1

6- for (int i=1 ; i<=n ; i++) rule: End-Start+1+1(for equal)


n-1+1+1=n+1
7- return x; 1

8- cout<<x 1

Examples:

1. public int sum (int n)


{
int total; X
total= n * (n*1) /2; 1
return total;
1
}
SE = 1+1= 2 => O(1)
Note: O(50) = O(1)
O(1000)=O(1)
If C : is a constant number, then O(C) = O(1)
______________________________________________________
2. public int sum (int n) X
{
int i, total; X

total=0; 1
for (int i=1 ; i<=n ; i++) n-1+1+1=n+1
{
total=total+i; // less than the SE of for loop by 1 n
}
return total; 1
}
SE = 1+n+1+n+1 = 2n+3 => O(n)
3. int func (int a[],int n) x
{
int x=5; 1
for (int i=1; i<=n ; i++) n+1
{
for (int j=1; j<=n ; j++) n
{
n-1 *n
x=x+i+j;
cout<<x;
} n-1
}
}

SE= 1+ n+1+ n(n) + n(n-1) + n(n-1)


= 1+n+1+n2 + n2 -n + n2 -n
= 3n2 -n +2 = O(n2)

• Searching and sorting


▪ Linear search: is a very simple search algorithm. In this type of search, a

sequential search is made over all items one by one. Every item is checked and
if a match is found then that item is returned, otherwise the search continues till
the end of the data collection.
Code:
▪ Binary search: is a fast search algorithm with run-time complexity of Ο(log n).

This search algorithm works on the principle of divide and conquer. For this
algorithm to work properly, the data collection should be in the sorted form.
Example: search element is 87

Code:
Selection sort :

▪ Algorithm

- Find the minimum element from the array of size N and swap it with the

element at front

- Find the minimum element from the array of size N-1 and swap it with

element at front

- Keep going until you are working with an array of size 0

- you aredone!
Example:

Let’s apply the steps to the following list of numbers 4 -1 6 5 8 3 7 2 1 0:

▪ Code:
Insertion sort :

▪ Algorithm

- Read each element from the array of size N

- Start from left and put the element in the correct order.

- Shift elements to the right to create a space if you need.

Example:
Let’s apply the steps to the following list of numbers 4 -1 6 5 8 3 7 2 1 0:

▪ Code:
Bubble sort :

▪ Algorithm

- Compare every two adjacent numbers

- Swap if smaller comes after bigger.

Example:

Let’s apply the steps to the following list of numbers 4 -1 6 5 8 3 7 2 1 0:

▪ Code:
Merge sort :

▪ sorting algorithms is based on the principle of Divide and Conquer Algorithm.


▪ Algorithm

- dividing an array into smaller subarrays.

- sorting each subarray.

- then merging the sorted subarrays back together to form the final sorted
array.

▪ Example:

▪ Code:

You might also like