Data Structures And Algorithms
LECTURE 02
Course Outline:
• Arrays and Records:
- Arrays
- Traversing a Linear Array - Inserting into Linear Array - Deleting from Linear Array
• Application of Linear Array:
- Bubble Sort - Linear Search - Binary Search
- Two Dimensional Arrays - String Handling Methods
Appah Bremang 2
Fundamentals of Data Structures & Algorithms
ARRAYS
• Storage of data into memory can be done linearly or non-linearly.
• Linear – data will be organized in a sequential or linear order.
• Non-linear – data is organized in a non-linear order.
• The categorization of linear methods are:
- Arrays
- Linked list
• In arrays the data will be stored in continuous memory location whereas,
- in linked list the data will be stored in different address (memory) location.
• The categorization of non-linear methods are:
- Graphs - Trees
Appah Bremang 4
Linear Array
• It is an array.
• Linear array can be defined using a single letter or many letter shown below:
Example: A or AB
Index represents: 1,……, n
Appah Bremang
Operations on an Array
• Retrieval of an element: Given an array index, retrieve the corresponding value.
- If the array is sorted, this enables one to compute the minimum, maximum, median (or in general, the ith smallest element) essentially for free in O(1) time.
• Search: Given an element value, determine whether it is present in the array.
Appah Bremang 6
Length or Size of Array
• The length or the size ( N) of any array can be found using:
N = Ub – Lb + 1 where Lb – Lower index Ub – Higher index
Example 1:
a[10]
n = 10 – 1 + 1 = 9 + 1
= 10
• Example 2:
If LB = 55, UB = 97, then
n = (97-55)+1=43
• Array can be represented by:
i) A(1), A(2), ….. , A(n) or ii) A[1], A[2], ……, A[n] or iii) A
1, A
2,….., A
nAppah Bremang 8
Declaration of Linear Array in various Languages
• The following are the various declarations of linear array in four languages (Pascal, Fortran and C/C++):
• FORTRAN Syntax:
Data type array name (size) Example: REAL A(10)
• PASCAL Syntax:
Array Name: ARRAY [start index….end index] of data type;
Example:
A:ARRAY [1 ………10] of real;
Appah Bremang 9• C/C++ Language:
Syntax:
Data type array name [size];
Example: int a[10];
Appah Bremang 10
Representation of Linear Array in Memory
• Memory consists of collection of cells.
• Each cell is represented address location.
In an array a value is stored at ‘α’ this is called base address which is the ‘’address of lower bound’’. The next value would be stored at α+1, α+2……… etc.
Appah Bremang
0 1 2 3 4 . . N Addresses
storage cell
• Using the base address it is easy to find out value of the address holding a data item.
Loc (A[λ]) = Base address (A) + w * (λ - Lb)
where
A = Array name
A[λ] = λth element of Array A
Base Address (A) = address of the first element of the array,
w = number of words in a memory cell
Lb = lower bound (start index)
Appah Bremang
12
Calculation of the address in an interval address
space in Memory
Example 1:
Base Address (A) = 1000
w = one (or word) = 1; LB = 1
Then,
Loc ((A[20]) = base address (A) + (1*(20-1)) = 1000 + (1*(20-1))
= 1019
Example 2:
int A[10] = {5, 6, 12, 4, 15, 45, 87, 1, 9, 13}
Loc((A[5]) = 100 + 4 * (5 – 0) = 120
Appah Bremang 14
Traversing a Linear Array
• The method of visiting each element in an array at exactly once is called Traversing.
• Traversing is used to perform one of the following operations:
- To read an element
- To print an element
- To process an element
• The following steps are used to traverse a linear array.
Steps:
1. Set index λ to Lb (lower bound) 2. Visit the value of λ
3. Increment the value of λ then compare this with Ub (upper bound)
4. If λ < Ub or λ = Ub then go to step 2.
5. Exit operation
Appah Bremang 16
Procedure
Traverse (A, Lb, Ub) {
for λ = Lb to Ub visit (A[λ]);
}
Example:
Write a procedure to find sum of n-numbers using array.
Sum (A, Lb, Ub) {
sum = 0;
For λ = Lb to Ub do sum = sum + A[λ];
Print ‘’sum=‘’, sum;
}
Appah Bremang 18
Insertion in Linear Array
• Example:
Insert the new element 17 to the next position of A[0]
Before Insertion After Insertion
15 7
8 9
18
15 17 7 8 9 18
Appah Bremang
150 151 152 153 154
A[0]
A[1]
A[2]
A[3]
A[4]
150 151 152 153 154 155
A[4]
A[3]
A[2]
A[1]
A[0]
A[5]
LB = 0; UB = 4
LB = 0; UB = 5
Figure: Insertion Operation
19
Insertion in Linear Array Algorithm
Insertion of a new element into a linear array can be done at the end or at the middle of the array (or any place within the linear array).
The following are the steps to be traversed during insertion operation.
Steps to insert a new element at ‘k’ in an array ‘A’
(i) Compare ‘’k’’ with Ub
where k = the place at which element is going to be inserted Ub = upper bound
(ii) If k = Ub+1, insert the new element at A[k], i.e. A[k] = new element (iii) If k <= Ub then
a) shift A[Ub], A[Ub-1],…., A[k] to A[Ub+1], A[Ub]…… A[k+1]
b) Insert the new element at A[k]; A[k] = new element (iv) Increment the values of Ub and n.
i.e. Ub = Ub+ 1 and n = n+1.
Appah Bremang
20
• Procedure
Insert (A, Lb, Ub, pos, item) //A-array Name
{
for j = Ub to pos do A[j+1] = A[j];
A[pos]= item; //new item inserted
Ub = Ub+1; //upper bound modified
}
Deletion from Linear Array
Example: Delete element 9 next of position A[2] from the linear array.
Before Deletion After Deletion
15 7
8 9
15 7 8
Appah Bremang
A[0]
A[1]
A[2]
A[3]
A[0]
A[1]
A[2]
150 151 152 153
150 151 152
LB = 0; UB = 3
LB = 0; UB = 2
22
Deletion from Linear Array
This method is used to delete an element from linear array.
The following steps are traversed to delete an item at the kth place.
Steps to delete an element from kth place on an array A are:
(i) Copy the element A[k] into a variable called item.
item = A[k]
(ii) Shift the values A[k+1], A[k+2], … , A[Ub] to A[k], A[k+1], … , A[Ub-1]
(iii) Decrement the values of Ub and n.
Ub = Ub -1 and n = n -1
Deletion from Linear Array
Appah Bremang 24
Procedure
delete (A, pos, Ub, item) {
item = A[pos];
//delete itemfor i = pos to Ub-1 do
A[i] = A[i+1];
//shift the dataUb = Ub-1;
}
Sorting – Bubble Sort Algorithm
• Consider an array A, which has some elements. To display the elements in ascending order scan the array ‘A’ for n-1 times.
• Scan-1
i) Compare the first element with the second element of the array. If the first element is smaller than second element then no need of interchange process.
Otherwise swap the elements.
ii) Compare the second element with the third one. If second element is smaller than the third element then need not interchange. Otherwise interchange the elements.
iii) Do this process until the companion of (n-1)th element with nth element.
iv) At the end of the first scan, the biggest element will be moved to the nth position of an array A.
25
Scan-2
i) Compare the first element with second element. If it is smaller need not swap it. Otherwise swap the elements.
ii) Then compare the second element with third element. If small, need not interchange. Otherwise elements have to be swapped.
iii) Do this process until (n-2)th element is compared with (n-1)th element.
iv) At the end of second scan, the second biggest element will be moved to (n-1)th place.
Scan (n-1)
Repeat the above steps until (n-1)th scan
Appah Bremang
26
Example:
• Consider the following array:
a[1]=43, a[2]=72, a[3]=10, a[4]=23, a[5]=1
Sort the values in ascending order.
Refer to the next slide for the solution.
Bubble Sort
Appah Bremang 28
Let i – is a pointer which counts the number of scan.
j – is a pointer which is used to compare the first element and second element.
Refer to ‘’previous slide’’ for the solution of the example.
Procedure (a, n)
//a……array, n….maximum numbers {for i = 1 to n-1 do
for j = 1 to n-i do //number of scan if (a[j] > a[j+1]) then
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
} }
Appah Bremang 29
Linear Search
Consider an array ‘A’ with ‘n’ elements. An element X is to be search from the array A. If the element is found then the search succeeds else the search fails.
The following are the steps to be tracked.
Steps involved in the Algorithm:
i) Compare X with A[1]. If X = A[1] then print as ‘’Search Success’’. Otherwise compare X with A[2]. If X = A[2] print this as ‘’Search Success’’. Do this process until X is found in A[n].
ii) If no element is equal to X, then print this as ‘’Search failed’’
Appah Bremang 30
• Example: Consider an array with the following elements (10).
The query is to search the element 25 from the given array.
10
7 13
9 25 17 15 6 19
5 A[1]
A[2]
A[3]
A[4]
A[5]
A[6]
A[7]
A[8]
A[9]
A[10]
Pass 1: 10 7 13 9 25 17 15 6 19 5 25 not matched
Pass 2: 10 7 13 9 25 17 15 6 19 5 25 not matched
Pass 3: 10 7 13 9 25 17 15 6 19 5 25 not matched
Pass 4: 10 7 13 9 25 17 15 6 19 5 25 not matched
Pass 5: 10 7 13 9 25 17 15 6 19 5 25 matched
Appah Bremang 32
Procedure
Search (A, n, X) //X is the search data {
for i = 1 to n do if (X = A[i]) then {
Print ‘’Search Success’’;
Return;
}
Print ‘’Search Failed’’;
}
Appah Bremang
Binary Search
Consider a sorted array ‘A’ with ‘n’ elements. Assign the values lower bound as Lb and upper bound as Ub.
Example:
Query to search the element 60 from the following array:
A: [7] [8] [9] [10] [11] [12] [13] [14] [15] [16]
Stored Data 5 13 18 25 33 48 54 60 80 85
Appah Bremang 34
• The query is to search the element 60 from the given array
A: [7] [8] [9] [10] [11] [12] [13] [14] [15] [16]
Stored data: 5 13 18 25 33 48 54 60 80 85 Search item: 60
Here Lb = 7 and Ub = 16
Pass 1
Compute mid = [(Lb + Ub)/2] = [(7 + 16)/2] = 11
A: [7] [8] [9] [10] [11] [12] [13] [14] [15] [16]
Data: 5 13 18 25 33 48 54 60 80 85
60 – search item is greater than the mid value Action – change the lower bound = 12
Pass 2
Compute mid = [((12+16)/2)] = 14
A: [7] [8] [9] [10] [11] [12] [13] [14] [15] [16]
Data: 5 13 18 25 33 48 54 60 80 85
60 success
Appah Bremang
35
Binary Search Algorithm
Consider a sorted array ‘A’ with ‘n’ elements. Assign the values lower bound as Lb and upper bound as Ub.
Using the following steps (or algorithm) it is possible to find out the element ‘X’ from A.
Steps (or algorithm):
i) Calculate mid value as mid = (Lb + Ub)/2.
ii) Compare X with A[mid]. If X = A[mid] then X is found.
iii) If X < A[mid] repeat step(i) and step(ii) from 1 to mid-1.
iv) If X > A[mid] repeat step(i) and step(ii) from mid+1 to n.
v) If no value is equal to X then return (0).
Appah Bremang 36
• Procedure
Binary Search (a, n, X, Ub, Lb) {
while (Lb <= Ub) do {
mid = (Lb + Ub)/2 if (X = A[mid]) then return (mid);
else
if (X < A[mid]) then Ub = mid-1;
else
Lb = mid+1 }
Return (0) }
37