You are on page 1of 37

Arrays

 Linear Array : A Linear Array is a list of a finite number n of


homogeneous data elements ( i.e., data elements of the
same type )
1. The elements of the array are referenced respectively by the
index set consisting of n consecutive numbers.
2. The elements of the array are stored respectively in
successive memory locations.

The number n of elements is called the length or


size of the array. We will assume the index set
consists of the integers 1,2,…….,n.

Length or the number of data elements of the array can be


obtained from the index set by the formula

Length = UB – LB + 1

Where UB is the largest index, called the upper bound, and


LB is the smallest index, called the lower bound, of the
array. Note that length = UB when LB = 1.
Representation of Linear array in memory
Let LA be the linear array in the memory of the computer.
Let us use the notation

LOC(LA[k]) = address of the element LA[k] of the array LA

As noted, the elements of LA are stored in successive


memory cells. Accordingly, the computer does not keep
track of the address of every elements of LA, but need to
keep track only of the address of the first element of LA,
denoted by
Base(LA)

And called the base address of LA. Using this address


Base(LA), the computer calculates the address of any
element of LA by the following formula:
LOC(LA[k]) = Base(LA) + w ( k - lower bound )

Where w is the number of words per memory cell for the array LA.
Traversing Linear Array
This can be accomplished by traversing array, that is, by
accessing and processing ( frequently called visiting ) each
element of array exactly once.

Algorithm:Step 1 : [ Initialize Counter ]


Set K := LB

Step 2 : Repeat step 3 & 4 while K<= UB

Step 3 : [ visit element ]


Apply PROCESS to LA[K]

Step 4 : [ increase counter ]


Set K:= K+1
[ End of step 2 Loop ]

Step 5: Exit.
Inserting into a Linear Array
INSERT ( LA, N, K, ITEM )
Here LA is a linear array with N elements and K is a positive
integer
such that K <= N. This algorithm inserts an element ITEM
into the Kth
position in LA.

Step 1 : [ initialize counter ] Set J := N


Step 2 : Repeat steps 3 & 4 while J >= K
Step 3 : [ move Jth element downward. ]
Set LA[J+1] := LA[J]
Step 4 : [ decrease counter ] Set J = J+1
[ End of Step 2 loop ]
Step 5 : [ insert element ] Set LA[K] := ITEM.
Step 6 : [ Reset N ] Set N := N+1
Step 7 : Exit.
Deleting from Linear Array
DELETE ( LA, N, K, ITEM )
Here LA is a linear array with N elements and K is the positive
integer
such that K <= N. This algorithm deletes the Kth element
from LA.

Step 1 : ITEM := LA[K]


Step 2 : Repeat for J = K to N-1
[ Move J+1st element upward ] Set LA[J] := LA[J+1]
[ End of Loop ]
Step 3 : [ Reset the number N of elements in LA ]
Set N := N-1
Step 4 : Exit.
Bubble Sort
Suppose the list of numbers A[1], A[2],…………,A[N] is in memory. The
bubble sort algorithm works as follows:

Step 1 : Compare A[1] and A[2] and arranged them in desired order, so
that A[1] < A[2]. Then compare A[2] and A[3] and arrange them
so that A[2] < A[3]. Then compare A[3] and A[4] and arrange
them so that A[3] < A[4]. Continue until we compare A[N-1] with
A[N] and arrange them so that A[N-1] < A[N].

Observe that Step 1 involves N-1 comparisons. When Step 1 is


completed, A[N] will contain the largest element.

Step 2 : Repeat Step 1 with one less comparison; that is, now we stop
after we compare and possibly rearrange A[N-2] and A[N-1].

Observe that Step 2 involves N-2 comparisons. When Step 2 is


completed, the second largest element will occupy A[N-1].
Step 3 : Repeat step 1 with two fewer comparison; that is, we stop
after
we compare and possibly rearrange A[N-3] and A[N-2].
…………………………………………………………………….
…………………………………………………………………….
…………………………………………………………………….

