0% found this document useful (0 votes)
96 views37 pages

Data Structures & Algorithms: Lecture # 2 Dated: 28-10-2010

The document provides information about data structures and algorithms. It discusses an algorithm to find the largest element in an array. The algorithm initializes variables, increments a counter, tests if the counter exceeds the array size, compares elements and updates the largest value found, and repeats the loop. A flow chart depicts the steps of the algorithm. The document also discusses arrays, including one-dimensional and two-dimensional arrays. Common operations on arrays like traversing, searching, inserting, deleting, sorting, merging, and reversing are described.

Uploaded by

Vicky Butt
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views37 pages

Data Structures & Algorithms: Lecture # 2 Dated: 28-10-2010

The document provides information about data structures and algorithms. It discusses an algorithm to find the largest element in an array. The algorithm initializes variables, increments a counter, tests if the counter exceeds the array size, compares elements and updates the largest value found, and repeats the loop. A flow chart depicts the steps of the algorithm. The document also discusses arrays, including one-dimensional and two-dimensional arrays. Common operations on arrays like traversing, searching, inserting, deleting, sorting, merging, and reversing are described.

Uploaded by

Vicky Butt
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Data Structures & Algorithms

Lecture # 2

Dated: 28-10-2010
Example 2. formal Algorithm
representation

 An array DATA of numerical values


is in memory. We want to find the
location LOC and the value MAX of
the largest element of data. Given no
other information about DATA.
(Largest Element in Array) A nonempty array DATA with N
numerical values is given. This algorithm finds the location
LOC and the value MAX of the largest element of DATA. The
variable K is used as a counter.

 Step 1. [Initialize] Set K:= 1, LOC := 1 and


MAX:= DATA[K].

 Step 2. [increment counter]. Set K:= K + 1.

 Step 3. [Test counter] if K > N , than


write: LOC, MAX, and exit.

 Step 4. [compare and update] if MAX < DATA[K],


than,
Set LOC:=K and MAX:= DATA[K].

 Step 5. [Repeat loop]. Go to step 2.


 Exit
Flow Chart for the algorithm.
start

K 1
LOC 1
MAX DATA[1]
.
K K+1

Yes
Is K > N ? write: LOC, MAX

No
STOP
No
Is MAX< DATA[K]

Yes

LOC K
MAX DATA[K]
Today Topics
 What are Arrays?

 Arrays Data Structures


 One Dimensional Array
 Two Dimensional Array
 Operations on Arrays
 Inserting
 Deleting
 Traversing
 Searching
 Reversing
What are Arrays?
‘A group of data items of the same type under one name’ or
‘array is a collection of elements or items or components
that have the same data type’. ‘or’ an array is a finite
number n of homogeneous data elements such that:
a. the elements of the array are referenced
respectively by and index set consisting
of n consecutive numbers.
b. the elements of the array are stored
respectively in successive memory
locations.
What are Arrays?
For example array of 10 integers, arrays of
names, etc. It can also be defined as ‘a
list of two or more variables with the
same name’.
 Elements of an array may be denoted by
the:
 Subscript notation
 Parenthesis's notation
 Bracket notation
Operation on array. Discussed later.
Why we use Array?
 For example you want to find smallest numbers
amongst four integer items and suppose that you do
not know how to use array or you do not desire to use
it. You may tackle the problem in the following
manner:

 Define four variables to hold four integers data items,


i.e.,
int a=10, b=20, c=30; d=5;

 After declaring these variables, you would be required


to write a quite few statements to find the smallest
number in them. Since there are four variables,
therefore, you would need to use four compound if
statements for the solution, such as:
Why we use Array?
 if (a<b) && (a<c) && (a<d) cout << “a is smallest number
in the data”;

 if (b<a) && (b<c) && (b<d) cout << “b is smallest number


in the data”;

 if (c<b) && (c<a) && (c<d) cout << “c is smallest number


in the data”;

 if (d<a) && (d<b) && (d<c) cout << “d is smallest number


in the data”;

 Based on the current data as mentioned above, computer


will produce that “d is the smallest number in the data”.
Why we use Array?
 Assuming if you would have to find smallest number
amongst 100 data items, off course you would be
required to write 100 compound if statements and each
compound condition made of 99 simple conditions.

 Further, it would not be possible to do other application


such as sorting data, finding any particular number in
large amount of data with the use simple data
structures. Therefore, the use of arrays in programming
languages is unavoidable.

 Processing on the data such as finding any number,


calculating mean, average and sorting data are some of
the fascinating applications which can be implemented
very easily with the help of array.
One Dimensional Array
 One dimensional array is most common form of the array used by
programmers to solve different kinds of problems.

 The general format of the one-dimensional array is as follows:


data_type array_name[size];
where
data_type: is any valid C++ data type (int, float, etc.)

array_name: is user defined identifier. User_defined


name is followed by the square

brackets which makes difference between ordinary variable


and array type variable.

Size: shows the actual length of the array, i.e., number of


locations or no data items.
Examples of One Dimensional Array
 int a[10]; ‘a’ is an integer array of
10 elements

 float f[10] ‘f’ is a float array of 10


