You are on page 1of 35

UNIT - II

***STORING STRINGS:

Alphabet: A B C D E F G H I J K L M N O P Q R S T U V W X Y

Z Digits: 0,1,2,3,4,….9

Special characters: + - / * ( ) , . $ = ‘ □

The set of special characters, which includes the blank space, frequently denoted by □.

A finite sequence S of zero or more characters is called a string. The number of characters in a
string is called its length. The string with zero characters is called the empty string of null string.
Specific strings will be denoted by
enclosing their characters in single
quotation marks.

Definition. A stringis a sequence of


characters taken from an alphabet A.

Example. Given the alphabet A = {a,b,d,y},


one can construct the string abadbaby.

Strings are stored in three types of


structures:

1. fixed-length structures

2. variable-length structures with fixed


maximums

3. linked structures

1.Record oriented, fixed-length


storage:In fixed-length storage each
line of print is viewed as a record,
where all records have the same
length, i.e., where each record
accommodates the same number of
characters. Since data are frequently
input on terminals with 80-column
images or using 80-column cards.

Dis-advantages:

1. Time is wasted reading an entire


record if most of the storage
consists of inessential blank
spaces.

2. Certain records may require more


space than available
AG&SG***MUNNI***Data Structures***Page 1
3. when the correction consists of more or fewer characters than the original text, changing a mis-
spelled word requires the entire record to be changed

2. Variable-length storage with fixed maximum: It can be done in two ways

a) one can use a marker such as two dollar signs ($$) to signal the end of the string.

b) one can list the length of the string – as an additional item in the pointer array, for example
these
ways of storing strings will obviously
save space and are sometimes used
in secondary memory when records
are relatively permanent and require
little change. However such methods
of storage are usually inefficient when
the strings and their lengths are
frequently being changed.

3. Lined storage:

Computers are being used very


frequently today for word processing,
i.e., for inputting, processing and
outputting printed matter. Therefore,
the computer must be able to correct
and modify the printed matter, which
usually means deleting, changing and
inserting words, phrases, sentences
and even paragraphs in the text.

Linked storage is a linearly ordered sequence of memory cells, called nodes where each node
contains an item, called link, which points to the next node in the list.

Strings may be stored in lined lists, each


memory cell is assigned one character or a
fixed number of characters, and a link
contained in the cell gives the address of
the cell containing the next character or
group of characters in the string. For
example consider a quotation:

“To be or not to be, that is the question”

Fig: 3.7(a) shows how the string would


appear in memory with one character per
node, and

Fig: 3.7(b) shows how it would appear with


four characters per node.

***CHARACTER DATA TYPE:

Constants: Many programming languages denote string constants by placing the string in either
AG&SG***MUNNI***Data Structures***Page 2
single or double quotation marks. For example: ‘THE END’ and ‘ TO BE OR NOT TO BE’

AG&SG***MUNNI***Data Structures***Page 3
Are string constants of length 7 and 18 characters respectively. Our algorithms will also define
character constants in this way.

Variables: Each programming language has its own rules for forming character variables. However,
such variables fall into one of three categories:

1.static

2.semistatic

3.dynamic

A static character variable, we mean a variable whose length is defined before the program is
executed and cannot change throughout the program.

By a semi-static character variable, we mean a variable whose length may vary during the
execution of the program as long as the length does not exceed a maximum value determined by the
program before the program is executed.

By a dynamic character variable, we mean a variable whose length can change during the execution
of the program.

***STRING OPERATIONS:

Alphabet: A B C D E F G H I J K L M N O P Q R S T U V W X Y

Z Digits: 0,1,2,3,4,….9

Special characters: + - / * ( ) , . $ = ‘ □

The set of special characters, which includes the blank space, frequently denoted by □. A finite
sequence S of zero or more characters is called a string. The number of characters in a string is
called its length. The string with zero characters is called the empty string of null string. Specific
strings will be denoted by enclosing their characters in single quotation marks.

Definition. A stringis a sequence of characters taken from an alphabet A. Example. Given the
alphabet A = {a,b,d,y}, one can construct the string abadbaby.

Operations on Strings. Strings can be manipulated by operations such


as concatenation, length, substringand patternrecognition. We discuss each of these operations
as follows.

CONCATENATION Given two strings sand t, the concatenationoperation,