Step N-1: Compare A[1] with A[2] and arrange them so that A[1] <
A[2].

After n-1 steps, the list will be sorted in increasing order.

The process of sequentially traversing through or part of a list is


frequently called a “pass”, so each of the above steps is called a
pass.
Accordingly, the bubble sort algorithm requires n-1 passes, where n
is
the number of input items.
Algorithm : BUBBLE ( DATA, N )
Here DATA is an array with N elements. This algorithm sorts
the
elements in DATA

Step 1 : Repeat Steps 2 and 3 for K = 1 to N-1


Step 2 : Set PTR := 1 [ initialize pass pointer PTR ]
Step 3 : Repeat while PTR <= N – K : [ execute pass ]
a) If DATA[PTR] > DATA[PTR+1], then:
Interchange DATA[PTR] and DATA[PTR+1].
[ end of if structure ]
b) Set PTR := PTR + 1.
[ end of inner loop ]
[ end of step1 outer loop ]
Step 4 : Exit.
Linear Search

Suppose DATA is in linear array with n


elements. Given no other
information about DATA, the most intuitive
way to search for a given
ITEM in DATA is to compare ITEM with each
element of DATA one by
one. That is, first we test whether DATA[1]
= ITEM, and then we test
whether DATA[2] = ITEM, and so on. This
method, which traverse
DATA sequentially to locate ITEM, is called
Linear Search or
Sequential Search.
Algorithm : LINEAR ( DATA, N, ITEM, LOC )
Here DATA is a linear array with N elements, and ITEM is a
given item
of information. This algorithm finds the location LOC of ITEM
in DATA,
or sets LOC := 0 if the search is unsuccessful.

Step 1 : [ insert ITEM at the end of DATA ]


Set DATA[N+1] := ITEM.
Step 2 : [ initialize counter ] Set LOC := 1.
Step 3 : [ search for ITEM ]
Repeat while DATA[LOC] != ITEM:
Set LOC := LOC + 1
[ end of loop ]
Step 4 : [ successful ? ] if LOC = N + 1, then : Set LOC := 0.
Step 5 : Exit.
Binary Search
Suppose we want to find the location of some name in
telephone
directory. Obviously, one does not perform linear search.
Rather, one
opens the directory in the middle to determine which half
contains the
name being sought. Then one open that half in the middle to
determine
which quarter of the directory contains the name. Then one
opens that
quarter in the middle to determine which eighth of the
directory contains
the name. And so on. Eventually, one finds the location of the
name,
since one is reducing ( very quickly ) the number of possible
locations
for it in the directory.
The Binary Search algorithm applied to our array DATA works
as
follows. During each stage of our algorithm, our search for
ITEM is
reduced to a segment of elements of DATA:

DATA[BEG], DATA[BEG + 1], DATA[BEG + 2],….,DATA[END]

Note that the variable BEG and END denote, respectively, the
beginning and end locations of the segment under
consideration. The
algorithm compares ITEM with the middle element DATA[MID]
of the
segment, where MID is obtained by

MID = INT (( BEG + END ) / 2 )

If DATA[MID] = ITEM, then the search is successful and we


set LOC : = MID. Otherwise a new segment of DATA is
obtained as
follows:
a) If ITEM < DATA[MID], then ITEM can appear only in the
left half of the segment:
DATA[BEG], DATA[BEG + 1],…,DATA[MID-1]
so we reset END := MID -1 and begin searching again.

b) If ITEM > DATA[MID], then ITEM can appear only in the


right half of the segment:
DATA[MID +1], DATA[MID+2],…,DATA[END]
so we reset BEG := MID+1 and begin searching again.

Initially, we begin with the entire array DATA; i.e., we begin