elements

 double d[10] ‘d’ is a double array


of 10 elements.
Examples of One Dimensional Array
 Suppose you want to store identity-numbers of 20
students, the following statement would be used to
declare a one dimensional integer array to hold data;
int student_id_no[20];

where int is type of the array, and


‘student_id_no’ is name of the array (user defined).

Computer will allocate 20 locations for


‘student_id_no’.

C++ starts array locations from zero, hence, first


location is indicated as zero
Diagrammatically Representation of
Array

Memory
Address 101 103 105 107 109 111 113 115 117 119

0 1 2 3 4 5 6 7 8 9
LB UB

Index
Number
Memory representation of One
Dimensional Array
 Maximum number of elements in a One-Dimensional
Array is computed as under:
Maximum number of elements = UB – LB + 1
UB – Upper Bound LB – Lower Bound
In pervious diagram UB = 9 and LB = 0
So, Maximum number of elements = 9 – 0 +1 = 10

 Each element of the array also has a unique memory


address

 The starting address of array is called Base Address.

 Elements of an array occupy a continuous block of


memory
Memory representation of One
Dimensional Array
 The memory address of an element with index k is
computed as:
L(X(k)) = L0 + C * (k – lower bound)

L0 is a Base Address, C memory size of each element in byte

 Suppose that Base address is 101 and size of element is 2


bytes, what will be the memory address for index 5
(Previous diagram)

L0 = 101, C = 2, k = 5
L(X(k)) = L0 + C * (k – 1)
L(X(5)) = 101 + 2 * (5 – 0)
= 101 + 2 * 4 = 101 + 10 = 111
Operations on 1-D Array
 Traversal: Processing each element in the array

 Search: Finding the location of an element with a given


value

 Insertion: Adding a new element to an array

 Deletion: Removing an element from an array

 Sorting: Organizing the elements in some order

 Merging: Combining two arrays into a single array

 Reversing: Reversing the elements of an array


Algorithms of Operations on 1-D
Array
Traverse_Array ( LA, UB, LB)
Algorithm for print the each element of
array
1. Start
2. Repeat step-3 For i=lB TO UB
3. Print arr[i]
4. End
inserting element into the array
Insert_Item (LA, N, K, ITEM)
LA is a 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.

1. [initialize counter]. Set J:=N.


2. Repeat step3 and 4 while j >= K
3. [move jth element downward (to nth position).] Set LA[j+1]:= LA[j]
4. [decrease counter] Set j:=j-1.
[end of step 2 loop]

5. [insert element] Set LA[k]:=ITEM


6. [Reset N] Set N:=N+1
7. exit
Deleting element from the array
Delete_Item (LA, N, K, ITEM)
LA is a linear array with N elements and K is a positive integer
such that K<=N. this algorithm delete an element ITEM from the
Kth position in LA.

1. [initialization]. Set ITEM := LA[K].


2. Repeat for J =K to N-1;
3. [move J + 1st element upward.] Set LA[J]:= LA[J + 1]
[end of step 2 loop]

4. [Reset the number N of elements in LA] Set N := N +1


5. exit
searching element in an array
Search_Item(num)
Algorithm for search Pos of the given element in the
array
1. Start
2. Repeat step 3 to 4 For i=1 to Max by 1
3. If arr[i] = num then
Print I
Return
End If
4. If i = Max then
Print “Element Not Found!”
End If
5. End
searching element in an array
Search_Item (DATA, N, ITEM, K)
DATA= linear array, N= total no of element, ITEM= element to be
searched

1. [Insert ITEM at the end of DATA] Set DATA[N+1]:=ITEM


2. [initialize counter] Set K:=1
3. [Search for ITEM] Repeat while DATA[K]!= ITEM
Set K:=K+1.
[End of loop]
4. [successful ?]
5. If K:=N+1. then
Unsuccessful
6. exit
Reversing an array element
Reverse_Array
Algorithm for reverse the entire array
1. Start
2. Repeat step 3 to 5 For i=1 to Max/2 by 1
3. temp = arr[i]
4. arr[i] = arr[Max-i+1]
5. arr[Max-i+1] = temp
6. End
Two Dimensional Array
 A two-dimensional array is a collection of elements placed
in m rows and n columns.

 The syntax used to declare a 2-D array includes two


subscripts, of which one specifies the number of rows and
the other specifies the number of columns of an array.
data_type array_name[rows][columns];
where
data_type: is any valid C++ data type (int, float, etc.)

array_name: is user defined identifier. User defined


name is followed by the square

Rows: Specifies the number of rows of an array


Column: Specifies the number of columns of an array
Examples of Two Dimensional Array
 int a[3][4]; ‘a’ is an integer array
containing 3 rows and 4 columns

 float f[3][4]; ‘f’ is a float array


containing 3 rows and 4 columns

 double d[3][4]; ‘d’ is a double array


containing 3 rows and 4 columns
Diagrammatically Representation of
Array
A[1,3] A[1,4]
A[1,1] A[1,2]
Columns

1 2 3 4
A[2,1]
1 12 1 -9 23 A[2,2]
Rows
2 14 7 11 121 A[2,3]

