[go: up one dir, main page]

0% found this document useful (0 votes)
37 views36 pages

Data Structures: Hapter

Uploaded by

munshijubair7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views36 pages

Data Structures: Hapter

Uploaded by

munshijubair7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Data Structures

Chapter 04
9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 1
Data Structures- Chapter 4
❑ Linear Array

– A linear array is a list of finite number N of homogeneous data


elements such that:
• The elements of the array are referenced respectively by an index
set consisting of N consecutive numbers.
• 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.

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 2


Data Structures- Chapter 4
❑ Linear Array
– Three numbers define an array: Lower Bound, Upper Bound, and
Size.
• The lower bound is the smallest subscript you can use in the array
(usually 0).
• The upper bound is the largest subscript you can use in the array.
• The size / length of the array refers to the number of elements in the
array. It can be computed as
• Length= Upper bound - Lower bound + 1

– Let the array name be A. Then the elements of A are: 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.
9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 3
Data Structures- Chapter 4
❑ Linear Array
Example:
– A linear array DATA consisting of six elements:

DATA
1 247 DATA [1] = 247
2 56 DATA [2] = 56
3 429 DATA [3] = 429
4 135 DATA [4] = 135
5 87 DATA [5] = 87
6 156 DATA [6] = 156

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 4


Data Structures- Chapter 4
❑ Linear Array
Example:
– An automobile company uses an array AUTO to record the number of
auto mobile sold each year from 1932 through 1984.

– LB = 1932, UB = 1984

– AUTO[k] = Number of auto mobiles sold in the year k.

– Length = UB – LB+1 = 1984 – 1930+1 =55

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 5


Data Structures- Chapter 4
❑ Operations on Linear Array

– Traversing: means to visit all the elements of the array in an


operation is called traversing.
– Insertion: means to put values into an array.
– Deletion / Remove: to delete a value from an array.
– Sorting: Re-arrangement of values in an array in a specific order
(Ascending / Descending) is called sorting.
– Searching: The process of finding the location of a particular
element in an array is called searching.
– There are two popular searching techniques/mechanisms: Linear
search and binary search.

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 6


Data Structures- Chapter 4
❑ Representation of Linear Array in Memory
– 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 of the address of every
element of LA, but needs to keep track only of the first element of LA,
denoted by
Base (LA) 1000 247
called the base address of LA. 1001 56
1002 429
– Using this address Base (LA), 1003 135
the computer calculates the address of 1004 87
1005 156
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
9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 7
Data Structures- Chapter 4
❑ Representation of Linear Array in Memory
Example: AUTO
An automobile company uses an array AUTO to record
200
the number of auto mobiles sold each year from 1932
through 1984. Suppose AUTO appears in memory as 201
pictured in Fig. That is, 202
Base(AUTO) = 200, and w = 4 words per memory cell, 203
LB=1932, UB=1984. Then, 204

LOC(AUTO[1932]) = 200, LOC(AUTO[1933]) =204 205


LOC(AUTO[1934]) = 208

The address of the array element for the year K = 1965


can be obtained by using:

LOC(AUTO[1965]) = Base(AUTO) + w(1965 – LB)


=200+4(1965-1932) =332

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 8


Data Structures- Chapter 4
❑ Solved Problems 4.1: Consider the linear arrays AAA(5:50), BBB(-5:10)
and CCC(18).
(a) Find the number of elements in each array.
(b) Suppose Base(AAA)=300 and w=4 words per memory cell for
AAA. Find the address of AAA[15], AAA[35] and AAA[55].

Solution:
(a) The number of elements is equal to the length; hence use the formula
Length = UB –LB+1
Accordingly, Length(AAA) = 50-5+1=46
Length(BBB) =10-(-5)+1=16
Length(CCC) = 18-1+1=18

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 9


Data Structures- Chapter 4
❑ Solved Problems 4.1: Consider the linear arrays AAA(5:50), BBB(-5:10)
and CCC(18).
(a) Find the number of elements in each array.
(b) Suppose Base(AAA)=300 and w=4 words per memory cell for
AAA. Find the address of AAA[15], AAA[35] and AAA[55].

Solution:
(b) Use the formula, LOC(AAA[K]) = Base(AAA) + w(K –LB)
Hence, LOC (AAA[15])=300+4(15-5)=340
LOC(AAA[35])=300+4(35-5)=420
AAA[55] is not an element of AAA, since 55 exceeds UB=50

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 10


Data Structures- Chapter 4
❑ Algorithm 4.1: (Traversing a Linear Array)
Here LA is a linear array with lower boundary LB and upper boundary 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.

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 11


Data Structures- Chapter 4
❑ Algorithm 4.1: (Traversing a Linear Array)
Here LA is a linear array with lower boundary LB and upper boundary UB.
This algorithm traverses LA applying an operation Process to each element
of LA.

