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
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
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
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.
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) A1 , A2 ,….., An
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:
C/C++ Language:
Syntax:
Data type array name [size];
Example: int a[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.
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)
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
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
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;
}
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 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
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.
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 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
Procedure
delete (A, pos, Ub, item) {
item = A[pos]; //delete item
for i = pos to Ub-1 do
A[i] = A[i+1]; //shift the data
Ub = 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.
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
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
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’’
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
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’’;
}
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
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).
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)
Two-Dimensional Arrays
The method of representing data by row-wise and column-wise is called 2-Dimensional array.
Syntax:
data type name[row size][column size];
Range:
- The size of the row is called a row range.
- The size of column is called a column range.
Multiplication of row range and column range is equal to the maximum elements of 2-D array.
The maximum elements of 2-D array = row range * column range.
38
Consider the following array: int a[3][2].
This array can hold a maximum of 6 elements in the memory.
Each location can be accessed using indexing or subscription.
column1 column2 Row1 a[1][1] a[1][2]
Row2 a[2][1] a[2][2]
Row3 a[3][1] a[3][2]
Representation of 2-Dimensional Array
An example of 2D array can be arr[2][3] containing 2 rows and 3 columns.
The arr[0][1] is an element placed at 0th row and 1st column in the array.
A 2-dimensional array can thus be represented as given below:
Fig: Representation of 2D Array in Memory
Memory Representation of 2-D Array
Two methods are used to represent the 2-dimensional arrays into memory.
i)
Row major representation.
ii)
Column major representation.
In the row major method, the first row of the array ‘a’ will be stored in the first location. The second row of the array ‘a’ will be stored in the second location. So elements will be stored in row-wise.
In the case of column major method, the elements will be stored in column-wise. First column elements will be stored first and then the second column element.
a[1][1]
a[1][2]
a[2][1]
a[2][2]
a[3][1]
a[3][2]
a[1][1]
a[2][1]
a[3][1]
a[1][2]
a[2][2]
a[3][2]
Row Major Column Major
Position 1
Position 2
Position 3
Position 1
Position 2
Memory Representation of Two-Dimensional Array
Implementation of 2-Dimensional Array
In ´´C´´ language, the 2-Dimensional array can be represented as:
int a[rs][cs]
where a – name of the array rs – Row size
cs – Column size
After the declaration of this statement the compiler will assign, rs*cs continuous memory location in the memory and will assign a name ´a´ for this array.
Then the data will be stored in these address locations by row major method.
Base Address
The address of a[0][0] is called the base address. Base (a) = a[0][0]. The dsize is the size of data in the memory. To find out the address a[0][1] of an element the following formula is used:
a[0][1] = base(a) + 1*dsize.
To find out the address a[0][2] of an element:
a[0][2] = base(a) + 2*dsize.
To find out address of the element which is stored at a[i][j]
the following method is used:
a[i][j] = base(a) + (i*cs*j)*dsize
Appah Bremang 44
Calculation of the Address of an element in the 2-D Array:
Example:
int A[3][4] = {10, 13, 24, 3, 41, 5, 6, 17, 8, 91, 10, 16}
Each element of A is 4bytes long
This 2-D array will be presented externally and in the memory of the computer using ROW- MAJOR-ORDER
ROW-MAJOR-ORDER
Store as ROW-MAJOR-ORDER
int A[3][4] = {10, 13, 24, 3, 41, 5, 6, 17, 8, 91, 10, 16}
Here row = 3 and column = 4 or M = 3 and N = 4
Stored as COLUMN-MAJOR-ORDER
Similarly, for the Column-Major-Order representation, let us consider the matrix.
int A[3][4] = {10, 13, 24, 3, 41, 5, 6, 17, 8, 91, 10, 16}
Here row = 3 and column = 4 or M = 3 and N = 4
48
Low Addresses High Addresses
SUMMARY OF ROW AND COLUMN
MAJOR
Representation of tables using Arrays:
Consider the following table player list:
Hockey Basketball Football Leo
Lydia Paul
Stephen Richard David
Samuel Binny David Peter Helena Lawal
This list is stored in the memory using different methods
Method 1, Using 2D Array Leo
Lydia Paul
Stephen Richard David
Samuel Binny David Peter Helena
Lawal Hockey
Basketball
Football
The player list is stored in an array of
M * N or N * M size.
Where m – represents group (item) names and n – represents maximum members of the team
Draw Back:
- Memory is wasted
- To store this player list in the memory, we need 18 memory locations.
- Out of the 18 memory locations only 12 locations are used by this array.
- The remaining 6- places unused.
52
Method 2 – Using 1-Dimensional Array
Leo Lydia
Paul Stephen Richard David Samuel
Binny David Peter Helena
Lawal Hockey
Basketball
Football
Advantage:
The memory is not wasted.
Draw Back:
- Retrieving a player from a particular group is not possible in this method, because there are no separation between two groups.
Here the elements of player list are stored in a continuous memory locations using 1 – Dimensional array.
First group of the play list is stored in the starting location of memory.
The second group name is stored in second location of the memory.
Third group name is stored in third location.
Method 3 – Using 1-Dimensional Array
Here a player list is stored in a continuous memory location, but each team is separated by a ($) dollar sign.
By separating each group by a dollar symbol it is very easy to read a player name from any group
Reading starts from the starting location of the array.
Leo Lydia
Paul
$ Stephen Richard David
$ Samuel
Binny David Peter Helena
Lawal
54 $
String Handling Methods
Strings are second category of data.
The char array starts with 0 initialization. An example declaration is shown below:
Example: Char str[15]
The above declaration tells the compiler that an array str is initialized with fifteen spaces.
If the string ‘’Hello , World!’’ is stored in the array the representation is as follows:
Characters: H e l l o , w o r l d !
Subscripts: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 H e l l o , w o r l d !
There are different variations of string functions.
They are:
Strlen()
Strcpy()
Strncpy()
Strcat()
Strncat()
Strcmp()
Strncmp()
strlen()
As the name indicates this function calculates the length of the string. The following gives the syntax declaration of
strlen ().
Syntax: len = strlen(ptr):
where len is an integer and ptr is a pointer to char.
This function returns the length of the input string excluding the NULL.
Example:
int len;
char str[15];
strcpy(str, ‘’Hello, world’’);
len = strlen(str);
Strcpy()
This is a library function which copies one string to another string
Syntax: strcpy(ptr1, ptr2); where ptr1 & ptr2 are pointers to char.
E.g. char S[25];
char D[25];
strcpy(S,’’This is string1.’’); //Putting text into a string.
strcpy(D,S); //copying a whole string from S to D strcpy(D,S[8]); //copying the tail end of string S to D
58
Strcat()
This library function concatenates or combine two strings.
The syntax is given below:
Syntax:
strcat(ptr1, ptr2); where ptr1 and ptr2 are pointers to char
Example:
char S[25] = ‘’world!’’;
char D[25]=‘’Hello,’’;
strcat(D, S); // concatenating the whole string S onto D. // gives Helloworld.
Strncat()
syntax:
strncat(ptr1, ptr2, n); where ptr1 and ptr2 are pointers to char
and n is an integer.
char S[25] = ‘’world!’’;
char D[25] = ‘’Hello,’’;
concatenating five characters from the beginning of S onto the end of D and placing a null at the end.
Strncat(D, S, 5);
Strncat(D, S, strlen(S)-1);
Both would result in D containing ‘’Hello,world’’.
60
Strcmp()
This library function compares two strings for equality.
syntax:
diff = strcmp(ptr1,ptr2);
where diff is an integer and ptr1 and ptr2 are pointers to char e.g.
char s1[25]=‘’pat’’;
char s2[25]=‘’pet’’;
diff = strcmp(s1,s2); //diff will have a negative value if s1<s2
diff = strcmp(s2,s1); //diff will have a positive value if s1>s2
diff = strcmp(s1,s1); //diff will have a value of zero(0) if s1=s2
Strncmp
syntax: diff = strncmp(ptr1, ptr2, n);
where diff and n are integers, and ptr1 and ptr2 are pointers to char.
Strncmp() is used to compare the first n characters of two strings. The strings are compared character by character starting at the characters pointed at by the two pointers.
if the first n strings are identical, the integer value zero (0) is returned.
Example ‘C’ programs for string handling methods
#include<stdio.h>
#include<string.h>
void main() {
char s[20] = ´´kwame´´;
char t[10] = ´´Ansah´´;
clrscr();
strcat (s, t);
printf(´´the new string is %s´´,s) getch()
}
Output: the new string is kwameAnsah
#include<stdio.h>
#include<string.h>
void main() {
char s[20] = ´´kwame´´;
char t[10] = ´´Ansah´´;
clrscr();
strncat (s, t,3);
printf(´´the new string is %s´´,s);
getch() }
Output: the new string is kwameAns
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char alpha[ ] = ´´kwame´´;
clrscr();
printf(´The string %s contains %d character´´,alpha, strlen(alpha));
getch();
}
Output: The string kwame contains 5 characters