written as s|| t or s+ t appends tto s, such that the last character of sis followed by
the first character of tand the remainder of t.

Example. Given s= "hello" and t= "dave" s+ t= "hellodave".

Algorithm. Given two strings sand tof length M and N, respectively, the concatenation
operation s+ tcan be expressed in pseudocode, as follows:
concatenate(s,t : string):
{
AG&SG***MUNNI***Data Structures***Page 4
for i = 1 to M do # M iterations
output(s[i]) # 1 I/O per iteration
for i = 1 to N do # N iterations
output(t[i]) # 1 I/O per iteration
}

Analysis. The preceding algorithm requires O(M + N) I/O operations, due to the two non-nested
loops.

LENGTH Given a string s, the lengthoperation returns the number of characters in s.

Example. Given s= "hello", length(s) = 5.

Example. Given s= "hello world", length(s) = 11, since leading blank symbols and internal blank
symbols are included in the length calculation.

Algorithm. Given a string s, and assuming that sterminates with a null character, the length of s can
be computed as follows:
length(s : string):
{
L=0
repeat until s[i] =
null L = L + 1
return(L)
}

Analysis. The preceding algorithm requires N incrementations to compute the length of an N-


character string.

SUBSTRING: Given a string sof length N, a substring of sis a contiguous partition of sthat has
length less than or equal to N.

Example. Given s= "friday", the substring s[3:4] = "id", and the substring s[4:6] = "day", as shown
below.

Figure 2.2.1. Example of substring extraction.

Algorithm. Given a string s, the [i:j]-th substring of s can be computed as follows:


substring(s : string, i,j: integer):
{
for k = i to j do
output(s[k])
}

Analysis. The preceding algorithm requires O(j - i) I/O operations to compute the [i:j]-th substring. If i

AG&SG***MUNNI***Data Structures***Page 5
> N or j > N or i < 1 or j < 1, then the substring is undefined.

INDEXING:

Indexing, also called pattern matching, refers to finding the position where a string pattern P first
appears in a given text T. we call this operation INDEX and write INDEX (text, pattern)If the pattern P
does not appear in the text T, then INDEX is assigned the value 0. the arguments “text” and “pattern”
can be either string constants or string variables.

Ex. ‘HIS FATHER IS THE PROFESSOR’

Then INDEX (T, ‘THE’), INDEX(T,’THEN’) and INDEX (T,’ □THE □’)

Have the values 7, 0 and 14 respectively.

***WORD/TEXT PROCESSING:
word processing are:

1) Replacement: Replacing one string in the text by another. Suppose in a given text T we want to
replace the first occurrence of a pattern P1 is by a pattern P2. We will denote this operation by
REPLACE (text, pattern1, pattern2) .
Ex. REPLACE(‘XABYABZ’, ‘AB’,’C’) = ’XCYABZ’
REPLACE(‘XABYABZ’, ‘BA’,’C’) =’XABYABZ’
Specifically, the REPLACE function can be executed by using the following three steps:
K;=INDEX(T,P1)
T:=DELETE(T,K,LENGTH(P1)) INSERT(T,K,P2) Suppose we want to replace every occurrence of the
pattern Q.
This might be accomplished by repeatedly applying REPLACE(T,P,Q) until INDEX(T,P)=0
Algorithm:
A text T and patterns P and Q are in memory, this algorithm replaces every occurrence of P in T
by Q.
Step 1: [Find index of P.] Set K:=INDEX(T,P).
Step 2: Repeat while K!=0:
(a) [Replace P by Q.] Set T:=REPLACE(T,P,Q).
(b) [Update index.] Set K:=INDEX(T, P).
[End of Loop.]
Step 3: Write: T.
Step 4: Exit.

Ex: T=XABYABZ, P=AB, Q=C

Algorithm executed twice. During first execution, the first occurrence of AB in T is replaced by C to
Yield T=XCYABZ. During second execution, the remaining AB in T is replaced by C to yield T= XCYCZ.
AG&SG***MUNNI***Data Structures***Page 6
Hence XCYCZ is the output. Ex2. T=XAY, P=A, Q=AB Algorithm will never terminate

2) Insertion: Inserting a string in the middle of the text. Suppose in a given text T we want to insert a
string S so that S begins in position K. We denote this operation by INSERT (text, position, string)