1. Repeat for K=LB to UB


Apply PROCESS to LA[K].
[End of loop].
2. Exit.

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 12


Data Structures- Chapter 4
❑ Insertion into Arrays
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.
Algorithm 4.2: (Inserting into a linear array)
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 K-th position
in LA.
Step 1. Set J:=N
Step 2. Repeat Steps 3 and 4 while J>=K
Step 3. Set LA [J+1]: =LA [J]
Step 4. Set J:=J-1
[End of step 2 loop]
Step 5. Set LA [K]: =ITEM
Step 6. Set N:=N+1
Step 7. Exit
9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 13
Data Structures- Chapter 4
❑ Deletion of elements from linear arrays
Deleting an 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.

Algorithm 4.3: (Deleting from a linear array)


Here LA is a linear array with N elements and K is a positive integer such
that K≤N. This algorithm deletes the K-th element from LA.
Step 1. Set ITEM: = LA [K]
Step 2. Repeat for J=K to N-1
Set LA [J]: =LA [J+1]
[End of loop]
Step 3. Set N:=N-1
Step 4. Exit
9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 14
Data Structures- Chapter 4
❑ Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.

Pass 1: (a) Compare A1 and A2. Since 32< 51, the list is not altered.
A: 32, 51, 27, 85, 66, 23, 13, 57
(b) Compare A2 and A3. Since 51>27, interchange 51 and 27.
A: 32, 27, 51, 85, 66, 23, 13, 57
(c) Compare A3 and A4. Since 51<85, the list is not altered.
A: 32, 27, 51, 85, 66, 23, 13, 57
(d) Compare A4 and A5. Since 85>66, interchange 85 and 66.
A: 32, 27, 51, 66, 85, 23, 13, 57

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 15


Data Structures- Chapter 4
❑ Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.

Pass 1: (e) Compare A5 and A6. Since 85>23, interchange 85 and 23.
A: 32, 27, 51, 66, 23, 85, 13, 57
(f) Compare A6 and A7. Since 85>13, interchange 85and 13.
A: 32, 27, 51, 66, 23, 13, 85, 57
(g) Compare A7and A8. Since 85>57, interchange 85 and 57.
A: 32, 27, 51, 66, 23, 13, 57, 85

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 16


Data Structures- Chapter 4
❑ Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.

After Pass 1: A: 32, 27, 51, 66, 23, 13, 57, 85

Pass 2: 27, 32, 51, 66, 23, 13, 57, 85


27, 32, 51, 66, 23, 13, 57, 85
27, 32, 51, 66, 23, 13, 57, 85
27, 32, 51, 23, 66, 13, 57, 85
27, 32, 51, 23, 13, 66, 57, 85
27, 32, 51, 23, 13, 57, 66, 85

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 17


Data Structures- Chapter 4
❑ Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.

After Pass 1: A: 32, 27, 51, 66, 23, 13, 57, 85


After Pass 2: A: 27, 32, 51, 23, 13, 57, 66, 85

Pass 3: 27, 32, 51, 23, 13, 57, 66, 85


27, 32, 51, 23, 13, 57, 66, 85
27, 32, 23, 51, 13, 57, 66, 85
27, 32, 23, 13, 51, 57, 66, 85
27, 32, 23, 13, 51, 57, 66, 85

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 18


Data Structures- Chapter 4
❑ Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.

After Pass 1: A: 32, 27, 51, 66, 23, 13, 57, 85


After Pass 2: A: 27, 32, 51, 23, 13, 57, 66, 85
After Pass 3: A: 27, 32, 23, 13, 51, 57, 66, 85

Pass 4: 27, 32, 23, 13, 51, 57, 66, 85


27, 23, 32, 13, 51, 57, 66, 85
27, 23, 13, 32, 51, 57, 66, 85
27, 23, 13, 32, 51, 57, 66, 85

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 19


Data Structures- Chapter 4
❑ Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.

After Pass 1: A: 32, 27, 51, 66, 23, 13, 57, 85


After Pass 2: A: 27, 32, 51, 23, 13, 57, 66, 85
After Pass 3: A: 27, 32, 23, 13, 51, 57, 66, 85
After Pass 4: A: 27, 23, 13, 32, 51, 57, 66, 85

Pass 5: 23, 27, 13, 32, 51, 57, 66, 85


23, 13, 27, 32, 51, 57, 66, 85
23, 13, 27, 32, 51, 57, 66, 85

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 20


Data Structures- Chapter 4
❑ Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.

After Pass 1: A: 32, 27, 51, 66, 23, 13, 57, 85


After Pass 2: A: 27, 32, 51, 23, 13, 57, 66, 85
After Pass 3: A: 27, 32, 23, 13, 51, 57, 66, 85
After Pass 4: A: 27, 23, 13, 32, 51, 57, 66, 85
After Pass 5: A: 23, 13, 27, 32, 51, 57, 66, 85

