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

Chapter 3- Abstract Data Types

The document provides an overview of abstract data types (ADTs) and linear arrays, detailing their definitions, properties, and operations such as traversal, insertion, and deletion. It includes examples of how to calculate memory locations for elements in arrays and algorithms for processing data. Additionally, it discusses the structure of arrays and the implications of inserting elements within them.

Uploaded by

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

Chapter 3- Abstract Data Types

The document provides an overview of abstract data types (ADTs) and linear arrays, detailing their definitions, properties, and operations such as traversal, insertion, and deletion. It includes examples of how to calculate memory locations for elements in arrays and algorithms for processing data. Additionally, it discusses the structure of arrays and the implications of inserting elements within them.

Uploaded by

syedafatmah124
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

NO TOPIC CLO

01 A General Overview CLO1


02 Introduction to Data Structures and Algorithm CLO1
03 String Processing CLO1
04 Abstract Data Types CLO1
05 Linked list CLO1
06 Stack and Queue CLO1
07 Recursion CLO1
08 Complexity Analysis CLO2
09 Sorting and Searching techniques CLO2
10 Trees CLO2
11 Graph CLO3
12 P & NP CLO3

1
Abstract data types

2
•An abstract data type or ADT (sometimes called an abstract
data type) is a mathematical model of a data structure.
•It describes a container which holds a finite number of
objects where the objects may be associated through a given
binary relationship.
•The operations which may be performed on the container
may be basic (e.g., insert, remove, etc.) or may be based on the
relationship (e.g, given an object (possibly already in the
container), find the next largest object).
• Thus, to describe an abstract data structure we must:
 Give the relationship between the objects being stored
 Specify the operations which are to be optimized with respect
to time and/or space.
• Array and the linked list are typical examples. 3
• A linear array is a list of finite number n of homogeneous data
elements such that :
a) The elements of the array are referenced respectively by an
index set consisting of n consecutive numbers.
b) 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.

• Three numbers define an array : lower bound, upper bound,


size.
a. The lower bound is the smallest subscript you can use in the array
(usually 0)
b. The upper bound is the largest subscript you can use in the array
c. The size / length of the array refers to the number of elements in the
• Let, Array name is A then the elements of A is : a1,a2….. an
Or by the bracket notation A[1], A[2], A[3],………….,
A[n]
• The number k in A[k] is called a subscript and A[k] is
called a
subscripted variable.
Example :
A linearDATA
array DATA consisting of the name of six elements
DATA[0] = 247
0 247
1 56 DATA[1] = 56
2 429
3 135 DATA[2] = 429
4 87
5
DATA[3] = 135
156
DATA[4] = 87
DATA[5] = 156 6
Example :
An automobile company uses an array AUTO to record the
number of auto mobile sold each year from 1932 through 1984.
AUTO[k] =
LB =
UB =
Length =

6
Example :
An automobile company uses an array AUTO to record the
number of auto mobile sold each year from 1932 through 1984.
AUTO[k] = Number of auto mobiles sold in the year K
LB = 1932
UB = 1984
Length = UB – LB+1 = 1984 – 1930+1 =55

7
Let LA be a linear array in the memory of the computer. The
memory of the computer is a sequence of addressed locations.
The computer does not need to keep track
LA of the address of every element of LA, but
100 needs to keep track only of the first
0 element of LA, denoted by
100
1 Base(LA)
100
2 Called the base address of LA. Using this
100 address Base(LA), the computer calculates
3 the address of any element of LA by the
100
4
following formula :
100 LOC(LA[k]) = Base(LA) + w(K – lower
5
bound)
Fig : Computer
Where w is the number of words per
memory
20
0
Example :
20 An automobile company uses an array
1 AUTO[193
2] AUTO to record the number of auto
20
2
mobile sold each year from 1932
through 1984. Suppose AUTO appears
20
3 AUTO[193 in memory as pictured in fig A . That is
20 3] Base(AUTO) = 200, and w = 4 words per
4 memory cell for AUTO. Then,
20 LOC(AUTO[1932]) = 200,
AUTO[193 LOC(AUTO[1933]) =204
5
4]
20 LOC(AUTO[1934]) = 208
6 the address of the array element for
20 the year K = 1965 can be :
7
20
8 Fig : 1
20 Example :
0 An automobile company uses an array
20 AUTO to record the number of auto
1 AUTO[193 mobile sold each year from 1932
20 2] through 1984. Suppose AUTO appears
2 in memory as pictured in fig A . That is
20 Base(AUTO) = 200, and w = 4 words per
3 AUTO[193 memory cell for AUTO. Then,
20 3] LOC(AUTO[1932]) = 200,
4 LOC(AUTO[1933]) =204
20 LOC(AUTO[1934]) = 208
AUTO[193
5 the address of the array element for the
4]
20 year K = 1965 can be obtained by using
6
:
20 LOC(AUTO[1965]) = Base(AUTO) +
7
w(1965 – lower bound)
20
8 Fig :
=200+4(1965-1932)=332 1
Example: We have an array AAA(5:50). Find the Location of
AAA(5)