Ex: INSERT(‘ABCDEFG’,3,’XYZ’) results-----‘ABXYZCDEFG’

INSERT (‘ABCDEFG’,6,’XYZ’) results-----’ABCDEXYZFG’

This insert function can be implemented by using the string operation as follows:

(algorithm for insertion)

INSERT (T, K, S) = SUBSTRING (T, 1, K-1) //S// SUBSTRING (T, K, LENGTH(T)-K+1)

This is the initial substring of T before the position K, which has length K-1, is concatenated with the
string S, and result is concatenated with the remaining part of T, which begins in position K and has
length

LENGTH(T)-(K-1) = LENGTH(T)-K+1

3) Deletion: Deleting a string from the text. Suppose in a given text T we want to delete the
substring which begins at position K and has length L. We denote this operation by DELETE(text,
position, length)

Ex: DELETE (‘ABCDEFG ‘, 4,2) = ’ABCFG’


DELETE (‘ABCDEFG’, 2,4) = ’AFG’
We assume that nothing is deleted if position K=0. Thus DELETE (‘ABCDEFG’, 0,2) =’ABCDEFG’
The delete function can be implemented using the string operations as follows.
DELETE (T, K, L) = SUBSTRING (T, 1, K-1)// SUBSTRING (T, K +L, LENGTH (T)-K-L+1)
That is. The initial substring of T before position K is concatenated with the terminal substring of T
beginning at position K+L. The length of the initial substring is K-1, and the length of the terminal
substring is:
LENGTH (T)- (K+L-1) = LENGTH (T)-K-L+1
We also assume that DELETE (T,K,L) = T when K = 0.Now suppose, in the text T, we first compute
INDEX(T,P), the position where P first occurs in T, and then we compute LENGTH (P), The number of
characters in P.
When INDEX(T,P)=0 the text T is not changed.

Ex1: Suppose T=’ABCEDEFG’ and P=’CD’.


Then INDEX(T,P)=3 and LENGTH (P)=2.
Hence DELETE (‘ABCDEFG’,3,2)=’ABEFG’

AG&SG***MUNNI***Data Structures***Page 7
Ex2: Suppose T=’ABCDEFG’ and P=’DC’.
Then INDEX(T,P)=0 and LENGTH(P)=2.
Hence, by the “zero case”
DELETE (‘ABCDEFG’,0,2) = ’ABCDEFG’
Algorithm:
A text T and pattern P are in memory. This algorithm deletes every occurrence of P in T.
Step 1: [ Find index of P.] Set K:=INDEX(T,P)
Step 2: Repeat while K!=0;
(a) [Delete P from T.]
Set T: =DELETE (T, INDEX (T, P), LENGTH (P))
(b) [Update index.] Set K: =Index (T, P).
[End of loop.]
Step 3: Write: T.
Step 4: Exit.
Ex. T=XABYABZ, P=AB Algorithm executed twice.
During first execution, the first occurrence of AB in T is deleted. Result T=XYABZ During the second
execution, the remaining occurrence of AB in T is deleted, so that T=XYZ Result is XYZ Ex2.
T=XAAABBBY P=AB
***PATTERN MATCHING ALGORITHM:
Pattern matching is the problem of deciding whether or not a given string pattern appears in a string
text T. We assume that the length of P does not exceed the length of T.
The First Pattern Matching Algorithm:
We compare a given pattern P with each of the substring of T, moving from left to right, until we get
a match

Wk=SUBSTRING(T,K,LENGTH(P))

Wk -Substring of T having the same length as P and beginning with the Kth character of T.

First we compare P, character by character, with the first substring, W1. If all the characters are the
same, then P=W1 and so P appears in T and INDEX(T,P)=1.
On the other hand suppose we find that some character of P is not the same as the corresponding
character of W1 . Then P!= W1, then we can immediately move on to the next substring, W2 . that is ,
we next compare P with W2 . if P!= W2, then we compare P with W3 , and so on. The process stops
(a)when we find a match of P with some substring Wk and so P appears in T and INDEX(T,P) = K or
(b) when we exhaust all the Wk ‘s with no match and hence P doesn’t appear in T. the maximum
value MAX if the subscript K is equal to LENGTH(T)-LENGTH(P)+1
Let us assume that P is a 4-character string and T is a 20character string, and that P and T appear in
memory as linear arrays with one character per element. That is,