with
BEG = 1 and END = n, or, more generally, with BEG = LB
and
END = UB.
If ITEM is not in DATA, then eventually we obtain
END < BEG
This condition signals that the search is unsuccessful, and in
such a
case we assign LOC := NULL.
Algorithm : BINARY( DATA, LB, UB, ITEM, LOC )
Step 1 : [ initialize segment variables ]
Set BEG := LB, END := UB and MID = INT((BEG+END)/2).
Step 2 : Repeat Step 3 and 4 while BEG <= END and
DATA[MID] != ITEM
Step 3 : if ITEM < DATA[MID], then:
Set END := MID – 1.
Else:
Set BEG := MID + 1.
[ end of if structure ]
Step 4 : Set MID := INT(( BEG + END )/2)
[ end of step 2 loop ]
Step 5 : if DATA[MID] = ITEM, then:
Set LOC := MID.
Else:
Set LOC := NULL
[ end of if structure ]
Step 6 : Exit.
Representation of Two-Dimensional Arrays
in Memory
Let A be a two-dimensional m x n array. The array will be represented in
memory by a block of m . n sequence memory locations. Specifically,
the
programming language will store the array A either

1) Column by column, is what is called column-major order, or


2) Row by row, in row-major order.

The following figure shows these two way when A is two-dimensional


3x3 array.
A A
(1,1) (1,1)
(2,1)
(3,1) (1,2)
(1,2)
(1,3)
(2,2)
(3,2)
(2,1)
(1,3)
(2,3) (2,2)
(3,3)
(2,3)
(a) Column-major order (b) Row-major order

(3,1)
In two dimensional array computer keeps track of Base(A) –
the
address of the first element A[1,1] of A – and computes the
address
LOC(A[J,K]) of A[J,K] using the formula

( Column-major order )

LOC(A[J,K]) = Base(A) + w[M(K-1) + (J-1)]

( Row-major order )

LOC(A[J,K]) = Base(A) + w[N(J-1) + (K-1)]

w denotes the number of words per memory location for the


array A
Sparse Matrices

Matrices with a relatively high proportion of zero entries are


called
sparse matrices.

a) Matrix, where all entries above the main diagonal are


zero or, equivalently, where nonzero entries can only
occur on or below the main diagonal, is called a ( lower )
triangular matrix.

b) Where nonzero entries can only occur on the diagonal or


on elements immediately below or above the diagonal, is
called a tridiagonal matrix.
1  1 7 
   
2 3  2 3 8 
4   5 6 2 
5 6  
 
7 8 9 2   9 2 4
1  5
 2 3 4 5
  4 

a) Triangular matrix b) Tridiagonal matrix

Sparse Matrices
Stacks
A Stack is list of elements in which an element may be inserted and
deleted only at one end, called the top of the stack. This means, in
particular, that elements are removed in stack in the reverse order of
that in which they are inserted into the stack.

Special terminology is used for the two basic operations associated into
Stacks.

a) “Push” is the term used to insert an element into a stack.


b) “Pop” is the term used to delete an element from stack.
Push
 In procedure PUSH, one must first test whether there is
room in the stack for the new item; if not, then we have
the condition known as overflow.

Algorithm : PUSH( STACK, TOP, MAXSTK, ITEM )

Step 1 : [ stack already filled ? ]


if TOP = MAXSTK, then: Print: Overflow, and Exit.
Step 2 : Set TOP := TOP + 1 [ increase TOP by 1 ]
Step 3 : Set STACK[TOP] := ITEM
[ inserts ITEM in new TOP position. ]
Step 4 : Exit.
Pop
 In procedure POP, one must first test whether there is an
element in the stack to be deleted; if not, then we have
the condition known as underflow.

Algorithm : POP( STACK, TOP, ITEM )

Step 1 : [ stack has an item to be removed ?]


If TOP = 0, then: Print: UNDERFLOW, and Exit.
Step 2 : Set ITEM := STACK[TOP]
[ assign TOP element to ITEM ]
Step 3 : Set TOP := TOP – 1 [ decrease TOP by 1 ]
Step 4 : Exit.
Arithmetic Expression
We assume the following three levels of precedence for the
usual five
binary operations:

Highest : Exponential.
Next Highest : Multiplication and Division.
Lowest : Addition and Subtraction.

Notation :

Infix : A+B
Prefix : +AB
Postfix : AB+
Evaluation of Postfix Expression
Algorithm : This algorithm finds the value of an arithmetic expression
P
written in postfix notation.
Step 1 : Add the right parenthesis “)” at the end of P.
[ this act as a sentinel. ]
Step 2 : Scan P from left to right and repeat step 3 & 4 for
each element
of P until the sentinel “)” is encountered.
Step 3 : If an operand is encountered, put it on STACK.
Step 4 : If an operator is encountered, Then:
a) Remove the two top elements of STACK, where A is the
top
element and B is the next-to-top element.
b) Evaluate B operator A.
c) Place the result of (b) back on STACK.
[ end of If structure ]
[ end of step 2 loop ]
Step 5 : Set VALUE equal to the top element on STACK.
Step 6 : Exit.
Transforming Infix Expressions into Postfix
Expressions
POLISH( Q, P )
Suppose Q is an arithmetic expression written in infix notation. This
Algorithm finds the equivalent postfix expression P.

Step 1 : Push “(“ onto STACK, and add “)” to the end of Q.
Step 2 : Scan Q from left to right and repeat step 3 to 6 for each
element of Q until the STACK is empty.
Step 3 : If an operand is encountered, add it to P.
Step 4 : If a left parenthesis is encountered, push it onto STACK.
Step 5 : If an operator is encountered, Then:
a) Repeatedly pop from STACK and add to P each operator
( on the top of STACK ) which has a same precedence
or the higher precedence than an operator encountered.
b) Add operator encountered to STACK.
[ end of if structure ]
Step 6 : If the right parenthesis is encountered, then:
a) Repeatedly pop from STACK and add to P each
operator
( on the top of STACK ) until a left parenthesis is
encountered.
b) Remove the left parenthesis. [ do not add left
parenthesis to
P]
[ end of if structure ]
[ end of step 2 loop ]
Step 7 : Exit.
Queues
 A queue is a linear list of elements in which deletion can take
place
only at one end, called the front, and the insertion can take
place
only at the other end, called the rear.

 Queues are also called first-in-first-out ( FIFO ) lists, since


the first element in a queue will be the first element out of
the queue.

 Queues will be maintained by a linear array QUEUE and two


pointer variables : FRONT, containing the location of the front
element of the queue; and REAR, containing the location of
the rear element of the queue.

 The condition FRONT = NULL will indicate that the queue is


empty.
 Whenever an element is deleted from the queue, the value
of FRONT is increased by 1; this can be implemented by
assignment
FRONT = FRONT + 1
 Whenever an element is added to the queue, the value of
REAR in increased by 1; this can be implemented by
assignment
REAR = REAR + 1
 We assume that the array QUEUE is circular, that is, that
QUEUE[1] comes after QUEUE[N] in the array.
 Therefore increasing REAR to N + 1, we reset REAR = 1
and then assign
QUEUE[REAR] := ITEM
 Similarly, if FRONT = N and an element of QUEUE is
deleted, we reset FRONT = 1 instead of increasing FRONT
to N + 1
 If FRONT = REAR != NULL means queue contain only one
element.
 If the last element of the queue is deleted the set
FRONT := NULL & REAR := NULL
Insertion
Algorithm : QINSERT( QUEUE, N, FRONT, REAR, ITEM )
This procedure inserts an element ITEM into a queue.

Step 1 : [ Queue already filled ? ]


