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

Data Structure Lect5 - Week2

Uploaded by

Hasnain Nisar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Data Structure Lect5 - Week2

Uploaded by

Hasnain Nisar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Discrete Structure

Lecture 5
Topics

 Algorithms

 Properties of algorithm

 Searching Algorithms

 Linear Search

 Binary Search
Algorithm
An algorithm is a finite sequence of precise instructions for performing a
computation or for solving a problem.

Setting up the appropriate mathematical model is only part of the solution.


To complete the solution, a method is needed that will solve the general problem
using the model.
Ideally, what is required is a procedure that follows a sequence of steps that
leads to the desired answer.
Such a sequence of steps is called an algorithm
Example of Maximum Number
Describe an algorithm for finding the maximum (largest) value in a finite sequence of
integers.

Solution of Example 1: We perform the following steps.


1. Set the temporary maximum equal to the first integer in the sequence. (The
temporary maximum will be the largest integer examined at any stage of the
procedure.)
2. Compare the next integer in the sequence to the temporary maximum, and if it is
larger than the temporary maximum, set the temporary maximum equal to this
integer.
3. Repeat the previous step if there are more integers in the sequence.
4. Stop when there are no integers left in the sequence. The temporary maximum at
this point is the largest integer in the sequence.
Algorithm 1

Finding the Maximum Element in a Finite Sequence.


procedure max(a1,a2,...,an: integers)
max := a1
for i := 2 to n
if max <a i then max := ai
return max { max is the largest element}
Properties of the Algorithm
 There are several properties that algorithms generally share. They are useful
to keep in mind when algorithms are described. These properties are:
 Input. An algorithm has input values from a specified set.
 Output. From each set of input values an algorithm produces output values
from a specified set. The output values are the solution to the problem.
 Definiteness. The steps of an algorithm must be defined precisely.
 Correctness. An algorithm should produce the correct output values for each
set of input values.
 Finiteness. An algorithm should produce the desired output after a finite (but
perhaps large) number of steps for any input in the set.
 Effectiveness. It must be possible to perform each step of an algorithm
exactly and in a finite amount of time.
 Generality. The procedure should be applicable for all problems of the desired
form, not just for a particular set of input values.
Searching Algorithm

 The problem of locating an element in an ordered list occurs in many


contexts.
 For instance, a program that checks the spelling of words searches for them
in a dictionary, which is just an ordered list of words.
 Problems of this kind are called searching problems
Discrete Structure
Lecture 6
Searching Algorithm: Linear Search
The first algorithm that we will present is called the linear search, or sequential
search, algorithm.

The linear search algorithm begins by comparing x and a1.


When x-a1, the solution of the location a1, namely ,1.

When x != a1, compare x with a2.


If x=a2, the solution of the location of a2 , namely, 2.
When x!=a2, compare x with a3.
Linear Search Algorithm
 procedure linear search(x: integer, a1,a2,...,an: distinct integers)
 i := 1
 while (i ≤ n and x = ai)
 i := i +1
 if i ≤ n then location:= I
 else location:= 0
 return location{location is the subscript of the term that equals x, or is 0
ifx is not found}
Binary Search
 It proceeds by comparing the element to be located to the middle term of the
list.

 The list is then split into two smaller sublists of the same size, or where one
of these smaller lists has one fewer term than the other.

 The search continues by restricting the search to the appropriate sublist


based on the comparison of the element to be located and the middle term.
Example: Binary Search
 To search for 19 in the list
 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 ,
 first split this list, which has 16 terms, into two smaller lists with eight terms
each, namely,
 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 .
 Then, compare 19 and the largest term in the first list.
 Next, split this list, which has eight terms, into the two smaller lists.
 12 13 15 16 18 19 20 22.
 Then, compare 19 and the largest term in the first list
 The list 18 19 20 22 is split into two lists
 18 19 20 22
Binary Search Algorithm
procedure binary search (x: integer, a1,a2,...,an: increasing integers)
i := 1{ i is left endpoint of search interval}
j := n{ j is right endpoint of search interval}
while i<j
m :=[ ( i +j )/2]
if x>a m then i := m+1
else j := m
if x = ai then location:= i
else location:=0
return location{location is the subscript i of the term ai equal to x, or 0 if x is not
found}
Discrete Structure
Lecture 7
Topics

 Sorting Algorithm

 Bubble Sort

 Example of Bubble sort

 Insertion Sort

 Examples Insertion Sort


