Lecture 03 - Using Brute Force
Lecture 03 - Using Brute Force
Solving
Lecture 03 – Problem Solving
using Brute Force Strategy
yes
move to next element ?
no
done
Selection Sort
ALGORITHM SelectionSort(A[0..n - 1])
//The algorithm 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 ascending order
for i 0 to n - 2 do
min i
for j i + 1 to n - 1 do
if A[j ]<A[min] min j
swap A[i] and A[min]
Bubble Sort
• Compare adjacent elements of the list
• and exchange them if they are out of
order
• Then we repeat the process
• By doing it repeatedly, we end up
‘bubbling up’ the largest element to the
last position on the list
Bubble Sort
ALGORITHM BubbleSort(A[0..n - 1])
//The algorithm sorts array A[0..n - 1] by bubble sort
//Input: An array A[0..n - 1] of orderable elements
//Output: Array A[0..n - 1] sorted in ascending order
for i 0 to n - 2 do
for j 0 to n - 2 - i do
if A[j + 1]<A[j ] swap A[j ] and A[j + 1]
Searching – Sequential Search