Csi 06
Csi 06
Csi 06
ALGORITHMS
Content
Define three constructs—sequence, selection, and repetition—and describe their use in algorithms.
Describe UML diagrams and how they can be used when representing algorithms.
Describe the concept of sorting and understand the mechanisms behind three primitive sorting algorithms.
Describe the concept of searching and understand the mechanisms behind two common searching algorithms.
Problem:
Develop an algorithm for finding the largest integer among a list of positive integers (for example 12, 8,
13, 9, 11). The algorithm should be general and not depend on the number of integers.
Solution :
Step 1 In this step, the algorithm inspects the first integer (12). The algorithm defines a data item, called
Largest, and sets its value to the first integer (12).
Step 2 The algorithm makes a comparison between the value of Largest (12) and the value of the second
integer (8). It finds that Largest is larger than the second integer, which means that Largest is still holding
the largest integer.
Step 3 The largest integer so far is 12, but the new integer (13) is larger than Largest. The value of Largest
should be replaced by the third integer (13). The algorithm changes the value of Largest to 13 and moves
to the next step.
Step 4 Nothing is changed in this step because Largest is larger than the fourth integer (9).
Step 5 Again nothing is changed because Largest is larger than the fifth integer (11).
Example (flow)
2. Refinement
Computer scientists have defined three constructs for a structured program or algorithm. The idea is that a
program must be made of a combination of only these three constructs: sequence, decision, and repetition
(Figure 8.6).
It has been proven there is no need for any other constructs. Using only these constructs makes a program
or an algorithm easy to understand, debug, or change.
Unified Modeling Language (UML) is a pictorial representation of an algorithm. It hides all the details
of an algorithm in an attempt to give the ‘big picture’ and to show how the algorithm flows from
beginning to end.
Problem Write an algorithm in pseudocode that finds the sum of two integers.
Solution This is a simple problem that can be solved using only the sequence construct. Note also that we
name the algorithm, define the input to the algorithm and, at the end, we use a return instruction to return
the sum.
One of the most common applications in computer science is sorting, which is the
process by which data is arranged according to its values. People are surrounded by
data.
If the data was not ordered, it would take hours and hours to find a single piece of
information. Imagine the difficulty of finding someone’s telephone number in a
telephone book that is not ordered
Common sorting algorithms
Bubble/Shell sort:
Insertion sort
Selection sort
Quick sort
Merge sort
Selection Sort
Figure 7.1 Example of selection sort Figure 6.13 Algorithm of selection sort
Bubble sorts
In the bubble sort method, the list to be sorted is also divided into two sublists—sorted and unsorted.
The smallest element is bubbled up from the unsorted sublist and moved to the sorted sublist.
After the smallest element has been moved to the sorted list, the wall moves one element ahead,
increasing the number of sorted elements and decreasing the number of unsorted ones.
Each time an element moves from the unsorted sublist to the sorted sublist, one sort pass is completed
Another common algorithm in computer science is searching, which is the process of finding
the location of a target among a list of objects. In the case of a list, searching means that given
a value, we want to find the location of the first element in the list that contains that value.
There are two basic searches for lists: sequential (linear) search and binary search.
Sequential search can be used to locate an item in any list,
whereas binary search requires the list first to be sorted.
5.2 Linear Search
A linear search or sequential search is a method for finding an element within a list . It sequentially checks
each element of the list until a match is found or the whole list has been searched.
A linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the
list. If each element is equally likely to be searched, then linear search has an average case
of n+1/2 comparisons, but the average case can be affected if the search probabilities for each element vary.
6. STOP
5.3 Binary Search