You are on page 1of 14

Class 3

ENGN- 1310 Computer Programming


Today’s Agenda

01
Searching Algorithms

02
Sorting Algorithms

Course Objectives:
To understand searching and sorting algorithms
and the different ways to write them.
Searching Algorithms

What for is a searching


algorithm used ??
Searching Algorithm

Serial Binary
The search then starts with the A binary search algorithm takes
first item and then moves to the data and keeps dividing it in
each item in turn, until either a half until it finds the item it is
match is found, or it reaches the looking for, provided should be
end of the data with no match an ordered list.
found.
Example for Serial Search:

Imagine that you have a database of sales made to customers. You need to deliver the g
Flowchart
Example for Binary Search:

Imagine that you have a database of customers and want to search for the customer Joh
Flowchart
Comparison of Searches
Serial Search Binary Search
• It is a very simple algorithm. • Much quicker than a serial
• It can be used on any kind of search.
data regardless of type or • In each step, data is searched
sorted. in halves.
• It is very slow. • The data needs to be sorted
for search.
Sorting Algorithm
A sorting algorithm will put items in a list into an order, such as
alphabetical or numerical order

Bubble sort Bucket sort


• Algorithm goes through a list of • Algorithm separates a list of
data a number of times, comparing data into different collections of
two items that are side by side to data, called ‘buckets’.
see which is out of order. • Empty buckets are set up at the
• It will keep going through the list of start of the sort and are filled
data until all the data is sorted into with the relevant data.
order. • Each bucket is then sorted, and
• Each time the algorithm goes the data is finally gathered back
through the list it is called a ‘pass’. into a list.
Example for Bubble sort:
Imagine that you have a list of children who you want to sort by age, from
youngest to oldest. A bubble sort can do this. The list of ages is: 2,8,5,3,9, 4 and
1.
https://youtu.be/xli_FI7CuzA
Example for Bucket sort:
Imagine that you have a list
of people who you want to
sort by age, from youngest
to oldest. A bubble sort can
do this. The list of ages is:
41, 15, 17, 32, 18, 28, 77
and 54
Comparison of Sorting Algorithms
Bubble Search Bucket Search
• It is a very simple algorithm. • Much quicker than a bubble
• It has just two steps search.
(compare and swap). • Fewer comparisons.
• It is very slow. • Complicated algorithm.

You might also like