Pass 6: 13, 23, 27, 32, 51, 57, 66, 85


13, 23, 27, 32, 51, 57, 66, 85

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 21


Data Structures- Chapter 4
❑ Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.

After Pass 1: A: 32, 27, 51, 66, 23, 13, 57, 85


After Pass 2: A: 27, 32, 51, 23, 13, 57, 66, 85
After Pass 3: A: 27, 32, 23, 13, 51, 57, 66, 85
After Pass 4: A: 27, 23, 13, 32, 51, 57, 66, 85
After Pass 5: A: 23, 13, 27, 32, 51, 57, 66, 85
After Pass 6: A: 13, 23, 27, 32, 51, 57, 66, 85

Pass 7: 13, 23, 27, 32, 51, 57, 66, 85

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 22


Data Structures- Chapter 4
❑ Bubble Sort
Suppose the following numbers are stored in an array A:
A: 32, 51, 27, 85, 66, 23, 13, 57
Apply the bubble sort to the array A. Show each pass separately.

After Pass 1: A: 32, 27, 51, 66, 23, 13, 57, 85


After Pass 2: A: 27, 32, 51, 23, 13, 57, 66, 85
After Pass 3: A: 27, 32, 23, 13, 51, 57, 66, 85
After Pass 4: A: 27, 23, 13, 32, 51, 57, 66, 85
After Pass 5: A: 23, 13, 27, 32, 51, 57, 66, 85
After Pass 6: A: 13, 23, 27, 32, 51, 57, 66, 85
After Pass 7: A: 13, 23, 27, 32, 51, 57, 66, 85

Sorted List: A: 13, 23, 27, 32, 51, 57, 66, 85


9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 23
Data Structures- Chapter 4
❑ Algorithm 4.4: (Bubble Sort) BUBBLE (DATA, N)
Here DATA is an Array with N elements. This algorithm sorts the elements
in DATA.

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


2. Set PTR: =1
3. Repeat while PTR<=N-K
(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 Step 1 Outer loop]
4. Exit
9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 24
Data Structures- Chapter 4
❑ Complexity of the bubble sort algorithm

– The time for a sorting algorithm is measured in terms of the number of


comparisons. The number f(n) of comparisons in the bubble sort is
easily computed. Specifically, there are n-1 comparisons during first
pass, which places the largest element in the last position, there are n-2
comparisons in the second pass, which places the second largest
element in the next to last position, and so on. Thus,

f(n) = (n-1) + (n-2) +. . . + 2 + 1 = n(n-1)/2=n2/2+O(n) = O(n2)

– In other words, the time required to execute bubble sort algorithm is


proportional to n2, where n is the number of input items.

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 25


Data Structures- Chapter 4
❑ Solved Problem 4.9: Using the bubble sort algorithm, find the number C
of comparisons and the number D of interchanges which alphabetize the
n=6 letters in PEOPLE.

Pass 1: P E O P L E, E P O P L E, EOPPLE
E O P P L E, E O P L P E, EOPLEP

Pass 2: E O P L E P, E O P L E P, EOPLEP
E O L P E P, EOLEPP

Pass 3: E O L E P P, E O L E P P, ELOEPP
ELEOPP
Pass 4: E L E O P P, E L E O P P, EELOPP
Pass 5: E E L O P P, EELOPP

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 26


Data Structures- Chapter 4
❑ Solved Problem 4.9: Using the bubble sort algorithm, find the number C
of comparisons and the number D of interchanges which alphabetize the
n=6 letters in PEOPLE.

Since n=6, the number of comparisons, C = 5+4+3+2+1=15 and


the number of interchanges, D = 9.

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 27


Data Structures- Chapter 4
❑ Binary Search
Example 4.9: Suppose the following sorted 13 elements are stored in an
array
A: 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
Now apply the binary search to array A for i) ITEM = 40 and ii) ITEM= 85.

Solution: i) For ITEM = 40


1. BEG = 1 and END =13
MID = INT [(1+13)/2] = 7. So, A [MID] = 55 ≠ ITEM
2. Since 40<55, BEG=1 and END = MID-1 = 7-1 = 6
MID = INT [(1+6)/2] = 3. So, A[MID] = 30 ≠ ITEM
3. Since 40>30, BEG = MID+1 = 3+1= 4 and END = 6
MID= INT [(4+6)/2] = 5. So, A[MID] = 40 = ITEM
We have found ITEM in location LOC = MID = 5
9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 28
Data Structures- Chapter 4
❑ Binary Search
Example 4.9: Suppose the following sorted 13 elements are stored in an
array
A: 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
Now apply the binary search to array A for i) ITEM = 40 and ii) ITEM= 85.

Solution: ii) For ITEM = 85