P = P[1] P[2] P[3] P[4] AND T = T[1] T[2] T[3] ….T[19] T[20]

Then P is compared with each of the following 4 character substring of T

W1= T[1] T[2] T[3] T[4] W2=T[2] T[3] T[4]................W17=T[17] T[18] T[19] T[20]

Note that there are MAX = 20 – 4 + 1 = 17 such substrings of T

AG&SG***MUNNI***Data Structures***Page 8
Algorithm:
P and T are strings with lengths R and S, Respectively, and are stored as arrays with one character
per element. This algorithm finds the INDEX of P in T.
Step1: [Initialize.] Set K:=1 and MAX:=S-R+1.
Step 2: Repeat Steps 3 to 5 while K<=MAX:
Step 3: Repeat fir L=1 to R: [Tests each character of P.]
If P[L]!=[K+L-1]. Then : Go to step 5.
[End of inner loop.]
Step 4: [Success.] Set INEDX=K, and Exit.
Step 5: K:=K+1.
[End of step 2 outer loop.]
Step 6: [Failure.] Set INDEX=0.
Step 7: Exit.

COMPLEXITY:

Second Pattern Matching


Algorithm:
Second pattern matching
algorithm uses a table which
is derived from a particular
pattern P but is independent of
the text T. Example: Suppose
P = aaba Suppose T = T1, T2,
T , where T1 denotes the ith
character of T; and suppose
the first two characters of T
match those of P; i.e. ,
suppose T = aa Then T has
one of the following three
forms: T = aab , T = aaa ,T =
aax

AG&SG***MUNNI***Data Structures***Page 9
Where x is any character different from a or b. Suppose we read T3 and find that T3 = b. Then we
next read T4, to see if T4 = a, which will give a match of P with W1 .
On the other hand, suppose T3 = a. Then we know that P ! = W1 ; but we also know that W2 = aa , i.e.
, that the first two characters of the substring W2 match those of P.
Hence we next read T4 to see if T4 = b. Last, suppose T3 = x, Then we know that P! = W1 , but we
also know that P ! = W2 and P! = W3 , since x does not appear in P. Hence we next read T4 to see
if T4 = a, i.e., to see if the first character of W4 matches the first character of P

AG&SG***MUNNI***Data Structures***Page 10
AG&SG***MUNNI***Data Structures***Page 11
ARRAYS, RECORDS AND POINTERS
Data structure is classified as either linear or non linear, there are two ways of representing linear
structures in memory. One way is to have linear relationship between the elements represented by
means of sequential memory location. These are called array. The other way is to have linear
relationship between the elements are represented by means of pointer or links. These structures
are called linked list. Here we discuss array and different operation performed on array.

AG&SG***MUNNI***Data Structures***Page 12
***REPRESENTATION OF LINEAR ARRAYS IN MEMORY:
A linear array is a list of finite numbers n of homogenous data elements. Array has a set of index
and values.

data structure. For each index, there is a value associated with that index. Index is used to reference
an element in memory location. Values are data elements stored in memory. representation
(possible)implemented by using consecutive memory.

The number n of elements is called the length or size of the array, if not defined we assume
index started from 1,2…..,n. in general the length or the number of data elements of array can be
obtained from index set by formula. Length = UB – LB + 1

Where UB is upper bound the largest index, and LB is lower bound the smallest index. elements of
an array A may be denoted by subscript notation A1, A2, An

Some programming languages e.g ( Fortran and Pascal ) allocate memory space for arrays

AG&SG***MUNNI***Data Structures***Page 13
statically during program compilation hence the size of array is fixed during program execution. On
the other hand some programming languages allows one to read an integer n and then declare an
array with n elements; such programming languages are said to allocated memory dynamically.

The elements of linear array are stored in consecutive memory locations. The computer does
not keep track of address of each element of array. It only keeps track of the base address of
the array and on the basis of this base address the address or location of any element can be
found. We can find out the location of any element by using following formula:

LOC (LA [K]) = Base (LA) + w (K – LB)


Here LOC (LA [K]) is the location of the Kth element of LA.
Base (LA) is the base address of LA.
w is the number of bytes taken by one element.
K is the Kth element.
LB is the lower bound.

Suppose we want to find out Loc (A [3]). For it, we have:


Base (A) = 1000
w = 2 bytes (Because an integer takes two bytes in the memory).
K=3
LB = 1

