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

Chapter 4 - Abstract Data Types

The document discusses data structures and algorithms. It provides an overview of topics to be covered in a course on data structures and algorithms taught by Dr. Irfana Memon at QUEST. The topics include string processing, abstract data types, linked lists, stacks, queues, recursion, complexity analysis, sorting/searching techniques, trees, and graphs. It also provides the course outline listing the topics and intended learning outcomes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Chapter 4 - Abstract Data Types

The document discusses data structures and algorithms. It provides an overview of topics to be covered in a course on data structures and algorithms taught by Dr. Irfana Memon at QUEST. The topics include string processing, abstract data types, linked lists, stacks, queues, recursion, complexity analysis, sorting/searching techniques, trees, and graphs. It also provides the course outline listing the topics and intended learning outcomes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

DATA STRUCTURE

AND
ALGORITHM

Dr. Irfana Memon


Department of CSE, QUEST

https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
1
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

2
Abstract data types

3
•Data structures are classified as either linear or nonlinear.
•A data structure is said to be linear if its elements form a sequence
or a linear list.
•There are two basic ways of representing such linear structures in
memory.
•One way is to have the linear relationship between the elements
represented by means of sequential memory locations. These linear
structures are called arrays.
•The other way is to have the linear relationship between the
elements represented by means of pointers or links. These linear
structures are called linked lists.
•Nonlinear structures are trees and graphs.

4
• 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
array , It can be computed as upper bound - lower bound 5 +1
• 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 linear array DATA consisting of the name of six elements
DATA
DATA[1] = 247
1 247
2 56 DATA[2] = 56
3 429
4 135
DATA[3] = 429
5 87 DATA[4] = 135
6 156
DATA[5] = 87
DATA[6] = 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 =

7
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

8
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
1000 needs to keep track only of the first
1001 element of LA, denoted by
1002
Base(LA)
1003
1004 Called the base address of LA. Using this
1005
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)
Fig : Computer
Where w is the number of words per
memory
memory cell for the array LA 9
200
201 Example :
202 An automobile company uses an array
AUTO[1932]
203 AUTO to record the number of auto
204 mobile sold each year from 1932
205 through 1984. Suppose AUTO appears
206 AUTO[1933] in memory as pictured in fig A . That is
207 Base(AUTO) = 200, and w = 4 words per
208 memory cell for AUTO. Then,
209 LOC(AUTO[1932]) = 200,
AUTO[1934] LOC(AUTO[1933]) =204
210
211 LOC(AUTO[1934]) = 208
212 the address of the array element for
the year K = 1965 can be :

Fig : A 10
200 Example :
201
An automobile company uses an array
AUTO to record the number of auto
202
AUTO[1932] mobile sold each year from 1932
203
through 1984. Suppose AUTO appears
204
in memory as pictured in fig A . That is
205
Base(AUTO) = 200, and w = 4 words per
206 AUTO[1933] memory cell for AUTO. Then,
207
LOC(AUTO[1932]) = 200,
208
LOC(AUTO[1933]) =204
209
AUTO[1934] LOC(AUTO[1934]) = 208
210 the address of the array element for the
211 year K = 1965 can be obtained by using
212 :
LOC(AUTO[1965]) = Base(AUTO) +
w(1965 – lower bound)
Fig : A =200+4(1965-1932)=332 11
Example: We have an array AAA(5:50). Find the Location of
AAA(5)

where Base=300, and w=4

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)

13
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

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

where
Base=300, and w=4

15
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

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(15-5) = 340

17
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

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)

19
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 20
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

21
• 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.

22
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.

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.

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.

Draw Flowchart for the above algorithm


25
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. 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

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.

28
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.

29
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 30


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. 31
•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
32
Algorithm: 2. INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)

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.

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

35
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
36
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.

37
•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
1 A
1 A
2 B
C
2
3 C
D
3
4 D
4
5
5
6
6

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

39
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


40
• Arranging the elements in some type of order.

• Popular sorting algorithms are:

✓Selection Sort
✓Bubble Sort
✓Insertion Sort
✓Merge Sort
✓Quick Sort
✓Topological Sort

41
 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

42
• 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.

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

44
45
46
47
48
• 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)

49
FIGURE C View of Three-dimensional Array
50
Wish
You
Good Luck

51

You might also like