Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 47

SRI KRISHNA COLLEGE OF ENGINEERING AND TECHNOLOGY

Kuniamuthur, Coimbatore, Tamilnadu, India


An Autonomous Institution, Affiliated to Anna University,
Accredited by NAAC with “A” Grade & Accredited by NBA (CSE, ECE, IT, MECH ,EEE, CIVIL& MCT)

Department of M.Tech Computer Science & Engineering

COURSE : DESIGN AND ANALYSIS OF ALGORITHMS

MODULE :Fundamentals of Algorithm Analysis


TOPICS : Brute Force Apporach-Selection Sort
FACULTY :Mr.S.SREERAJ M.E.,
Assistant Professor / M.Tech CSE

www.skcet.ac.in
BRUTE FORCE

• Brute force is a straightforward approach to


solving a problem, usually directly based on the
problem statement and definitions of the
concepts involved.
• The “force” implied by the strategy’s definition
is that of a computer and not that of one’s
intellect.
• “Just do it!” would be another way to describe
the prescription of the brute-force approach.

04/07/2023 2
CONT…

• And often, the brute-force strategy is indeed the


one that is easiest to apply.
• As an example, consider the exponentiation
problem: compute an for a nonzero number a
and a nonnegative integer n.
• Although this problem might seem trivial, it
provides a useful vehicle for illustrating several
algorithm design strategies, including the brute
force.

04/07/2023 3
CONT…

• Also note that computing an mod m for some


large integers is a principal component of a
leading encryption algorithm.
• By the definition of exponentiation,

• This suggests simply computing an by


multiplying 1 by an times.

04/07/2023 4
CONT…

• Brute Force Algorithms are exactly what they


sound like – straightforward methods of solving
a problem that rely on sheer computing power
and trying every possibility rather than advanced
techniques to improve efficiency.
• Brute force approach can also be called as
exhaustive search.
• Basically brute force means you go through all
the possible solutions.

04/07/2023 5
CONT…

• It is one of the easiest way to solve a problem.


• But in terms of time and space complexity will
take a hit.

04/07/2023 6
EXAMPLES

• Sequential search
• String matching algorithm
• Travelling sales man problem
• Knapsack problem

04/07/2023 7
EXAMPLE

• You are given a string “s” and s pattern “p”, you


need to check if the pattern is there in the string.
• S = “prodevelopertutorial”
• P = “rial”

04/07/2023 8
…..
…..

04/07/2023 9
TYPES OF ALGORITHMS

• Sorting Algorithms
• Search Algorithms
• Compression Algorithms

04/07/2023 10
04/07/2023 11
PSUEDO CODE

• For (every possible outcome i) do


• {
• If(i is a correct solution)
• {
• Print solution i;
• (exit if you only need one solution)
• }
• }
04/07/2023 12
CLASSES OF ALGORITHMS

• Graph
• Dynamic Programming
• Sorting
• Searching
• Strings
• Math
• Computational Geometry
• Optimization
• Miscellaneous.
04/07/2023 13
SELECTION SORT

• Selection sort by scanning the entire given list


to find its smallest element and exchange it
with the first element, putting the smallest
element in its final position in the sorted list.
• Then we scan the list, starting with the second
element, to find the smallest among the last n −
1 elements and exchange it with the second
element, putting the second smallest element in
its final position.

04/07/2023 14
CONT..

• Generally, on the ith pass through the list, which


we number from 0 to n−2, the algorithm
searches for the smallest item among the last
n−i elements and swaps it with Ai:

• After n − 1 passes, the list is sorted.

04/07/2023 15
CONT..

• Here is pseudocode of this algorithm, which, for


simplicity, assumes that the list is implemented
as an array:

ALGORITHM SelectionSort (A[0..n − 1])


//Sorts a given array by selection sort
//Input: An array A[0..n − 1] of orderable elements
//Output: Array A[0..n − 1] sorted in non
decreasing order for i ← 0 to n − 2 do

04/07/2023 16
CONT..

min ← I
for j ← i + 1 to n − 1 do
if A[j ] < A[min]
min ← j
swap A[i] and A[min]

• As an example, the action of the algorithm on


the list 89, 45, 68, 90, 29, 34, 17 is illustrated in
Figure.

04/07/2023 17
CONT…

04/07/2023 18
CONT..

• The analysis of selection sort is


straightforward.
• The input size is given by the number of
elements n; the basic operation is the key
comparison A[j ] < A[min].
• The number of times it is executed depends only
on the array size and is given by the following
sum:

04/07/2023 19
04/07/2023 Dr.D.Mansoor Hussain, AP/CSE 20
PROPERTIES

• Not stable
• O(1) extra space
• Θ(n2) comparisons
• Θ(n) swaps
• Not adaptive