A[3,1]
A[3,4]
3 6 78 15 34

A[3,2]
Two Dimensional Array
 Maximum number of elements in a Two-Dimensional
Array is computed as under:
Maximum number of elements = M x N
M – Number of rows N – Number of columns
In pervious diagram M = 3 and N = 4
So, Maximum number of elements =3 x 4 = 12

 Each element of the array also has a unique memory


address

 The starting address of array is called Base Address.

 Elements of an array occupy a continuous block of


memory
Representation of Two-Dimensional
Arrays in memory
 Two Dimensional arrays are represented in memory in
two ways.
 Row-major order
1st Row 2nd Row 3rd Row

Values 12 1 -9 23 14 7 11 121 6 78 15 34
Memory 502 504 506 508 510 512 514 516 518 520 522 524
Address

 Column-major order
1st Col 2nd Col 3rd Col 4th Col

Values 12 14 6 1 7 78 -9 11 15 23 121 34
Memory 502 504 506 508 510 512 514 516 518 520 522 524
Address
Note: Each integer occupies two bytes.
Row-Major Order
 The memory address of an element with index i (row) and
j (Col) is computed as:
L(X[i][j]) = L0 + [(i – 1) * N + (j – 1)] * d

L0 is a Base Address, d memory size of each element in byte


i is a row, j is a col and N is number of columns of an array

 Suppose that Base address is 502 and size of element is 2


bytes, what will be the memory address for position x[2][2]
(Previous diagram)

L0 = 502, d = 2, i = 2, j = 2, N = 4
L(X[i][j]) = L0 + [(i – 1) * N + (j – 1)] * d
L(X[2][2]) = 502 + [(2 – 1) * 4 + (2 – 1)] * 2
= 502 + [4 + 1] * 2 = 502 + 5 * 2 = 502 + 10 = 512
Col-Major Order
 The memory address of an element with index i (row) and
j (Col) is computed as:
L(X[i][j]) = L0 + [(i – 1) + (j – 1) * M] * d

L0 is a Base Address, d memory size of each element in byte


i is a row, j is a col and M is number of rows of an array

 Suppose that Base address is 502 and size of element is 2


bytes, what will be the memory address for position x[2][2]
(Previous diagram)

L0 = 502, d = 2, i = 2, j = 2, M = 3
L(X[i][j]) = L0 + [(i – 1) + (j – 1) * M] * d
L(X[2][2]) = 502 + [(2 – 1) + (2 – 1) * 3] * 2
= 502 + [1 + 3] * 2 = 502 + 4 * 2 = 502 + 8 = 510
Operations on 2-D Array
 Traversal: Processing each element in the array

 Search: Finding the location of an element with a given


value

 Insertion: Adding a new element to an array

 Deletion: Removing an element from an array

 Sorting: Organizing the elements in some order

 Merging: Combining two arrays into a single array

 Reversing: Reversing the elements of an array


Algorithms of Operations on 2-D
Array
Traverse_Array
Algorithm for print the each element of
array
1. Start
2. Repeat step-3 For i=1 to rMax by 1
3. Repeat step-4 For j=1 to cMax by 1
4. Print arr[i][j]
5. End
Algorithms of Operations on 2-D
Array
Insert_Item_Row_Major(r, c, num)
Algorithm for inserting an element at given position of the
array
1. Start
2. Repeat step-3 For i=rMax to r by -1
3. Repeat step-4 For j=cMax to 1 by -1
4. if i <> r or j > c then
if j = 1 then
arr[i][j] = arr[i-1][cMax]
else
arr[i][j] = arr[i][j-1]
end if
end if
5. Arr[r][c] = num
6. End
Algorithms of Operations on 2-D
Array
Delete_Item_Row_Major(r, c)
Algorithm for delete an element from the given position of the
array
1. Start
2. Repeat step-3 For i=r to rMax by 1
3. Repeat step-4 For j=1 to cMax by 1
4. if i <> r or j >= c then
if j = cMax then
arr[i][j] = arr[i+1][1]
else
arr[i][j] = arr[i][j+1]
end if
end if
5. Arr[rMax][cMax] = 0
6. End
Algorithms of Operations on 1-D
Array
Search_Item(num)
Algorithm for search Pos of the given element in the
array
1. Start
2. Repeat step 3 For i=1 to rMax by 1
3. Repeat step 4 to 5 For j=1 to cMax by 1
4. If arr[i][j] = num then
Print i & “, “ & j
Return
End If
5. If i = rMax and j = cMax then
Print “Element Not Found!”
End If
6. End
Multi-Dimensional Array
 A three-dimensional array can be thought of as an array of
arrays of arrays.

 Following figure shows a 3-D array, which is a collection of


three 2-D arrays each containing 4 rows and 2 columns
int arr[3][4][2];

2 8 3 2 3 9
0 6 8 6 1 8
4 7 1 6 6 5
1 5 4 5 4 0

1st 2-D Array 2nd 2-D Array 3rd 2-D Array


Next Lecture
 What are Stacks?

 Representation of Stacks

 Operations on Stacks
 Push
 Pop

 Evaluation of Expressions

 Infix to Postfix Conversion

You might also like