You are on page 1of 21

Analysis of Algorithms

Lecture 3

By
Dr. Waqas Haider Khan Bangyal
1
Brute Force Algorithms:

Searching:
 Linear Searching ( home task)
 Sorting
Bubble Sort
Selection Sort
Insertion Sort ( home task)

Task-01:
Find the worst time complexity of linear search and Insertion

2
Sort
Selection Sort
Summary of Steps
Find the smallest element in the array

Exchange it with the element in the first position

Find the second smallest element and exchange it


with the element in the second position

Continue until the array is sorted

3
Selection Sort
The algorithm works as follows:

1. Find the minimum value in the list

2. Swap it with the value in the first position

3. Repeat the steps above for the remainder of the list


(starting at the second position and advancing each
time)

4
Example
Step 1

8 4 6 9 2 3 1 1 2 3 4 9 6 8

Step 2

1 4 6 9 2 3 8 1 2 3 4 6 9 8

Step 3 Step 7

1 2 6 9 4 3 8 1 2 3 4 6 8 9

Sorted Array

1 2 3 9 4 6 8 1 2 3 4 6 8 9

5
Selection Sort
Alg.: SELECTION-SORT(A)
n ← length[A] // c1=1
for j ← (1) to (n – 1) // c2= (n-1)+1=n-1+1=n
{
smallest ← j // c3= n-1
for i ← ( j + 1) to (n ) // c4= (n+1-j) * (n-1)
// loop2 * loop1
{
if A[i] < A[smallest] //c5=(n-1)*(n-j)
// loop1*loop2
then smallest ← I // c6= (n-1)*(n-j)
}
exchange A[j] ↔ A[smallest] // c7= n-1
6 }
Selection Sort
Alg.: SELECTION-SORT(A)
n ← length[A] // C1= 1

for j ← (1) to (n – 1) // C2= ( n-1)+1= n


{
smallest ← j // C3= n-1
for i ← (j + 1) to (n ) // c4= ( n-j+1)* (n-1)
loop2 * loop1
{
if A[i] < A[smallest] // c5= (n-1)*(n-j)
// loop1*loop2
then smallest ← I // c6= (n-1)*(n-j)

}
exchange A[j] ↔ A[smallest] // c7= (n-1)
}
7
Loop2
For i= (J+1) to (n) // (n+1-j)
lower bound to upper bound

For i= (3) to (10)


For i= (2+1) to (10) // (10+1) // [(10+1)-2]=9
i= (3,4,5,6,7,8,9,10), 11

8
9
Selection Sort

10
Selection Sort

11
Selection Sort

12
Example
Dry Run the Selection-Sort algorithm on the following
example

7, -5, 2, 16, 4

13
Selection Sort
Worst case

О(n2)
Best case

О(n2)
Average case
О(n2)

14
Selection Sort

Pros:
 Simple and easy to implement.

• Cons:
 Inefficient for large lists,
 so similar to the more efficient insertion sort that
the insertion sort should be used in its place.

15
Sorting
 Insertion sort
 Design approach: incremental
 Sorts in place: Yes
 Best case: (n)
 Worst case:
(n2)

 Bubble Sort
incremental
 Design approach:
 Sorts in place:
Yes
(n2)
 Running time:

16
Sorting
 Selection sort
 Design approach: incremental
 Sorts in place: Yes
 Running time:
(n2)

17
Analysis of algorithm(revision)

It isn’t sufficient that our algorithms perform


the required tasks.

We want them to do so efficiently, making the


best use of
Space
Time

18
Time and Space
Time

Instructions take time.

How fast does the algorithm perform?

 Space

Data structures take space.

What kind of data structures can be used?

19
Selection Sort Program Implementation

int i, j;
int min, temp;
 
for (i = 0; i < array_size-1; i++)
{
min = i;
for (j = i+1; j < array_size; j++)
{
if (array [j] < array [min])
min = j;
}
temp = array [i];
array [i] = array [min];
array [min] = temp;
}

20
21

You might also like