Professional Documents
Culture Documents
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
3
Selection Sort
The algorithm works as follows:
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
}
exchange A[j] ↔ A[smallest] // c7= (n-1)
}
7
Loop2
For i= (J+1) to (n) // (n+1-j)
lower bound to upper bound
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)
18
Time and Space
Time
Space
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