04/07/2023 21
KEY

• Black values are sorted.


• Gray values are unsorted.
• A red triangle marks the algorithm position.

04/07/2023 22
CONT..

• It is a simple sorting method and it works fine for


smaller number of elements in the list.
• Selection sort is an example of Internal Sort.
• In order to sort N elements using selection sort
technique we required to perform N-1 PASS.
• During First PASS entire array is searched from
first element to find the smallest element.
• When smallest element is found, it is
interchanged with the first element in the array.

04/07/2023 23
CONT..

• Thus the element with the smallest value is


placed at first position in the array.
• During Second PASS array is searched from
second element to find the second smallest
element.
• When this element is found, it is interchanged
with the second element in the array.
• Thus the element with the second smallest value
is placed at second position in the array.

04/07/2023 24
CONT..

• During each successive PASS the smallest


element is placed at its proper position and this
process continues until all the elements in the
array are arranged in ascending order.
• During first pass we required N-1 comparisons.
• During second pass we required N-2
comparisons and during Ith pass we required N-I
comparisons.
• Thus the order of comparisons is proportional to
N2 i.e. O(N2)
04/07/2023 25
EXAMPLE

04/07/2023 26
ALGORITHM

for i = 1:n,
k=i
for j = i+1:n,
if a[j] < a[k], k = j
→ invariant: a[k] smallest of a[i..n]
swap a[i,k]
→ invariant: a[1..i] in final position
end

04/07/2023 27
04/07/2023 28
64 25 12 22 11
0 1 2 3 4
64 25 12 22 11
0 0 1 2 3
11 25 12 22 64
0 1 0 1 2
11 12 25 22 64
0 1 2 0 1
11 12 22 25 64
0 1 2 3 0
11 12 22 25 64
0 1 2 3 4
11 12 22 25 64

04/07/2023 29
How Selection Sort Works?
• 20,12,10,15,2

04/07/2023 30
04/07/2023 31
04/07/2023 32
First Iteration

04/07/2023 33
Second Iteration

04/07/2023 34
Third Iteration

04/07/2023 35
Fourth Iteration

04/07/2023 36
 Assume that the array A=[7,5,4,2] needs
to be sorted in ascending order
• At ith iteration, elements from position 0 to i-1
will be sorted.

04/07/2023 37
arr[] = 64 25 12 22 11
arr[] = 64 25 12 22 11 // Find the minimum element in
arr[0...4] and place it at beginning

11 25 12 22 64 // Find the minimum element in arr[1...4]


and place it at beginning of arr[1...4]

11 12 25 22 64 // Find the minimum element in arr[2...4]


and place it at beginning of arr[2...4]

11 12 22 25 64 // Find the minimum element in arr[3...4]


and place it at beginning of arr[3...4]

11 12 22 25 64
04/07/2023 Dr.D.Mansoor Hussain, AP/CSE 38
Time Complexity

• To find the minimum element from the array


of N elements, N−1 comparisons are required.
• After putting the minimum element in its proper
position, the size of an unsorted array reduces
to N−1 and then N−2 comparisons are required
to find the minimum element in the unsorted
array.
• Therefore (N−1) + (N−2) + ..... + 1 = (N⋅(N−1))/
2 comparisons and N swaps result in the overall
complexity of O(N2). (two nested loops).
04/07/2023 39
Auxiliary Space: O(1)

• The good thing about selection sort is it never


makes more than O(n) swaps and can be useful
when memory write is a costly operation.

04/07/2023 40
Number of comparisons: (n - 1)+(n - 2)+(n - 3)+….+1
= n(n - 1) / 2 nearly equals to n2.

Complexity = O(n2)
04/07/2023 41
Also, we can analyze the complexity by simply
observing the number of loops.
There are 2 loops so the complexity is n*n = n2.
•Worst Case Complexity: O(n2)
If we want to sort in ascending order and the array
is in descending order then, the worst case occurs.

•Best Case Complexity: O(n2)


It occurs when the array is already sorted

•Average Case Complexity: O(n2)


It occurs when the elements of the array are in
jumbled order (neither ascending nor descending).
04/07/2023 42
• The time complexity of the selection sort is the
same in all cases.
• At every step, you have to find the minimum
element and put it in the right place.
• The minimum element is not known until the end
of the array is not reached.

Space Complexity:
• Space complexity is O(1) because an extra
variable temp is used.

04/07/2023 43
Selection Sort Applications
The selection sort is used when:

•a small list is to be sorted

•cost of swapping does not matter

•checking of all the elements is compulsory

•cost of writing to a memory matters like in


flash memory (number of writes/swaps
is O(n) as compared to O(n2) of bubble sort)
04/07/2023 44
04/07/2023 45
04/07/2023 46
04/07/2023 47

You might also like