Professional Documents
Culture Documents
AOP Unit 3 chapter 2
AOP Unit 3 chapter 2
AOP Unit 3 chapter 2
Array Techniques
Array Techniques
An array is an indexed collection of data elements of the same type.
Indexed means that the array elements are numbered (starting at 0).
All the elements will have the same name, but each element is accessed or identified using
the index or subscript or position value.
The index will always begin with 0, hence if there are ‘n’ elements in an array the index of
last element will be n – 1.
i.e. the first element with the last element, then the second element with the second last
element, and so on, till the middle element of the array.
In each step, the index on the left is increasing by one and the index on the
right is decreasing by 1.
The index from left to right is denoted by i and the index from right to left is denoted by
j.
temp = a[i]
a[i] = a[j]
a[j] = temp
Problem
Find out the frequency count of the number of students that obtained each
possible mark
What is Histogram?
A histogram is a graphical representation that classifies a set of data points into ranges
defined by the user.
The histogram, which resembles a bar graph in appearance, consolidates a data series.
A histogram is used to count or visualize the frequency of data (the number of occurrences)
over discrete intervals.
A frequency histogram is a histogram that shows the frequencies (the number of occurrences)
of the given data items.
Algorithms Development
Consider 101 variables count0, count1, count3, ……. count100 each corresponding to particular marks
from 0 – 100
But this approach requires 101 tests to be done, to update just one particular mark. The program becomes very
lengthy.
Let us understand the logic to find the frequency count of the number of students that
obtained each possible mark.
Create an array of 101 location, where each location has particular mark value.
The array index number ( 0 to 100) denotes the marks and its array elements are the
frequency count of each array element. The count of number of students are stored in
array locations.
For eg, if 25 students have scored 50, then 25 will be stored in that array location.
When a new count get added to a location, the count in that location gets updated.
a[50] = a[50] + 1
Let us assume that we want to find the frequency count of 10 students marks.
Suppose if first student marks is 50, then add 1 to the existing value in a[50] ( the existing
value in a[50] is 0 and adding 1 to the existing value becomes 0 + 1 = 1)
Suppose if second student marks is 75, then add 1 to the existing value in a[75] ( the
existing value in a[75] is 0 and adding 1 to the existing value becomes 0 + 1 = 1)
Suppose if third student marks is 50, then add 1 to the existing value in a[50] ( the existing
value in a[50] is 1 and adding 1 to the existing value becomes 1 + 1 = 2) and so on.
The maximum number is a number which is greater than or equal to all other numbers in
an array of n elements.
An array should have one or more elements to find the maximum number.
The solution is to initialize the first element as max, then traverse the given array from
second element till end.
For every traversed element, compare it with max, if it is greater than max, then update
max.
The approach devised is as follows:
• Every element in the array is examined and compared to each other.
• The first array element is set as temporary candidate for maximum, max
While examining second number (i.e, the second array element), three
situations are possible:
1. Second number less than max
2. Second number equal to max
3. Second number greater than max
If the second number is less than or equal to max, then examine and
compare the third number.
Otherwise if second number is greater than max, update the value of max
with value of second number.
Example
Consider an array with five elements a[5] = {11, 24, 90, 35,75};
Let temporary maximum variable, max = a[0] = 11
Step 1: Compare max with second element a[1]
11 < 24, so update the value of max with value of second number (24)
max = 24
Step 2: Compare max with third element a[2]
24 < 90, so update the value of max 90
max = 90
Step 3: Compare max with fourth element a[3]
90 > 35, so no change in value of max.
max = 90
Step 4: Compare max with fifth element a[4]
90 = 90, so no change in value of max.
max = 75
After iterating the complete array, the value of max is 90.
Algorithm : Finding the maximum number in a set.
Step1: Let a[1…n] be the array ,where n ≥ 1.
Step 2: Set first array element as temporary maximum max
Step3: While n > 0 do
(a)If next array element > current maximum max then assign it to max
Step 4: Return largest number max for the array .
To Remove Duplicates from an Ordered Array
Problem
Remove all duplicates from an ordered array and contract the array accordingly.
Given a sorted array, we should remove all the duplicates from the array such that each
element appears only once and array should be ordered after removing the
duplicate elements.
The elements of a sorted or ordered array are arranged in ascending (or
descending) order.
Step 1: The element at 1st position (arr[i] =11) is compared with element at 2nd position
(arr[i+1] =21).
Elements compared are different (11, 21). 11 is a unique element placed at 1st position.
arr[j]=arr[i]
arr[j] = arr[0]
arr[j] = 11
arr[0] = 11 increment i by1 and j by 1
So, i =1 and j =1
Step 2: The element at 2nd position (arr[i] =21) is compared with element at 3rd position
(arr[i+1] =21).
Elements compared are same (21, 21).
If it is same, then just increment i by 1.
i=2 , No change in J value. So, j=1
Step 3: The element at 3rd position (arr[i] =21) is compared with element at 4th position
(arr[i+1] =34).
Elements compared are different (21, 34). 21 is a unique element placed at 2nd position.
arr[j]=arr[i]
arr[j] = arr[2]
arr[j] = 21
arr[1]= 21 increment i by1 and j by 1 So, i =3 and j =2
Step 4: The element at 4th position (a[i] =34) is compared with
element at 5th position (a[i+1] =34).
Elements compared are same (34, 34).
If it is same, then just increment i by 1.
i=4
No change in J value. So, j=2
Step 5: The element at 5th position (arr[i] =34) is compared with element at 6th position
(arr[i+1] =50).
Elements compared are different (34, 50). 34 is a unique element placed at 3rd position.
arr[j]=arr[i]
arr[j] = arr[4]
arr[j] = 34
arr[2] = 34
increment i by1 and j by 1
So, i =5 and j =3
Since i value is n-1, we can't compare with a[i+1]. So, we stop iterating the loop.
Finally store the last element of an array in arr[j]
arr[j]= arr[n-1] or arr[i]
arr[3] = arr[5]
arr[3]= 50
Increment j by 1, so j = 4
Now, print first j elements of array arr.
Partition the elements of a given unordered array of n elements into two subsets so that
elements less than or equal to x are in one subset and elements greater than x are in
other subset.
In an unordered array, the elements are randomly ordered.
Example: { 38, 35, 32, 12, 15, 10, 23, 28, 4, 10}
The problem of partitioning an array to two subsets can be easily solved if we sort array
in ascending order.
But sorting is a costly operation. So a less costly method should be devised for the
problem.
Consider an array of 10 elements which has to partitioned
Let 20 be the value of partition key (x). The partitioned array with 20 as partition key is
given as below. x
By comparing the above two data sets, we can see, elements from left side which are greater than 20 must be
moved to right side and elements from right side which are less than or equal to 20 must be moved to left side.
But at first, we do not know how many elements are less than < 20.
So we need to loop though all the elements in an array and count elements less than 20.
Make another loop where any value > 20 on the left side is moved to right side of p.
When any value on the left must be moved to right side, search for a value on right that has to move to left
partition and exchange them.
A better partitioning mechanism using only one pass is as follows:
Left and right partitions are extended inwards and by exchanging values such
that left side has valued less than partitioning index x and right side has
values greater than x.
Two partitions will finally meet and array will be divided to two subsets.
“Moving inwards” process:
Movement inwards from left can proceed until we encounter an element larger than the
partitioning value x. i.e., while a[i] ≤ x and (i < j) do i = i + 1
The loop terminates when a[i] > x
Movement inwards from right can proceed until we encounter an element smaller than
or equal to x i.e., while a[j] > x and (i < j) do j = j - 1
The loop terminates when a[j] < x
Step 1: Here n = 10 and initial value of i = 0 and j = n-1 = 9 and partitioning
index x = 20
a[i] = a[0] = 1
a[j] = a[n-1] = a[9] = 38
Step 2: Since i < j is true (0 < 9 is true), lets proceed with moving inwards
While (i < j and a[i] ≤ x )do i = i + 1
0 < 9 and 1 ≤ 20 is true , so i = 0 + 1 = 1
1 < 9 and 4 ≤ 20 is true, so i = 1 + 1 = 2
2 < 9 and 32 ≤ 20 is false, so stop iterating loop.
i=2
While (i < j and a[j] > x) do j = j - 1
2 < 9 and 38 > 20 is true, so j = 9 – 1 = 8
2 < 8 and 35 > 20 is true, so j = 8 - 1 = 7
2 < 7 and 28 > 20 is true, so j = 7 - 1 = 6
2 < 6 and 10 > 20 is false, so stop iterating loop
j=6
Now, swap a[i] and a[j] i.e., a[2] and a[6] - swap 32 and 10
i= 2 + 1 = 3
j = 6-1 = 5
Step 3: i=3 ,j=5
Since i< j is true), lets proceed with moving inwards
While (i < j and a[i] ≤ x )do i = i + 1
3 < 5 but 23 ≤ 20 is false , so stop iterating loop , No change in i value, i = 3
While (i < j and a[j] > x) do j = j – 1, 3 < 5 and 12 > 20 is false, so stop iterating
loop
No change in j value, i = 5
Now, swap a[i] and a[j] i.e., a[3] and a[5] - swap 23 and 12
. If A and B are the two matrices, then the product of the two matrices A
and B are denoted by:
X = AB
Rule for Matrix Multiplication:
1. Make sure that the number of columns in the 1st matrix equals
the number of rows in the 2nd matrix
(compatibility of matrices).
2. Multiply the elements of each row of the first matrix by the
elements of each column in the second matrix.
3. Add the products.
4. Place the added products in the respective columns
Approach to Find the Multiplication of Two Matrices