IntroductiontoDataStructureandArray PDF
IntroductiontoDataStructureandArray PDF
6
Algorithms
• Properties of algorithms:
7
Algorithm Examples
•We will use a pseudocode to specify algorithms, which
slightly reminds us of Basic and Pascal.
•Example: an algorithm that finds the maximum element
in a finite sequence
8
Algorithm Examples
•Another example: a linear search algorithm, that is, an
algorithm that linearly searches a sequence for a particular
element.
procedure linear_search(x: integer; a1, a2, …, an: integers)
i := 1
while (i n and x ai)
i := i + 1
if i n then location := i
else location := 0
•{location is the subscript of the term that equals x, or is
zero if x is not found}
9
Algorithm Examples
•If the terms in a sequence are ordered, a binary search
algorithm is more efficient than linear search.
•The binary search algorithm iteratively restricts the
relevant search interval until it closes in on the position of
the element to be located.
10
Algorithm Examples
procedure binary_search(x: integer; a1, a2, …, an: integers)
i := 1 {i is left endpoint of search interval}
j := n {j is right endpoint of search interval}
while (i < j)
begin
m := (i + j)/2
if x > am then i := m + 1
else j := m
end
if x = ai then location := i
else location := 0
•{location is the subscript of the term that equals x, or is
zero if x is not found}
11
Complexity
12
Complexity
13
Complexity
•This means that algorithm B cannot be used for large
inputs, while algorithm A is still feasible.
14
The Growth of Functions
•The growth of functions is usually described using the
big-O notation.
15
The Growth of Functions
•When we analyze the growth of complexity functions,
f(x) and g(x) are always positive.
•Therefore, we can simplify the big-O requirement to
•f(x) Cg(x) whenever x > k.
16
The Growth of Functions
•The idea behind the big-O notation is to establish an
upper boundary for the growth of a function f(x) for large
x.
•This boundary is specified by a function g(x) that is
usually much simpler than f(x).
•We accept the constant C in the requirement
•f(x) Cg(x) whenever x > k,
•because C does not grow with x.
•We are only interested in large x, so it is OK if
f(x) > Cg(x) for x k.
17
The Growth of Functions
Example:
Show that f(x) = x2 + 2x + 1 is O(x2).
f(x) is O(x2).
18
The Growth of Functions
•Question: If f(x) is O(x2), is it also O(x3)?
19
The Growth of Functions
•“Popular” functions g(n) are
•n log n, 1, 2n, n2, n!, n, n3, log n
21
Arrays : An Overview
Introduction:
Array is one of the simplest data structure in computer
programming. Arrays hold a fixed number of equally sized data
elements, generally of the same data type.
Objective:
At the end of the Module, students should be able to,
Introduction to Arrays
Multi-dimensional Arrays
Advantages
Limitations
Introduction to Arrays
• Arrays are a data type that are used to represent a large number of
homogeneous values, that is values that are all of the one data
type.
• The data type could be of type char, in which case we have a string.
• The data type could just as easily be of type int, float or even
another array.
• Example : int myarray[] = {1,23,17,4,-5,100};
The elements in the Array are always stored in consecutive
memory locations.
• When data is passed to a function, it is passed by value. But in the
case of the array , the actual array is passed to the function and the
function can modify it any way it wishes to. The result of the
modifications will be available back in the calling program. This is
called pass by reference.
Array Representation
• Arrays can be declared in various ways in different languages. For
illustration, let's take C array declaration.
34