where Base=300, and w=4

11
Example: We have an array AAA(5:50). Find the Location
of AAA(5) where

Base=300, and w=4

By the Formula

Loc (LA[K]) = Base(LA) +w (K-LB)

12
Example: We have an array AAA(5:50). Find the Location of
AAA(5) where

Base=300, and w=4

By the Formula

Loc (LA[K]) = Base(LA)

+w(K-LB) Loc(AAA[5])= 300 +

4(5-5) = 300

13
Example: We have an array AAA(5:50). Find the Location of
AAA(15)

where
Base=300, and w=4

14
Example: We have an array AAA(5:50). Find the Location of
AAA(15) where
Base=300, and w=4

By the Formula

Loc (LA[K]) = Base(LA)+w(K-LB)

Loc(AAA[15])= 300 + 4(15-5) = 340

15
Example: We have an array AAA(5:50). Find the Location of
AAA(35) where

Base=300, and w=4

By the Formula

Loc (LA[K]) =
Base(LA)+w(K-LB)

Loc(AAA[15])= 300
+ 4(15-5) = 340

16
Example: We have an array AAA(5:50). Find the Location of
AAA(35) where

Base=300, and w=4

By the Formula

Loc (LA[K]) =
Base(LA)+w(K-LB)

Loc(AAA[15])= 300
+ 4(35-5) = 420

17
Example: We have an array BBB(-5:50), ) where Base=300,
and w=4

Find the Location of following:

(i) BBB(0)
(ii) BBB(5)
(iii) BBB(8)
(iv) BBB(18)
(v) BBB (20)

18
Example: We have an array BBB(-5:50), ) where Base=300, and
w=4

Find the Location of following:


(i) BBB(0)
(ii) BBB(5)
(iii) BBB(8)
(iv) BBB(18)
(v) BBB (20)

By the Formula: Loc (LA[K]) = Base(LA)+w(K-LB)

(i) Loc(BBB[0])= 300 + 4(0-(-5)) = 320


(ii) Loc(BBB[5])= 300 + 4(5-(-5)) = 340
(iii) Loc(BBB[8])= 300 + 4(8-(-5)) = 352
(iv) Loc(BBB[18])= 300 + 4(18-(-5)) = 392
(v) Loc(BBB[20])= 300 + 4(20-(-5)) = 400 2
1. Traversal: Processing each element in the list
2. Search: Finding the location of the element with a
given value of the record with a given key
3. Insertion: Adding a new element to the list
4. Deletion: Removing an element from the list
5. Sorting: Arranging the elements in some type of order
6. Merging: Combining two lists into a single list

20
•Print the contents of each element of DATA or Count
the number of elements of DATA with a given property.
•This can be accomplished by traversing DATA, That is,
by accessing and processing (visiting) each element of
DATA exactly once.

21
Algorithm 1: Given DATA is a linear array with lower bound LB
and upper bound UB . This algorithm traverses DATA applying an
operation PRINT to each element of DATA.

22
Algorithm 4.1: Given DATA is a linear array with lower bound
LB and upper bound UB . This algorithm traverses DATA applying
an operation PRINT to each element of DATA.

1. Set K : = LB.
2. Repeat steps 3 and 4 while K<=UB:
3. Write: DATA[K]
4. Set K : = K+1.
5. Exit.

23
Algorithm 4.1: Given DATA is a linear array with lower bound
LB and upper bound UB . This algorithm traverses DATA applying
an operation PRINT to each element of DATA.

1. Set K : = LB.
2. Repeat steps 3 and 4 while K<=UB:
3. Write: DATA[K]
4. Set K : = K+1.
5. Exit.

Draw Flowchart for the above algorithm


24
Algorithm 4.1: Given DATA is a linear array with lower bound
LB and upper bound UB . This algorithm traverses DATA applying
an operation PRINT to each element of DATA.

1. Set K : = LB.
2. Repeat steps 3 and 4 while K<=UB:
3. Write: DATA[K]
4. Set K : = K+1.
5. Exit.

Rewrite the above algorithm using for loop and also


draw flow chart. 2
Example 1 :

An automobile company uses an array AUTO to record the number


of auto mobile sold each year from 1932 through 1984.

a) Find the number NUM of years during which more than 300
automobiles were sold.

b) Print each year and the number of automobiles sold in that


year

26
Example 1 :
An automobile company uses an array AUTO to record the number
of auto mobile sold each year from 1932 through 1984.
a)Find the number NUM of years during which more than
300 automobiles were sold.
b) Print each year and the number of automobiles sold in that year