After putting these values in the given formula, we get:


LOC (A [3]) = 1000 + 2 (3 – 1)
= 1000 + 2 (2)
= 1000 + 4
= 1004
****OPERATIONS ON ARRAYS: Various operations that can be performed on an array

• Traversing

• Insertion

• Deletion

AG&SG***MUNNI***Data Structures***Page 14
• Sorting

• Searching

• Merging

1)Traversing Linear Array:


Let A be a collection of data elements stored in the memory of the computer. Suppose we want
to print the content of each element of A or suppose we want to count the number of elements
of A, this can be accomplished by traversing A, that is, by accessing and processing each
element of a exactly ones. The following algorithm traverses a linear array LA. The simplicity of
the algorithm comes from the fact that LA is a linear structure. Other linear structures, such as
linked list, can also be easily traversed. On the other hand, traversal of nonlinear structures, such as
trees and graph, is considerably more complicated.

Algorithm: (Traversing a Linear Array)

Here LA is a linear array with lower bound LB and upper bound UB.

This algorithm traverses LA applying an operation PROCESS to each element of LA.

1. [Initialize counter] Set k: =LB.

2. Repeat steps 3 and 4 while k <=UB.

3. [Visit Element] Apply PROCESS to LA [k].

4. [Increase Counter] Set k: =k + 1. [End of step 2 loop]

5. Exit

Complexity of Traversal in a Linear Array

Traversal operation results in visiting every element of the linear array once. In the algorithm the
following is the way the steps are counted.

1. Step 1 is executed once, so it contributes 1 to complexity function f(n)

2. Step 2 is a loop control step that executes step 3 and step 4 N times (once for each
element of array DATA having N elements)

So the f(n) can be defined

as f(n)= 2*n+1 or

f(n)=2n+1

The highest order term in this function defined in the terms of input size of the algorithm is n, so
we can say that the complexity of this algorithm is O(n). It also means that the time of traversal of
linear array grows linearly with the size of array DATA.

AG&SG***MUNNI***Data Structures***Page 15
2,3.Insertion and Deletion in Linear Array:

Let A be a collection of data elements in the memory of the computer. ―Inserting‖ refers to the
operation of adding another element to the collection A, and ―deleting‖ refers to the operation
of removing one element from A. Inserting an element at the end of the linear array can be easily
done provided the memory space allocated for the array is large enough to accommodate the
additional element. On the other hand, suppose we need to insert an element in the middle of the
array. Then, on the average, half of the elements must be moved downward to new location to
accommodate the new elements and keep the order of the other elements. Similarly, deleting an
element at the end of the array presents no difficulties, but deleting the element somewhere in
the middle of the array requires that each subsequent element be moved one location upward in
order to fill up the array. The following algorithm inserts a data element ITEM in to the Kth position
in the linear array LA with N elements.

