ALGORITHMS
Heuristic algorithms
& method of sorting
By
Dr Hjh Rahmah bt Murshidi
Mohamad Iskandar Shah Sitam
HEURISTIC ALGORITHMS
Heuristic algorithm is a method of
problem solving using exploration, trial
and error methods
May not lead to the best or optimal
solution of the problem
Heuristic algorithms
for Bin-Packing
People move belongings need efficient packing
With odd sizes belongings, hard to formulate
algorithm for packing efficiently
Also arises in copying computer files of varying
sizes to diskettes
What is the fewest number of diskettes
required?
It depends on how efficient the memory space
in each diskette is fully utilised
Heuristic algorithms
First-fit Algorithm
First-fit decreasing Algorithm
Full bins
First-fit Algorithm
Also sometimes called greedy algorithm.
Provides a fast but often non-optimal solution
Taking the boxes in the order listed, place
the next box to be packed in the first
available slot that can take the box in the bin
First-fit Algorithm
Each box is selected and attempts made to
place it in the first bin.
If this fails then the second bin is tried.
If this fails the next bin is tried and so on until
eventually the element is placed into a bin.
The result is not necessarily optimal.
First-fit Decreasing Algorithm
The simplest heuristic algorithms for solving the
bin packing problem.
Operates by first sorting the items to be inserted
in decreasing order by volume, inserting each
item into the first bin in the list with sufficient
remaining space.
First sorting the list of elements into decreasing
order, does not guarantee an optimal solution,
and, particularly for longer list, can increase the
time taken to implement the algorithm.
Full bins Algorithms
Look for combinations of boxes to fill bins.
Pack these boxes.
For the remainder, place the next box to be
packed in the first available slot that can take
that box
Required a lot of different combinations of
boxes to be checked if they make full bins
EXAMPLE 1
Eleven boxes to be packed into crates
Each crate must weight not more than
100kg. There are three boxes of weight
50 kg, three of weight 40 kg, three of
weight 30 kg and two of weight 20 kg.
1) Apply first fit decreasing algorithms
and state number of crates used
2) Apply full-bin algorithm and state the
number of crates used
Applying First fit Decreasing
Algorithms
Rearrange the boxes in decreasing size
50 50 50 40 40 40 30 30 30 20 20
Applying First fit Decreasing
Algorithms
Stack the boxes in decreasing weight into
crates on condition no crates exceed 100kg
30
50 40 40 30 20
50 50 40 30 20
Crate 1 Crate 2 Crate 3 Crate 4 Crate 5
5 crates required
Applying Full-Bin Algorithms
Stack the boxes in decreasing weight into
crates on condition no crates exceed 100kg
20 20 30
50 30 40 30
50 50 40 40
Crate 1 Crate 2 Crate 3 Crate 4
4 crates required
EXERCISE
There are 11 boxes, A to K with heights (in cm) as
follows
A B C D E F G H I J K
8 7 4 9 6 9 5 5 6 7 8
The rack is 15 cm high and you are asked to stack
boxes one on top of the other using few slots as
possible
State all the steps that you take.
SORTING ALGORITHMS
Algorithm that put elements of a list in a
certain order
The most used orders are numerical
order and lexicographical order
Efficient sorting is important to optimize
the use of other algorithm
Methods of sorting
Interchange sort
Bubble sort
Shuttle sort
Quick sort
Interchange Sort
Any sorting algorithm that is based on
swapping.
In this algorithm, the smallest number
in the list is found and interchanged
with the first number.
Then the smallest number excluding
the first is found and interchanged with
the second number.
This process continues until the list is
sorted.
EXAMPLE 1 (Interchange sort)
7 1 1 1 1 1 1 1
5 5 2 2 2 2 2 2
2 2 5 3 3 3 3 3
4 4 4 4 4 4 4 4
10 10 10 10 10 5 5 5
1 7 7 7 7 7 6 6
6 6 6 6 7 7
6 6
3 3 5 10 10 10
3 5
Original 1st 2nd 3rd
4th 5th 6th 7th
list pass pass pass pass pass pass pass
EXAMPLE 1 (Interchange sort)
7 1 1 1 1 1 1 1
5 5 2 2 2 2 2 2
2 2 5 3 3 3 3 3
4 4 4 4 4 4 4 4
10 10 10 10 10 5 5 5
1 7 7 7 7 7 6 6
6 6 6 6 7 7
6 6
3 3 5 10 10 10
3 5
Original 1st 2nd 3rd
4th 5th 6th 7th
list pass pass pass pass pass pass pass
Bubble Sort
The algorithm gets its name from the way
smaller elements "bubble" to the top (i.e.
the beginning) of the list via the swaps.
Because it only uses comparisons to
operate on elements, it is a
comparison sort.
Although simple, this algorithm is highly
inefficient and is rarely used except in
education.
Bubble Sort
A straightforward and simplistic method of sorting
data that is used in computer science education.
The algorithm starts at the beginning of data set.
It compares the first two elements, and if the first
is greater than the second, it swaps them.
Continues for each pair of adjacent elements to
the end of data set.
Starts again with first two elements, repeating until
no swaps have occurred on the last pass.
EXAMPLE 2 (Bubble sort)
7 5 5 5 5 5 5 5
5 7 2 2 2 2 2 2
2 2 7 4 4 4 4 4
4 4 4 7 7 7 7 7
10 10 10 10 10 1 1 1
1 1 1 1 1 10 6 6
6 6 6 6 10 3
6 6
3 3 3 3 3 10
3 3
Original 1st
list pass
EXAMPLE 2 (Bubble sort)
5 2 2 2 2 2 2
2 5 4 4 4 4 4
4 4 5 5 5 5 5
7 7 7 7 1 1 1
1 1 1 1 7 6 6
6 6 6 6 6 7 3
3 3 3 3 7
3 3
10 10 10 10 10
10 10
2nd
pass
EXAMPLE 2 (Bubble sort)
2 2 2 2 2 2 2
4 4 4 4 4 4 4
5 5 5 1 1 1 1
1 1 1 5 5 5 5
6 6 6 6 6 3 3
3 3 3 3 3 6 6
7 7 7 7 7
7 7
10 10 10 10 10
10 10
3rd
pass
EXAMPLE 2 (Bubble sort)
2 2 2 2 2
4 4 1 1 1
1 1 4 4 4
5 5 5 5 3
3 3 3 3 5
6 6 6 6 6
7 7 7
7 7
10 10 10
10 10
4th
pass
EXAMPLE 2 (Bubble sort)
2 1 1
1 2 2
4 4 3
3 3 4
5 5 5
6 6 6
7 7
7
10 10
10
5th
pass
Shuttle Sort
Repeatedly taking the next item and inserting
it into the final data structure in its proper
order with respect to items already inserted.
Known as insertion sort.
First, the first two numbers are compared
and swap if necessary to place in ascending
order.
Process is continued until numbers are in the
ascending order.
Shuttle Sort
1st pass:
Compare first and second number in the list. Swap if
necessary to place the number in ascending order.
2nd pass:
Compare 2nd and 3rd number and swap if
necessary. Then compare 1st and 2nd and swap if
necessary
3rd pass:
Compare 3rd and 4th number and swap if necessary.
Compare 2nd and 3rd number and swap if
necessary. Then compare 1st and 2nd and swap if
necessary
EXAMPLE 3 (Shuttle sort)
7 5 5 2 2 2
5 7 2 5 5 4
2 2 7 7 4 5
4 4 4 4 7 7
10 10 10 10 10 10
1 1 1 1 1 1
6 6 6 6
6 6
3 3 3 3
3 3
Original 1st 2nd
list pass pass
EXAMPLE 3 (Shuttle sort)
2 2 2 2 2 2 1
4 4 4 4 4 1 2
5 5 5 5 1 4 4
7 7 7 1 5 5 5
10 10 1 7 7 7 7
1 1 10 10 10 10 10
6 6 6 6
6 6 6
3 3 3 3
3 3 3
3rd 4th
pass pass
EXAMPLE 3 (Shuttle sort)
1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2
4 4 4 4 4 4 4 3
5 5 5 5 5 5 3 4
7 7 6 6 6 3 5 5
10 6 7 7 3 6 6 6
6 10 10 7 7
3 7 7
3 3 3 10 10
10 10 10
5th 6th 7th
pass pass pass
Quick Sort
A well-known sorting algorithm developed by
C. A. R. Hoare.
A comparison sort.
Sorts by employing a divide and conquer
strategy to divide a list into two sub-lists.
Pick an element, called a pivot, from the list.
Quick Sort
First split the list into two sub-lists:
(1) Number less than or equal to first number
in the list
(2)Number greater than the first number in
the list
Place the first number between the two sub-
lists
Repeat the process on sub-lists containing
two or more numbers until there are no sub-
lists
EXAMPLE 4 (Quick sort)
7 5 2 1 1
5 2 4 2 2
2 4 1 4 3
4 1 3 3 4
10 6 5 5 5
1 3 6 6 6
6 7 7
7 7
3 10 10
10 10
Original
list
THANK YOU
ALL THE BEST FOR FINAL
EXAMINATION
Proud to be a teacher