1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : =
NUM+1
3. Exit.

27
Example 1:
An automobile company uses an array AUTO to record the number
of auto mobile sold each year from 1932 through 1984.
a) Find the number NUM of years during which more than 300
automobiles were sold.
b)Print each year and the number of automobiles sold in
that year

1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : =
NUM+1
3. Exit.
1. Repeat for K = 1932 to
1984:
Write : K, AUTO[K]
2. Exit.
28
Example :
An automobile company uses an array AUTO to record the number
of auto mobile sold each year from 1932 through 1984.
a) Find the number NUM of years during which more than 300
automobiles were sold.
b)Print each year and the number of automobiles sold in
that year

1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : =
NUM+1
3. Exit.
1. Repeat for K = 1932 to
1984:
Write : K, AUTO[K]
2. Exit.
Draw Flowchart for the above algorithms 3
Example :
An automobile company uses an array AUTO to record the number
of auto mobile sold each year from 1932 through 1984.
a) Find the number NUM of years during which more than 300
automobiles were sold.
b)Print each year and the number of automobiles sold in
that year

1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : =
NUM+1
3. Exit. 1.Repeat for K = 1932 to
1984: Write : K, AUTO[K]
2. Exit.
Rewrite the above algorithms using Repeat-while loop
and also draw flow chart. 3
•Inserting refers to the operation of adding another element to the Array
• Inserting an element somewhere in the middle of the array
require that each subsequent element be moved downward to new
locations to accommodate the new element and keep the order of the
other elements. Fig shows E Inserted.

STUDENT
STUDENT
A 1 A
1
B 2 B
2
C 3 E
3
D 4 C
4
5 D
5
6
6
31
Algorithm: 2. INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)

32
Algorithm: 2. INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)

Paragraph:
Here LA is 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.

33
Algorithm: 2. INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)

Paragraph:
Here LA is 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.

ALGORITHM
Step 1. [Initialize counter] Set J:=N
Step 2. Repeat Steps 3 and 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

34
Algorithm: 2. INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)

Paragraph:
Here LA is 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.

ALGORITHM
Step 1. [Initialize counter] Set J:=N
Step 2. Repeat Steps 3 and 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
Draw flowchart for above algorithm
35
Algorithm: 4.3. DELETING AN ELEMENT FROM A LINEAR ARRAY
Delete (LA, N, K, ITEM)

Paragraph:
Here LA is linear array with N elements and K is a positive integer
such that K<=N. This algorithm deletes an element ITEM from the
Kth position in LA.

36
•Deleting refers to the operation of removing one element from the Array
• Deleting an element somewhere from the middle of the array
require that each subsequent element be moved one location upward in
order to “fill up” the array. Fig shows B deleted.

STUDENT
STUDENT
A
1 A
B
1
2
C 2
3 C
D 3
4 D
4
5
5
6
6

37
Algorithm: 3. DELETING AN ELEMENT FROM A LINEAR ARRAY
Delete (LA, N, K, ITEM)

Paragraph:
Here LA is linear array with N elements and K is a positive integer
such that K<=N. This algorithm deletes an element ITEM from the
Kth position in LA.
ALGORITHM
Step 1. Set 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

38
Algorithm: 3. DELETING AN ELEMENT FROM A LINEAR ARRAY
Delete (LA, N, K, ITEM)

Paragraph:
Here LA is linear array with N elements and K is a positive integer
such that K<=N. This algorithm deletes an element ITEM from the
Kth position in LA.
ALGORITHM
Step 1. Set 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

Draw flowchart for above algorithm


39
•Arranging the elements in some type of order.

•Popular sorting algorithms are:

Selection Sort
Bubble Sort
Insertion Sort
Merge Sort
Quick Sort
Topological Sort

40
⦿ There are a variety of situations that we can
encounter
◾ Do we have randomly ordered keys?
◾ Are all keys distinct?
◾ How large is the set of keys to be ordered?
◾ Need guaranteed performance?

⦿ Various algorithms are better suited to some of


these situations

We will discuss existing sorting algorithm in next chapter

4
•Many applications require that data be stored in more
than one dimension.
• One common example is a table, which is an array
that
consists of rows and columns.
•Two dimensional arrays are declared similarly to one
dimensional arrays:

The first subscript gives the row number, and the second
subscript specifies column number.

42
FIGURE: Two-dimensional Array FIGURE : Array Of Arrays

43
44
45
46
47
•Multidimensional arrays can have three, four, or more
dimensions.
•The first dimension is called a plane, which consists of rows and
columns.
•The C language considers the three-dimensional array to be an
array of two-dimensional arrays.

FIGURE A Three-dimensional
Array (3 x 5 x 4)

48
FIGURE C View of Three-dimensional Array
49

You might also like