Algorithm for Insertion: (Inserting into 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. The algorithm inserts an element ITEM into the Kth position in LA.

1. [Initialize counter] Set J: = N.

2. Repeat Steps 3 and 4 while j >= k;

3. [Move jth element downward.] Set LA [J + 1]: =LA [J].

4. [Decrease counter] Set J: = J-1 [End of step 2 loop]

5. [Insert element] Set LA [K]:=ITEM.

6. [Reset N] Set N:=N+1

7. EXIT.

For example: Insertion in Linear Array-an element at the beginning of the array
In this case we have to move all the elements one position backwards to make a hole at the
beginning of array. Though the insertion process is not difficult but freeing the first location for new
element involves movement of all the existing elements. This is the worst-case scenario in insertion
in a linear array.

In the below example, array elements from index 1 to index 8 have to moved one position backwards
so that the new element 44 can be stored at index 1

Initially

AG&SG***MUNNI***Data Structures***Page 16
After movement of elements backwards

After insertion

Insertion in Linear Array- at the end of the array

In this case we don’t have to move any elements since the action here will be just to append the
element at the location after the last element. This is the best-case scenario.

In the example array no elements are moved. The new element is stored in index 9.

Initially

After Insertion

Insertion in Linear Array- at the give position J

Let J be any location in the array ofone element already existing. We have to add the new element at
J position. To do this, starting from J, every element is moved one place backwards so that a hole is
created at J and new element can be inserted here. This is the average case scenario. In the
example array, elements from index J (4) to index 8 have to moved one position backwards so that a
new element can be stored at index J(4)

Initially

AG&SG***MUNNI***Data Structures***Page 17
After movement of elements

After Insertion

Algorithm for Deletion: (Deletion from a 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

1. Set ITEM: = LA [k].

2. Repeat for J = K to N – 1. [Move J + 1st element upward] Set LA [J]: = LA [J +1]. [End of loop]

3. [Reset the number N of elements in LA] Set N: = N-1

4. EXIT

For example:

Deletion in array means removing an element and replacing it with the next element or element
present at next index. It involves three cases

Deletion in Array- an element at the beginning of the array

In this case we have to move all the elements one position forward to fill the position of the
element at the beginning of array. Though the deletion process is not difficult but moving all
elements one position forward involve movement of all the existing elements except the one being
deleted. This is the worst case scenario in deletion in a linear array.

In the example array elements from index 1 to index 8 have to moved one position forwards so
AG&SG***MUNNI***Data Structures***Page 18
that

AG&SG***MUNNI***Data Structures***Page 19
the first element is replaced by second, second by third and so on

Deletion in Array – an element at the end of the array

In this case we don’t have to move any elements since the action here will be just removing the last
element. This is done by redefining the index of last element of linear array = N-1. This is the
best case scenario in deletion in a linear array. In the example array no elements are moved.
The last element is removed by setting the index of last element as 7

Deletion in Array – an element at the give position J

Let J be any location in the array for one existing element. We have to delete the element at J
position. To do this starting from J every element is moved one place forward so that the
element after index J comes to position of J th element. This is the average case scenario in deletion
in linear array.

AG&SG***MUNNI***Data Structures***Page 20
In the example array ,elements from index J (4) to index 8 have to moved one position forward
so that an element at index 4 is replaced by element at index 5. Similarly element at 6 th postion
comes at 5th position, element at 7th position comes at 6th position and element at 8 th position
replaces element at 7th position completing the deletion process

Complexity of Deletion in Array Algorithm

Deletion operation results deleting one element of a linear array . In the algorithm the following is the
way the steps are counted.

1. Steps 1, 2 and 6 are executed once, so they contributes 3 to complexity function f(n)

2. Step 3 is a loop control step that executes step 4 and step 5 N times (once for each
element of array DATA after the element to be deleted)

So the f(n) can be defined

as f(n)= 2*n+3 or

f(n)=2n+3

The highest order term in this function defined in the terms of input size of the algorithm is n, so
we can say that the complexity of this algorithm is O(n). It also means that the average time of
element deletion in array grows linearly with the size of array DATA.

4.BUBBLE SORT:

The bubble sort algorithm compares two adjacent elements and swaps them if they are not in the
intended order.

Algorithm for bubble sort:

AG&SG***MUNNI***Data Structures***Page 21
Working of Bubble Sort Algorithm

sort the elements in ascending order.

1. First Iteration (Compare and Swap)

1. Starting from the first index, compare the first and the second elements.

2. If the first element is greater than the second element, they are swapped.

3. Now, compare the second and the third elements. Swap them if they are not in

order. The above process goes on until the last element.

2. Remaining Iteration

The same process goes on for the remaining iterations. After each iteration, the largest element
among the unsorted elements is placed at the end

Complexity of Bubble Sort

Cycle Number of comparisons Here, number of comparisons


(n - 1) + (n - 2) + (n - 3) +...+ 1 = n(n - 1) / 2
1
st n-1 nearly equals to n2
Nd n-2 Hence, Complexity: O(n2)
Also, if we simply observe the number of loops. The
3
rd n-3 algorithm implements two loops. Hence, the complexity is n*n =
n2
….. …..
1. Time Complexities
Last 1 • Worst Case Complexity: O(n2)
If we want to sort in ascending order and the array is in
• Best Case Complexity: O(n)
descending order then, the worst case occurs.
If the array is already sorted, then there is no need for sorting.

AG&SG***MUNNI***Data Structures***Page 22
• Average Case Complexity: O(n2)
It occurs when the elements of the array are in jumbled order (neither ascending nor
descending).

2. Space Complexity
• Space complexity is O(1) because an extra variable temp is used for swapping.
• In the optimized bubble sort algorithm, another variable swapped is used. Hence, the space
complexity will be O(2).

For example:

5.SEARCHING: (LINEAR AND BINARY SEARCHING)

Search is a process of finding a value in a list of values. In other words, searching is the process of
locating given value position in a list of values.

Linear Search Algorithm (Sequential Search Algorithm)

Linear search algorithm finds a given element in a list of elements with O(n) time complexity
where n is total number of elements in the list. This search process starts comparing search
element with the first element in the list. If both are matched then result is element found otherwise
search element is compared with the next element in the list. Repeat the same until search element

AG&SG***MUNNI***Data Structures***Page 23
is compared with the last element in the list, if that last element also doesn't match, then the result is
"Element not found in the list". That means, the search element is compared with element by
element in the list.

Linear search is implemented using following steps...

• Step 1 - Read the search element from the user.

• Step 2 - Compare the search element with the first element in the list.

• Step 3 - If both are matched, then display "Given element is found!!!" and terminate the
function

• Step 4 - If both are not matched, then compare search element with the next element in the
list.

• Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list.

• Step 6 - If last element in the list also doesn't match, then display "Element is not found!!!"
and terminate the function.

Algorithm:

Example
Consider the following list of elements and the element to be searched...

AG&SG***MUNNI***Data Structures***Page 24
AG&SG***MUNNI***Data Structures***Page 23
Binary Search Algorithm

Binary search algorithm finds a given element in a list of elements with O(log n) time complexity
where n is total number of elements in the list. The binary search algorithm can be used with only a
sorted list of elements. That means the binary search is used only with a list of elements that
are already arranged in an order. The binary search cannot be used for a list of elements
arranged in random order. This search process starts comparing the search element with the middle
element in the list. If both are matched, then the result is "element found". Otherwise, we check
whether the search element is smaller or larger than the middle element in the list. If the search
element is smaller, then we repeat the same process for the left sub-list of the middle element.
If the search element is larger, then we repeat the same process for the right sub-list of the
middle element. We repeat this process until we find the search element in the list or until we
left with a sub-list of only one element. And if that element also doesn't match with the search
element, then the result is "Element not found in the list".

Binary search is implemented using following steps...

• Step 1 - Read the search element from the user.

• Step 2 - Find the middle element in the sorted list.

• Step 3 - Compare the search element with the middle element in the sorted list.

• Step 4 - If both are matched, then display "Given element is found!!!" and terminate the
function.

• Step 5 - If both are not matched, then check whether the search element is smaller or larger
than the middle element.

• Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for
the left sub-list of the middle element.

• Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the
right sub-list of the middle element.

• Step 8 - Repeat the same process until we find the search element in the list or until sub-list
contains only one element.

• Step 9 - If that element also doesn't match with the search element, then display "Element is
not found in the list!!!" and terminate the function.

Example

Consider the following list of elements and the element to be searched...

AG&SG***MUNNI***Data Structures***Page 24
AG&SG***MUNNI***Data Structures***Page 25
****TYPES OF ARRAYS

• One-dimensional Array
• Two-dimensional Array
• Three-dimensional Array
Two dimensional and three dimensional arrays are also called multi-dimensional arrays. in types of
arrays, multi-dimensional arrays also include arrays with four and higher dimensions.

One-dimensional Array

In a one-dimensional array the elements are stored in contiguous memory locations where each
element is accessed by using a single index value. It is a linear data structure storing all the
elements in sequence.

The elements are stored in memory in continuation and the variable declared as an array is actually
a pointer to the address of first element of the array. This address is called the base address.
The address of any other element can be calculated with the following formula
ADDRESS(ARRAY[K])= BASEADDRESS(ARRAY)+ WORDLENGTH *( LOWERBOUND-K)

Where

AG&SG***MUNNI***Data Structures***Page
• ADDRESS -memory location of the Kth element of the array (To be calculated)
• ARRAY – name of the ARRAY
• WORDLENGTH – number of bytes required to store one element depending upon its data type like a
character value needs 1 byte and an integer value needs 2 bytes.
• LOWERBOUND – index of the first element of the array.
• BASEADDRESS– Address of first element of the
array
• In this example a snap shot of the memory
displays how an array of size 8 is stored in
memory. To calculate memory address of a given
element say 6th element of the array you will put the
values in the previous formula
K=6
WORDLENGTH=2 (integer data)
LOWERBOUND =1 (index of first element of the
array) BASEADDRESS = 1001
Putting these values in formula
this is the address of
ADDRESS(ARRAY[6])= 1001+ 2*( 6-1) =1011
memory location where 6 element (34) is stored as visible
th

in the figure above

Two-dimensional Array

In types of arrays, a two dimensional array is a tabular representation of data where elements
are stored in rows and columns. A two dimensional array is actually a collection of M X N
elements which has M rows and N columns. To access any element in a two-dimensional array
two subscripts are required for defining position of an element in a specific row and column. The
first index is for row number and second is for column index. In the example shown the first index
values row=2 column=3 is used to access element 56.

Two dimensional arrays are stored in memory in two


representations

Row Major Representation

In the row major representation the storage of array elements


takes place row wise. All elements of first row of the array are
first stored in sequence followed by second row and then
third, fourth and so on. The 2-dimensional array given in
previous examples is stored in row-major representation in the
figure below.

To find the Address of any element located at I th row and Jth column is calculated by using the
formula

ADDRESS(ARRAY[I,J])= BASEADDRESS(ARRAY)+ WORDLENGTH *( N*(I-1)+ (J-1))

AG&SG***MUNNI***Data Structures***Page
Where

• ADDRESS – memory location of the element at Ith row and Jth columnof the array (To be calculated)
• ARRAY – name of the ARRAY
• WORDLENGTH – number of bytes required to store one element depending upon its data type like a
character values needs 1 byte and an integer value needs 2 bytes.
• N – number of columns of the array.
• BASEADDRESS – Address of first element of the 2-D array
In this example to calculate memory address of a given element (44) says in 1st row and 3rd column
you will put the values in the formula.

I=1, J=3

N=4

WORDLENGTH=1 (assuming only one byte is required to store these small ints

) BASEADDRESS = 1001

ADDRESS(ARRAY(6))= 1001+ 1*( 4*(1-1) + (3-1)) =1003 this is the address of memory location
where 44 is stored as visible in the previous figure

Column Major Representation

In the column major representation, the storage of array elements takes place column wise. All
elements of first column of the array are first stored in sequence followed by second column and
then third, fourth and so on. The 2-dimensional array given in previous examples is stored in column-
major representation in the figure below.

To find the Address of any element located at Ith row and jth column is calculated by using the
formula

ADDRESS(ARRAY[K])= BASEADDRESS(ARRAY)+ WORDLENGTH *( M*(J-1)+ (I-1))

• ADDRESS – memory location of the Ith and Jth element of the array (To be calculated)

AG&SG***MUNNI***Data Structures***Page
• ARRAY – name of the ARRAY
• WORDLENGTH – number of bytes required to store one element depending upon its data
type like a character values needs 1 byte and an integer value needs 2 bytes.
• M – number of rows of the array.
• BASEADDRESS -Address of first element of the array
In this example to calculate memory address of a given element (44) say in 1st row and 3rd column
you will put the values in the formula.
I=1, J=3
M=3
WORDLENGTH=1 (assuming only one byte is required to store these small ints
) BASEADDRESS = 1001
ADDRESS(ARRAY(6))= 1001+ 1*( 3*(3-1) + (1-1))
=1007 this is the address of memory location where 44 is stored as visible in the previous figure

Three-dimensional Array
In types of arrays, a three-dimensional array is an extension to the two dimensional array with
addition of depth. It can be seen as a cube that has rows, columns and depth as third dimension. To
access any element in a three-dimensional array three subscripts are required for position of
element in a specific row, column and depth. The first index is for depth (dimension or layer), second
is for row index and third is for column. In the example shown the index values (2,0,3) is used to
access element 24.

**POINTER ARRAYS: Array of pointers can be used to point to an array of data items with
each element of the pointer array pointing to an element of the data array.

AG&SG***MUNNI***Data Structures***Page
***RECORD STRUCTURES:

AG&SG***MUNNI***Data Structures***Page
AG&SG***MUNNI***Data Structures***Page
AG&SG***MUNNI***Data Structures***Page
***MATRICES:

Matrix, a set of numbers arranged in rows and columns so as to form a rectangular array. The
numbers are called the elements, or entries, of the matrix.

Matrices are representing using two dimensional arrays.

AG&SG***MUNNI***Data Structures***Page

You might also like