1. BEG = 1 and END =13
MID = INT [(1+13)/2] = 7. So, A [MID] = 55 ≠ ITEM
2. Since 85>55, BEG=MID+1=7+1=8 and END = 13
MID = INT [(8+13)/2] = 10. So, A[MID] = 77 ≠ ITEM
3. Since 85>77, BEG = MID+1 = 10+1= 11 and END = 13
MID= INT [(11+13)/2] = 12. So, A[MID] = 88 ≠ ITEM

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 29


Data Structures- Chapter 4
❑ Binary Search
Example 4.9: Suppose the following sorted 13 elements are stored in an
array
A: 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
Now apply the binary search to array A for i) ITEM = 40 and ii) ITEM= 85.

Solution: ii) For ITEM = 85


4. Since 85<88, BEG = 11 and END = MID-1 =12-1=11
MID= INT [(11+11)/2] = 11. So, A[MID] = 80 ≠ ITEM
5. Since 85>80, BEG = MID + 1 =11 + 1 =12 and END = 11
Since BEG > END, ITEM does not belong to A.

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 30


Data Structures- Chapter 4
❑ Binary Search

Algorithm 4.6: (Binary Search) BINARY (DATA, LB, UB, ITEM, LOC)

Here DATA is a sorted array with lower bound LB and upper bound UB,
and ITEM is a given item of information. The variables BEG, END and
MID denote respectively the beginning, end and middle locations of a
segment of elements of DATA.

This algorithm finds the location LOC of ITEM in DATA or sets


LOC=NULL.

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 31


Data Structures- Chapter 4
❑ Binary Search
1. Set BEG=LB, END=UB and MID=INT ((BEG+END)/2).
2. Repeat Steps 3 and 4 while BEG ≤ END and DATA[MID] ≠ ITEM
3. If ITEM < DATA[MID], then
Set END= MID - 1
Else
Set BEG= MID + 1
[End of If structure]
4. Set MID= INT((BEG+END)/2)
[End of Step 2 loop]
5. If ITEM = DATA[MID], then
Set LOC= MID
Else
Set LOC= NULL
[End of If structure]
6. Exit.
9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 32
Data Structures- Chapter 4
❑ Complexity of the Binary Search Algorithm
– The Complexity of the Binary Search algorithm is measured by the
maximum (worst case) number of Comparisons it performs for
searching operations. The searched array is divided by 2 for each
comparison/iteration. Therefore, we require at most f(n)
comparisons to locate ITEM:
2f(n) > n or equivalently, f(n) = log 2 n  + 1
– Approximately, equal to log2n.

Example:
– If a given sorted array contains 1024 elements, then the maximum
number of comparisons required is:
log2(1024) = 10 (only 10 comparisons are enough)

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 33


Data Structures- Chapter 4
❑ Limitations of the Binary Search Algorithm

The algorithm requires two conditions:


– The list must be sorted.
– One must have direct access to the middle element in any sub list.

9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 34


Data Structures- Chapter 4
❑ Solved Problem 4.11: Suppose multidimensional arrays A and B are declared
using
A(-2:2, 2:22) and B(1:8, -5:5, -10;5)
(a) Find the length of each dimension and the number of elements in A and B.
(b) Consider the element B[3, 3, 3] in B . Find the effective indices E1, E2, E3
and the address of the element, assuming Base(B)=400 and there are w=4
words per memory location.
Solution:
(a) Length = UB – LB +1
Hence, the lengths of the dimensions of A are:
L1 = 2-(-2)+1=5 and L2 = 22 -2+1=21
Accordingly, A has 5.21=105 elements.

The lengths of the dimensions of B are:


L1 = 8-1+1=8, L2 = 5-(-5)+1=11, and L3 =5-(-10)+1=16
Therefore, B has 8.11.16 =1408 elements.
9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 35
Data Structures- Chapter 4
❑ Solved Problem 4.11: Suppose multidimensional arrays A and B are declared
using
A(-2:2, 2:22) and B(1:8, -5:5, -10;5)
(b) Consider the element B[3, 3, 3] in B . Find the effective indices E1, E2, E3
and the address of the element, assuming Base(B)=400 and there are w=4
words per memory location.
Solution:
(b) The effective index Ei is obtained from Ei=ki – LB, where ki is the given
index and LB is the lower bound . Hence,
E1=3-1=2 E2=3-(-5)=8 E3=3-(-10)=13
The address depends on whether the programming language stores B in row-
major order or column-major order. Assuming B is the stored in column–major
order, we get:
E3 L2 =13.11=143 E3 L2 + E2 =143+8=151
(E3 L2 + E2)L1 =151.8=1208 (E3 L2 + E2)L1 +E1 =1208+2=1210
Therefore, LOC(B[3,3,3])=400+4(1210)=400+4840=5240
9/9/2024 Md. Golam Moazzam, Dept. of CSE, JU 36

You might also like