Sorting
Ordering the elements of a list is a problem that occurs in many contexts.
For example, to produce a telephone directory it is necessary to alphabetize the
names of subscribers.
Similarly, producing a directory of songs available for downloading requires that
their titles be put in alphabetic order.
Putting addresses in order in an e-mail mailing list can determine whether there
are duplicated addresses.
Creating a useful dictionary requires that words be put in alphabetical order.
Generating a parts list requires that we order them according to increasing part
number
Example of Sorting
Sorting is putting these elements into a list in which the elements are in increasing
order.
For instance, sorting the list 7, 2, 1, 4, 5, 9 produces the list 1, 2, 4, 5, 7, 9.

Sorting the list d, h, c, a, f (using alphabetical order) produces the list a, c, d, f, h.


Bubble Sort
The bubble sort is one of the simplest sorting algorithms, but not one of the most
efficient.

It puts a list into increasing order by successively comparing adjacent elements,


interchanging them if they are in the wrong order.

To carry out the bubble sort, we perform the basic operation, that is,
interchanging a larger element with a smaller one following it, starting at the
beginning of the list, for a full pass.

We iterate this procedure until the sort is complete


Example of Bubble Sort
 Use the bubble sort to put 3 , 2, 4, 1, 5 into increasing order
Algorithm: Bubble Sort

procedure bubblesort(a1,...,an : real numbers with n ≥ 2)


for i := 1 to n−1
for j := 1 to n−i
if aj >a j+1 then interchange aj and aj+1
{a1,...,an is in increasing order}
Insertion Sort
The insertion sort is a simple sorting algorithm, but it is usually not the most
efficient.
To sort a list with n elements, the insertion sort begins with the second element.
The insertion sort compares this second element with the first element and
inserts it before the first element if it does not exceed the first element and
after the first element if it exceeds the first element.
At this point, the first two elements are in the correct order.
The third element is then compared with the first element, and if it is larger
than the first element, it is compared with the second element; it is inserted into
the correct position among the first three elements.
Example of Insertion Sort
 The insertion sort to put the elements of the list 3,2,4,1,5 in increasing order
 The insertion sort first compares 2 and 3. Because 3>2 , it places 2 in the first
position, producing the list 2, 3, 4,1,5 (the sorted part of the list is shown in
color). At this point, 2 and 3 are in the correct order.
 Next, it inserts the third element, 4, into the already sorted part of the list
by making the comparisons 4 > 2 and 4 > 3. Because 4 > 3,4 remains in the
third position. At this point, the list is 2, 3, 4, 1, 5 and we know that the
ordering of the first three elements is correct.
 Next, we find the correct place for the fourth element, 1, among the already
sorted elements, 2,3,4. Because 1 < 2, we obtain the list 1, 2, 3, 4, 5.
 Finally, we insert 5 into the correct position by successively comparing it to 1,
2, 3, and 4. Because 5 > 4, it stays at the end of the list, producing the
correct order for the entire list.
Algorithm Insertion Sort
procedure insertion sort(a1,a2,...,an: real numbers with n ≥ 2)
for j := 2 to n i := 1
while aj > ai
i := i +1
m := aj
for k := 0 to j −i −1
aj−k := aj−k−1
ai := m
{a1,...,an is in increasing order}
Discrete Structure
Lecture 8
Topics

 Big O Notation

 Example Of Big O Notation

 Halting Problem
Big O Notation

Let f and g be functions from the set of integers or the set of real numbers to the
set of real numbers. We say that f(x) is O(g(x))if there are constants C and k such
that

|f(x)| ≤ C |g(x)| whenever x >k.

[ This is read as “f(x)is big-oh of g(x).” ]


Example
Show that f(x)= x2 +2x +1 is O (x2).

We observe that we can readily estimate the size of f(x)when x>1 because x<x 2 and
1<x 2 when x>1.

It follows that
0 ≤ x2 +2x +1 ≤ x2 +2x2 +x2 = 4x2
Graph Of Function
Halting Problem

 We will show that there is a problem that cannot be solved using any
procedure.

 That is, we will show there are unsolvable problems. The problem we will
study is the halting problem
Example of Halting Problem

You might also like