0% found this document useful (0 votes)
5 views

Algorithm

Algorithm basic

Uploaded by

info.bptrades
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Algorithm

Algorithm basic

Uploaded by

info.bptrades
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CMPT130 J.

Ye

Searching and Sorting


Algorithms

• Searching
• Efficiency of algorithms
• Sorting
Searching
• Searching is the process of finding a target element within
a group of items called the search pool; the target may or
may not be in the search pool.
• The search pool can have different data structure. Here we
examine sequences (we use arrays).
• A searching function can return a Boolean value whether
the target is found in the array.
• A searching function can also return the index of the target
• Let's look at two classic searching approaches: linear
search and binary search
2
Searching
Linear Search
• A linear search begins at one end of an array and
examines each element in turn
• Eventually, either the item is found or the end of the array
is encountered
• Implement Linear Search function:
– the function takes an array of values and a target value as input
– the function returns true if the target value is found in the array
or false otherwise
– What about returning the index of the target value assuming
there is no duplicates ? (if not found, return -1)

3
Searching
Linear Search (cont)
• Let’s examine two different implementations of the linear
search algorithm using three different inputs.
• See Week 12 handout 1 …

4
Linear Search (cont) Searching

Conclusion:
-- It takes the longest time to complete the linear search
when the target value is not in the search pool -- we call this
“worst case” when talking about the efficiency of algorithms

-- In the worst case, the two different implementations (A


and B) have the same efficiency.
Let’s be more general:
In the worst case, for an array of size n, how many elements
have to be checked, in other words, how many times the
critical comparison operation has to be executed before
knowing that the value is not there when doing linear search?
5
Searching
Binary Search
• A binary search assumes the items in the search pool
(array) is sorted
• A binary search first examines the middle element of the
array -- if it matches the target, the search is over
• If it doesn't, the target can only be in one half of the array,
thus only that half of the array needs to be searched
• The process continues by comparing the middle element of
the remaining half …
• Eventually, the target is found or the data is exhausted
• Implement binary search algorithm
6
Searching
Binary Search (cont)
• Let’s examine binary search using three different inputs.
• See Week 12 handout 2…

• Questions:

In the worst case (the target is not in), for a sorted array of
size n, how many elements have to be checked, in other
words, how many times the critical comparison operation
has to be executed before knowing that the value is not
there when doing binary search?

7
Efficiency of Algorithms
• time efficiency and space efficiency
• How can we measure how long it takes for an algorithm
to solve a problem of a certain size?
– Usually we measure the number of critical operation(s) needed
based on the problem size n, which can be expressed as a function
f on n: f(n)

– Usually, we investigate the algorithm efficiency in the worst case


scenario. That is, we seek a function f(n) to describe the time
efficiency of an algorithm when the algorithm is used to solve a
problem in the worst case.

– Our major concern is when problem size n gets large, whether or


not the algorithm is workable on the problem
8
Efficiency of Algorithms (cont)
• We use a series of reference functions in terms of big O
notation to measure the efficiency of individual algorithms.
They are listed from the most efficient to least efficient
Function Name of function
------------ ------------------------
O (1) constant
O (log n) logarithmic
O (n) linear
O (n log n) linearithmic
O (n2) quadratic
O (nc); c > 1 polynomial
O (2n) exponential
O (n!) factorial
O (nn) n to the n
• A description of a function in terms of big O notation provides
an upper bound on the growth rate of the function 9
Sorting
• Sorting is the process of arranging an array/list of items in a
particular order

• For example:
– sorting an array of test scores in ascending numeric order
– sorting an array of people alphabetically by last name

• There are many sorting algorithms:


– selection sort  we have examined this algorithm in detail
– bubble sort  we are going to look at this algorithm
– Insertion sort  we are going to look at this algorithm
– merge sort
– quick sort
– … 10
Sorting
Selection Sort

Suppose we want to sort an array in ascending order:


• find the position of the smallest value in the array
• switch it with the value in the first position
• find the next smallest value in the array
• switch it with the value in the second position
• repeat until all values are in their proper places

11
Sorting
Selection Sort (cont)
• An example:
original array: 3 9 6 1 2
smallest is 1 at index 3
1 9 6 3 2
smallest is 2 at index 4
1 2 6 3 9
smallest is 3 at index 3
1 2 3 6 9
smallest is 6 at index 3
1 2 3 6 9

• Each time, the smallest remaining value is found and


exchanged with the element in the "next" position to be
filled
12
Sorting
Selection Sort (cont)
• We have implemented the selection sort algorithm as an
in-place sorting function in Week 7 using two supporting
functions

• Now, can you re-implement selection sort in one single


function? Use int as the base type of the array.

– How many arguments? Their types?


– Any return value?
– Does the function modify the arguments?
– How to test it?

13
Sorting
Bubble Sort

• Step through the array to be sorted, comparing each


pair of adjacent elements and swapping them if they
are in wrong order
• The pass through the array is repeated until no swaps
are needed, which indicates the array is sorted

14
Sorting
Insertion Sort
Suppose we want to sort an array in ascending order:
• consider the first item to be a sorted subarray (of one item)
• insert the second item into the sorted subarray, shifting the
first item as needed to make room to insert the new
addition
• insert the third item into the sorted subarray (of two items),
shifting items as necessary
• repeat until all values are inserted into their proper
positions

15
Sorting
Insertion Sort (cont)
• An example:
original: 3 9 6 1 2
[3] is sorted, insert 9:
3 9 6 1 2
[3 9] is sorted, insert 6:
3 6 9 1 2
[3 6 9] is sorted, insert 1:
1 3 6 9 2
[1 3 6 9] is sorted, insert 2
1 2 3 6 9
entire array is sorted

16
Sorting

• Implement bubble sort and insertion sort


algorithm as functions
– First, make sure you know how bubble sort and insertion sort
algorithm works. Apply the algorithm on a couple random
arrays to help you understand the algorithms
– Then, think about how to implement them. If needed, refer
to selection sort implementation for some idea/hint.
– Write your functions on paper
– Use more random arrays to run/test your implementation in
your head
– Only when you are confident that your implementation will
work, type it in Visual C++ and test it on computer

17

You might also like