If FRONT = 0 and REAR = N, or if FRONT = REAR + 1,
then;
Write : OVERFLOW, and exit.
Step 2 : [ find new value of REAR ]
If FRONT := NULL, then: [ Queue initially empty ]
Set FRONT = 1 and REAR = 1
Else if REAR = N, then:
Set REAR := 1
Else Set REAR := REAR + 1
[ end of if structure ]
Step 3 : Set QUEUE[REAR] := ITEM [ this inserts new
elements ]
Step 4 : Exit.
Deletion
Algorithm : QDELETE( QUEUE, N, FRONT, REAR, ITEM )
This procedure delete an element from a queue and assign it
to the
variable ITEM.
Step 1 : [ Queue already empty ? ]
If FRONT := NULL, then;
Write : UNDERFLOW, and exit.
Step 2 : Set ITEM := QUEUE[FRONT].
Step 3 : [ find new value of FRONT ]
If FRONT := REAR, then: [ Queue has only one
element to start. ]
Set FRONT := NULL and REAR := NULL
Else if FRONT = N, then:
Set FRONT := 1
Else Set FRONT := FRONT + 1
[ end of if structure ]
Step 4 : Exit.
Deques
 A deque ( pronounced either “deck” or “dequeue” ) is a
linear list in which elements can be added or removed at
either end but not in the middle. The term deque is a
contraction of the name double – ended queue.

 There are two variation of a deque, which are


intermediate between a deque and a queue.

1) An input – restricted deque : is a deque which allows


insertions at only one end of the list but allow deletions at
both ends of the list.
2) An output – restricted deque : is a deque which allows
deletion at only one end of the list but allows insertions at
both ends of the list.
Priority Queues
 A priority queue is a collection of elements such that each
element has been assigned a priority and such that the
order in which elements are deleted and processed
comes from the following rules:

1) An element of the higher priority is processed before any


element of the lower priority.

2) Two elements with the same priority are processed


according to the order in which they are added to the
queue.

A prototype of a priority queue is a time sharing system :


programs of
higher priority are processed first, and the program with the
same
priority form a standard queue.
Data Structure
Data : Data are simply values or sets of values.
Data Structure : The logical or mathematical model
of a particular organization of data is called a
data structure.
Data Structures = Organized Data + Allowed
Operations.
Data Structures are of three types:
 Linear and Non Linear: In linear, the data
elements are arranged in a linear sequence, i.e.
data processed one by one sequentially like
Arrays, Linked Lists, Stacks and Queues.
In Non Linear, the data elements are not in
sequence that means insertion and deletion
are not possible in a linear array like, Tree,
Graph.
 Homogeneous and Non Homogeneous: In
homogeneous the data elements are of same
type like an array. In Non Homogeneous, the
data elements may not be of same type like
Structures in C.
 Static and Dynamic: Static structures are
ones whose sizes and structures associated
memory locations are fixed at compile time
like Arrays. Dynamic structures are ones that
expand or shrink as required during the
program execution and their associated
memory location change like Linked Lists.
Data Structure Operations
 Traversing : Accessing each record exactly once so that
certain items in the record may be processed.

 Searching : Finding the location of the record with a given


key value, or finding the locations of all records which
satisfy one or more conditions.

 Inserting : Adding a new record to the structure.

 Deleting : Removing a record from the structure.

 Sorting : Arranging the records in some logical order (e.g.


alphabetically according to some NAME key, or in numerical
order according to some NUMBER key, such a social
security number or account number )

 Merging : Combining the records in two different sorted


files into a single sorted file.
Algorithms : Complexity, Time-Space Tradeoff

 Algorithm : An algorithm is a well-defined list of steps for


solving a particular problem.

 The time and space it uses are two major measures of the
efficiency of an algorithm.

 The complexity of an algorithm is the function which gives


the running time and/ or space in terms of the input size.

 Choice of data structure involves a time-space tradeoff : by


increasing the amount of space for storing the data, one
may be able to reduce the time needed for processing the
data, or vice versa.
The performance of the different order of algorithms, their
time complexity and their function values are computed in
the table below:
Log2n : 0 1 2 3 4 5
N : 1 2 4 8 16 32
Nlog2n : 0 2 8 24 64 160
N2 : 1 4 16 64 256 1024
N3 : 1 8 64 512 4096 32768
2n : 2 4 16 256 65536 4294967296

You might also like