0% found this document useful (0 votes)
14 views11 pages

List

Uploaded by

wiekdowow
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)
14 views11 pages

List

Uploaded by

wiekdowow
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/ 11

ARRAYS

The array data type is the simplest structured data type. It is a collection of elements of the same type (i.e. homogeneous
collection). Any element in array is access directly and randomly. Each element in array is having a fixed position in
collection known as its Index or subscript In order to denote an individual element, the name of the array is augmented by
the index. This index is to be a positive integer. Two fundamental operations store and retrieve are defined on array.
Let A is an array then the elements of A are denoted by subscript notation
a0, a1, a2, ……., an-1
or, by the parenthesis notation
A(0), A(1), A(2), A(3),……., A(n-1)
or, by the bracket notation
A[0], A[1], A[2], A[3],…….., A[n-1]
Regardless of the notation, the number K in A[K] is called a subscript and A[K] is called a subscripted variable. Linear
arrays are called one-dimensional arrays because each element in such array is referenced by one subscript. A two-
dimensional array is a collection of similar data elements where each element is referenced by two subscripts. Such arrays
are called matrices in mathematics, and tables in business applications.
2.1 Representation of Arrays in Memory
Memory may be regarded as 1-dimensional array with words numbered from 1 to m.
Memory cells Here main concern is representation of n-dimensional arrays in a 1-dimensional
memory. Also it is necessary to determine the amount of memory space to be reserved
1F00 for a particular array. The elements in array are stored in successive words (or cells) of
1F01 contiguous memory block allocated to array by operating system. Assuming that each
1F02 Word array element requires only one word of memory, the number of word needed is number
1F03
1F04
of elements in the array. If an array is declared A[l1..u1, l2..u2, ........, ln..un] then number
1F05 of elements is
1F06 n
1F07  (u
i 1
i  li  1)
1F08
1F09 Where li & ui are lower and upper bounds for ith dimension. Some languages like Pascal
permit to mention upper and lower bound for each dimension.
For Example: in array A [4..5, 2..4, 1..2, 3..4],
Address
Number of elements = (5-4+1) * (4-2+1) * (2-1+1) * (4-3+1) =2*3*2*2= 24
But in languages like C, lower bound is always zero hence upper bound represents the
number of elements in that dimension.

Declaration Dimension Number of elements


int a[5]; one =5
int b[5][4]; two =5 *4 = 20
float c[3][2][3]; three =3 * 2 * 3 = 18
char d[2][3][2][3]; four = 2 * 3 * 2 * 3 = 36
Normally size of word of memory express in bits (or Byte) depends on the hardware and operation system. Let us assume
that w is a size of word of memory. If each element of array takes more than one word of memory then total amount of
memory require in bits (or Byte) to store array is
= w * (No of words per element) * (No of Elements)
For example if one assume that word is of one byte (i.e. w=1) then (for typical C compiler)

Declaration Dimension Number of elements No words per memory required in


in Array element Byte (as w=1 byte)
int a[5]; one =5 2 = 1 * 2 * 5 = 10
int b[5][4]; two =5 *4 = 20 2 = 1 *2 * 20 = 40
float c[3][2][3]; three =3 * 2 * 3 = 18 4 = 1 *4 * 18 = 72
char d[2][3][2][3]; four = 2 * 3 * 2 * 3 = 36 1 = 1 *1 * 36 = 36

2.1.1 Representation of One-Dimensional Arrays in Memory


int a[5] float a[3]

1F00 a[0] 1F00


1F01 1F01 a[0]
1F02 a[1] 1F02
1F03 1F03
1F04 1F04
a[2]
1F05 1F05
a[1]
1F06 1F06
a[3]
1F07 1F07
1F08 1F08
a[4]
1F09 1F09
a[2]

Figure 2.1. Representation of One-Dimensional Arrays in Memory

One-dimensional array is directly mapped with one-dimensional memory as shown in Figure 2.1. The number of words
assigned to each element in array depends on the data type of array.
Consider the following prototype of declaration
data_type array_name[SIZE];
Let  is a base address or starting address of an array and s is a size-of data_type i.e. number of words of memory required
to store element of data type. The address of ith element is  + s*i.
In C programming, name of an array always stores the base address that is also address of 0 th element in array. Consider
declaration int a[5], if base address () is 1F00 then array name a stores 1F00 that is a address of 0th element i.e. &a[0] (&
is address operator). The address of 2nd element is  + s*i = 1F00 + 2*2 = 1F04.
Such arithmetic (+, -, * and /) operations on array are known as address arithmetic operations and they are different from the
arithmetic operations performed on values.
2.1.2 Representation of Two-Dimensional Arrays in Memory
In 2–dimensional array elements in array are arranged in rows and columns. Each element in array is identified by unique
row and column numbers. There are two possible ways to map 2-dimentional array in 1-dimensional memory
a. Row-major order
b. Column-major order
In Row-major order all elements of 1st row are mapped in memory first, and then all elements of 2 nd row and so on. In
Column-major order all elements of 1st column are mapped in memory first, then all elements of 2 nd column and so on. In
implementation of row-major order all the elements of 0th row are followed by all elements of 1st row and so on. Another
synonym for row-major order is lexicographic order. In implementation of column-major order all the elements of 0th column
are followed by all elements of 1st column and so on. Representation of Two-Dimensional Array in Memory in row-major
order and column-major order is shown in Figure 2.2.

Row-major order Representation of Matrix Column-major order


0 a00 in 1-Dimensional Array a00 0
Row 0
1 a01 a10 1
2 a02 a20 2
Column 0
3 a03 a30 3
4 a10 a01 4
Row 1 a00 a01 a02 a03
5 a11 a11 5
6 a12 a10 a11 a12 a13 a21 6
Column 1
7 a13 a31 7
8 a20 a20 a21 a22 a23 a02 8
Row 2
9 a21 a30 a31 a32 a33 a12 9
10 a22 a22 10
11 a23 Column 2 a32 11
12 a30 a03 12
Row 3
13 a31 a13 13
14 a32 a23 14
15 Column 3
a33 a33 15

Figure 2.2. Representation of Two-Dimensional Array in Memory


It is important to derive formula to translate 2-dimensional location (in the form row, col) of element to 1-dimensional
location (in the form of index). A 2-dimensional array A[l1..u1][l2..u2] may be interpreted as u1 rows with each row consisting
of u2 elements. In row-major order, the formula to find location of 2-D element A[i][j] in 1-D is
m = (i-l1) *(u2-l2+1) + (j-l2)
If lower bounds of both the dimensions are 0 then formula to calculate index in 1-D becomes
m = (i) *(u2+1) + (j)
For ex:
for a[2][3] the value of k= 2*(3+1)+3= 11
for a[3][1] the value of k= 3*(3+1)+1= 13
To calculate address of memory location we can use formula  + s*m.
Similarly for 3-D array formula to find out location for element A[i][j][k] is
m = (i-l1) *(u2-l2+1) (u3-l3+1) + (j-l2)(u3-l3+1) + (k-l3)
Generalizing on preceding discussion, the addressing formula for any element A[i 1[i2]....[in] in n-dimensional array declared
as A[l1..u1][l2..u2]...[ln..un] is
m = (i1-l1) *(u2-l2+1) (u3-l3+1) ..... (un-ln+1)
+ (i2-l2)(u3-l3+1) (u4-l4+1) ..... (un-ln+1)
+ (i3-l3)(u4-l4+1) (u5-l5+1) ..... (un-ln+1)
.
.
.
+(in-1-ln-1)(un-ln+1)
+ (in-ln)
Note: row-major order storage is used in C & Java and column-major order storage is used in FORTRAN.

Exercise 2.1:
1. Obtain an addressing formula for the element A[i 1][i2]....[in] in a n-dimensional array declared as
A[l1..u1][l2..u2]...[ln..un] . Assuming a column-major order representation of an array
2. Representation of lower triangular matrix and upper triangular matrix into a single matrix
3. Give suitable scheme to representation of diagonal matrix and trans diagonal matrix
4. Discuss suitable scheme to efficiently represent sparse matrix in memory.
2.3 Linear or Ordered list
A list is an abstract data type that represents an ordered collection of elements, where the same element may occur more than
once. Term list is used whenever elements coming in and being removed in a random order. For example, shelf of library
books, where a book can be removed from anywhere on the shelf and can be inserted back into their proper location when
they are returned.
A list is a sequence of zero or more elements of a given type
a0, a1, a2, ……., an-1
where n : length of list
a0 : First element of list
an-1 : last element of list
n=0 : empty list
Elements can be linearly ordered according to their position in the list. Let say a i precedes ai+1, ai+1 follow ai, and ai is at
position i. List with length 0 (i.e. no element in list) is called null list or empty list.
A list is a container that stored elements in a certain linear order, which is imposed by the operations performed. The first
element of a list is called head of list and the last element is called the tail of list. The basic operations performed on list are
retrieval, insertion, deletion and traversal.
Special types of lists include stacks and queues, where insertions and deletions can be done only at the head or the tail of the
list. The basic realization of list is by means of arrays and linked lists.
2.3.1 Implementation of List using Array
Array is a very natural data structure to represent an ordered list. An ordered list is a collection of elements that are placed in
certain order. Array is a container of elements of same type and index (or subscript) of an individual element in array set
order of element in collection. An array of size greater than or equal to the number of elements in list is required for
representation.
To variables are required in order to implement list using array
1. List: an array of SIZE (i.e. maximum number of elements that can be hold by the List).
2. n: integer variable gives information about number of elements in the list.
A list can be implemented from either end of an array as shown in Figure 2.3. Let us consider a list of n elements that is
stored in array of capacity SIZE ≥ n. These n elements can be stored in consecutive locations on either end of array (in
increasing or decreasing order of index of an array). The simplest implementation is array positions correspond to list
positions (i.e increasing order of index). In other words, the element at position i in the list is stored at array cell with index
i. The head of the list is always at position 0. This makes random access to any element in the list quite easy. Given some
position in the list, the value of the element in that position can be accessed directly in Θ(1) time.
List stored in array with List stored in array with
increasing order of index decreasing order of index
Order of Order of
element element element element

0 a0 0 0
1 a1 1 1
2 a2 2 2 an-1 n-1
3 3

an-1 n-1 a2 2
a1 1
SIZE-1 SIZE-1 a0 0

Index of array Index of array

Figure 2.3. Implementation of List using Array


Insertion Operation: this operation adds an element to the list. Insertion of an element at the end of a list (also called as
append operation) can easily done provided the memory space allocated for the list is large enough to accommodate the
additional element. In order to insert an element at position p in the list, the elements at p and followed by p must be move
downward to new locations to accommodate the new element by keeping the order of the other elements intact. If p ≥ n then
new element is appended to the list. It is not possible to insert element in the list if list is full (i.e. n == SIZE) and it generates
list overflow condition. Figure 2.4 shows insertion of elements in a list implementation using Array

0 5 0 5 0 5 0 5
1 9 1 9 1 9 1 9
2 15 2 2 10 Insert 11 at 2 10
3 17 Insert 10 at 3 15 3 15 3 15
2nd position end of the list
4 7 4 17 4 17 4 17
5 7 5 7 5 7
6 11
SIZE-1 SIZE-1 SIZE-1 SIZE-1
nd
Initial List Shift elements Insert 10 at 2 position Append 11

Figure 2.4. Insertion of elements in a list implementation using Array

Appending element to a list does not need any shifting hence time complexity is O(1). Insertion at the beginning or somewhere
in the middle requires shifting of elements. In worst case (i.e. insertion at 0th position) needs shifting of n elements hence
time complexity is O(n).
Following is a code for insertion operation. Function Inslist insert element (i.e. data) at position p in the list.

# define SIZE 10
int List[SIZE], n= 0;

void Inslist( int data, int p)


{
int i;

if (n == SIZE)
printf("\n Error: List is full -- Overflow \n");
else
if (p >= n) // append element
{
List[n]=data;
n++;
}
else
{
for(i=n; i>p; i--)
List[i]=List[i-1];
List[p]=data;
n++;
}
}
Deletion Operation: This operation removes an element from the list. Deletion of last element in the list presents no
difficulties. But deletion of element somewhere in the beginning or middle of the list requires upward shifting of elements
preceded by deleted element to fill up the gap in the list. Any attempt to delete element from the list at position >=n gives an
error as there is no element at that mapped index in an array. It is not possible to delete element from empty list. Figure 2.5
shows deletion of elements from a list implementation using Array.

0 5 0 5 0 5 0 5
1 9 1 9 1 9 1 9
2 15 Delete element 2 2 17 Delete element 2 17
3 17 at 2nd position 3 17 3 7 at 7th position 3 7
4 7 4 7 4 11 4 11
5 11 5 11

SIZE- SIZE- SIZE- SIZE-


1 1 1 Error: Position 1is out of List
Initial List Shift elements

Figure 2.5. Delete elements from a list implementation using Array

Deletion of last element from list does not need any shifting hence time complexity is O(1). Deletion of element at the
beginning or somewhere in the middle requires shifting of elements. In worst case (i.e. deletion element at 0th position) needs
shifting of n-1 elements hence time complexity is O(n).
Following is a code for deletion operation. Function Dellist delete element at position p in the list.

void Dellist(int p)
{
int i;

if(n == 0)
printf(" \n Error: List is empty -- Underflow\n ");
else
if( p >= n)
printf("\nError: Position is out of List\n");
else
{
printf("Element deleted from List is:%d\n", List[p]);
for(i=p; i<n-1; i++)
List[i]=List[i+1];
n--;
}
}
Traversal operation: Traversing means accessing and processing (frequently called visiting) each element in list at least
once. Visit to all elements in list can be carried out in particular order. List can be traverse from head to tail or tail to head.
In both the cases the time complexity is O(n). Following is a code for traversal operation

void Traverse()
{
int i;

if(n == 0)
printf(" \n List is empty\n ");
else
{
printf("Traversing in List\n");
for(i=0; i< n; i++)
printf("%d\t", List[i]);
printf("\n");
}
}

Exercise 2.2:
1. Write a function to implement
(i) Get (p): where p is a position, It returns the element at position p or an error number if p ≥ n.
(ii) Set(x, p): where p is a position and x is element: It Replaces the element at position p with x or show an error
message if p ≥ n.
(iii) Find(x): where x is element: Returns the position of the first occurrence of x in the list. If x does not occur in
the list, then it returns error number.
2. Compute time complexity of following operations on list implemented using array. Give suitable justification.
Get, Set, Find and Reversal.
3. Given two sorted lists, L1 and L2, write a function to compute L1 L2.
4. Given two sorted lists, L1 and L2, write a function to compute L1 L2.
5. Write a function to delete duplicate elements in a list.
6. An alternative to the deletion strategy is to use lazy deletion. To delete an element, mark it deleted (using an extra
bit field). The number of deleted and non-deleted elements in the list is kept as part of the data structure. If there are
as many deleted elements as non-deleted elements, traverse the entire list, performing the standard deletion operation
on all marked nodes. Give implementation of lazy delete and also state the advantages and disadvantages of lazy
deletion.
7. Consider an array of size n. Write an algorithm to shift all items in the array k places cyclically counter-clockwise.
You are allowed to use only one extra location to implement swapping of items.
8. Let L be a linear list represented in the array. Write a function to make an in-place reversal of the order of elements
in L. The only additional space available is a simple variable. How much time does a function take to accomplish
the reversal?
2.3.2 Application of List: Polynomial representation
Let P(x) = anxn+ an-1xn-1+…….+ a2n2+ a1x+ a0 is a polynomial of degree n (if an ≠ 0) in variable x. So polynomial is a collection
of terms and each term has coefficient & exponent. Terms in polynomial can be arranged in increasing or decreasing order of
their exponent. Hence polynomial is an ordered list of terms.
The simplest way to represent polynomial using array is mapping each term of polynomial with each element in array as follows
Term of Polynomial Element of Array
Coefficient Value
Exponent Index
Declaration: float poly[N];
Creates an array of real values representing the coefficient of terms in polynomial and indexes are automatically mapped to
exponent.
For Example: P(x) = 4.9x5 - 7.8x4 + 3.0x3 + 4.6x2 + 5.2x –7.1 is represented as

Index  0 1 2 3 4 5  Exponent
Value  -7.1 5.2 4.6 3.0 -7.8 4.9  Coefficient

This is a very simple scheme to represent polynomial and very effective if polynomial is not a sparse polynomial. Sparse
polynomial is a polynomial in which number of terms with non-zero coefficient is less than the number of terms with zero
coefficients.
For Example: P(x) = 4.9x6- 0x5 - 0x4 + 3.0x3 + 0x2 + 0x –7.1 is polynomial having 3 terms with non-zero coefficient and 4
terms with zero coefficients. Hence P(x) is a sparse polynomial.
If polynomial is sparse e.g. x1000 + 1 then 1001 array elements are assigned to store this polynomial out of which only 2 elements
hold non-zero values and rest elements store 0. This leads to wastage of memory space.

In another scheme to represent polynomial, term is a structure (collection) of coefficient & exponent and polynomial is an array
of terms. Number of terms in polynomial is always ≤ the SIZE of an array.

#define SIZE 10
struct term
{
float coef;
int expo;
} poly[SIZE];

For Example: P(x) = 4.9x5 - 7.8x4 + 4.6x2 + 5.2x –7.1 (with number of terms 5) is represented as
Index (Term Number)  0 1 2 3 4 5 .. .. SIZE-1
Coefficient  4.9 -7.8 4.6 5.2 -7.1
Exponent  5 4 2 1 0

Following are the functions to read and print polynomial. Variable nterm stores number of terms in polynomial.
Read_poly(int nterm)
{
int i;
printf("Enter terms of Polynonial:\n");
for (i=0; i < nterm; i++)
scanf("%f%d", &poly[i].coef, &poly[i].expo);
}

Print_poly(int nterm)
{
int i;

for (i=0; i<nterm; i++)


printf ("%4.2f %d\n", poly[i].coef, poly[i].expo);
}

Polynomial Addition: Following example shows addition of two polynomials.


polyA( x)  6.0 x 6  7.5 x 4  8.0 x  9.7
polyB( x)  5.2 x 5  1.5 x 4  2.3 x 2

polyC ( x)  polyA( x)  polyB( x)  6.0 x 6  5.2 x 5  6.0 x 4  2.3 x 2  8.0 x  9.7

Here polyA(x) and polyB(x) are two input polynomials for addition and polyC(x) is a resultant polynomial that stores sum of
two input polynomials. In order to perform addition, sum the coefficient of terms with same exponent in both the polynomials.
If this sum comes out to be non-zero then add term with this sum & exponent to the resultant polynomial. Add terms with
distinct (not common in both polynomials) exponent to resultant polynomial as it is. During addition operation each
polynomial is traversed using marker or pointer. There are three situations
1. Terms available in both the polynomials for processing (addition)
2. Terms available in first polynomial and second polynomial is processed completely
3. Terms available in second polynomial and first polynomial is processed completely

Let pa, pb & pc are markers moving in these three polynomials. Marker pc always point to location in resultant polynomial
where term to be added. Following procedure depicts the addition of two polynomials
1. Let pa=0 & pb=0, markers pointing to 1st terms in polyA & polyB. Let pc=0, marker set at 1st term in polyC where
new term is to be added. .

polyA 6.0 -7.5 8.0 9.7


6 4 1 0

pa
5.2 1.5 2.3
polyB
5 4 2 pc

pb

2. Exponent of term pointed by pa is greater than exponent of term pointed by pb hence add term pointed by pa to
resultant polynomial polyC. Move pointer pa to next term in polyA and move pc to next location in polyC.

polyA 6.0 -7.5 8.0 9.7


6 4 1 0
6.0
pa pa 6
5.2 1.5 2.3
polyB
5 4 2 pc

pb

3. Exponent of term pointed by pb is greater than exponent of term pointed by pa hence add term pointed by pb to
resultant polynomial polyC. Move pointer pb to next term in polyB and move pc to next location in polyC
polyA
6.0 -7.5 8.0 9.7
6 4 1 0
6.0 5.2
pa 6 5
5.2 1.5 2.3
polyB
5 4 2 pc

pb pb

4. Exponent of terms pointed by pa & pb are same and also sum of their coefficients (-7.5+1.5=-6.0) is non-zero. Add
new term in polyC that contains coefficients (-6.0) and exponent (4). Move pointers pa & pb to next term in polyA &
polyB respectively and move pc to next location in polyC.

polyA
6.0 -7.5 8.0 9.7
6 4 1 0
6.0 5.2 -6.0
pa pa 6 5 4
5.2 1.5 2.3
polyB
5 4 2 pc

pb pb

5. Exponent of term pointed by pb is greater than exponent of term pointed by pa hence add term pointed by pb to
resultant polynomial polyC. Move pointer pb to next term in polyB and move pc to next location in polyC.

polyA 6.0 -7.5 8.0 9.7


6 4 1 0
6.0 5.2 -6.0 2.3
pa 6 5 4 2
5.2 1.5 2.3
polyB
5 4 2 pc

pb pb

6. Now pb points to end of polyB but there are terms in polyA. It means there are un-processed terms in polyA whereas
polyB is processed completely. Hence copy remaining terms of polyA to polyC.

polyA 6.0 -7.5 8.0 9.7


6 4 1 0
6.0 5.2 -6.0 2.3 8.0 9.7
pa pa pa 6 5 4 2 1 0
5.2 1.5 2.3
polyB
5 4 2 pc

pb

Function polyAdd add two polynomials. Variables ntermA & ntermB store number of terms in polyA and polyB respectively.

#define SIZE 10
struct term
{
float coef;
int expo;
};
typedef struct term poly;

int polyAdd(poly *polyA, int ntermA, poly *polyB, int ntermB, poly *polyC)
{
int pa=0, pb=0, pc=0 ;
float temp;

while (pa <ntermA && pb<ntermB) //move in polyA & polyB as long as there are terms in both the polynomials
if (polyA[pa].expo > polyB[pb].expo)
{
polyC[pc].expo = polyA[pa].expo;
polyC[pc].coef = polyA[pa].coef;
pa++; pc++;
}
else
if (polyA[pa].expo < polyB[pb].expo)
{
polyC[pc].expo = polyB[pb].expo;
polyC[pc].coef = polyB[pb].coef;
pb++; pc++;
}
else
{
temp = polyA[pa].coef + polyB[pb].coef;
if (temp != 0)
{
polyC[pc].coef = temp;
polyC[pc].expo = polyA[pa].expo;
pc++;
}
pb++; pa++;
}
while (pa <ntermA) // Terms available in polyA and polyB is processed completely

{
polyC[pc].expo = polyA[pa].expo;
polyC[pc].coef = polyA[pa].coef;
pa++; pc++;
}
while (pb <ntermB) // Terms available in polyB and polyA is processed completely
{
polyC[pc].expo = polyB[pb].expo;
polyC[pc].coef = polyB[pb].coef;
pb++; pc++;
}
return(pc);
}

Table 2.1 shows run of the function polyAdd with polynomials:


polyA(x)= 6.0x6 - 7.5x4 + 8.0x1 + 9.7
polyB(x)= 5.2x5 + 1.5x4 + 2.3x2

Table 2.1. Run of function polyAdd


pa pb polyA[pa].expo Relation polyB[pb].expo pc polyC[pc].coef polyC[pc].expo
0 0 6 5 0 -- --
0 1 0 6 > 5 0 1 6.0 6
1 0 1 4 < 5 1 2 5.2 5
1 2 1 2 4 = 4 2 3 -7.5+1.5=6.0 4
2 23 1 < 2 3 4 2.3 2
2 3 3 1 End of polyB 4 5 8.0 1
3 4 3 0 -- 5 6 9.7 0
4 3 End of polyA -- 6

Read_poly(poly *p, int nterm)


{
int i;
printf("Enter terms of Polynonial:\n");
for (i=0; i < nterm; i++)
scanf("%f%d", &p[i].coef, &p[i].expo);
}
Print_poly(poly *p, int nterm)
{
int i;
for (i=0; i<nterm; i++)
printf ("%4.2f %d\n", p[i].coef, p[i].expo);
}
main()
{
poly polyA[SIZE], polyB[SIZE], polyC[2*SIZE];
int ntermA, ntermB, ntermC;

printf("enter number of terms in Polynomial A:");


scanf("%d", &ntermA);
Read_poly(polyA, ntermA);
//Print_poly(polyA, ntermA);

printf("enter number of terms in Polynomial B:");


scanf("%d", &ntermB);
Read_poly(polyB, ntermB);
//Print_poly(polyB, ntermB);

ntermC=polyAdd(polyA, ntermA, polyB, ntermB, polyC);


printf("Addision of Polynomials A & B:\n");
Print_poly(polyC, ntermC);
getch();
}

Exercise2.3:
1. Write a function to implement
a. Evaluation of polynomial P(x) at x= x0
b. Multiply polynomial P(x) with cxn
c. Integration of given polynomial P(x)
d. Differentiation of given polynomial P(x)

2. Write function to do following operations on two polynomials


a. Subtraction
b. Multiplication

4. You are given an array S of n integers and another integer x.


(a) Describe an O(n log n) algorithm (in the sense of the worst case performance) that determines whether or not there
exist two elements in S whose sum is exactly x.
(b) Describe an algorithm that accomplishes the same task, but runs in O(n) expected (i.e., average) time.

5. Given two arrays of n integers, design an algorithm that finds out in O(n log(n)) steps if the two arrays have an element
in common.

6. Given an array A[1..100] which contains all natural numbers between 1 and 99, design an algorithm that runs in O(n) and
returns the duplicated value.

7. Assume you are given two arrays A and B, each containing n distinct positive numbers and the equation x 8 - x4y4 = y6 +
x2y2 + 10. Design an algorithm which runs in time O(n log n) which finds if A contains a value for x and B contains a
value for y that satisfy the equation.

8. You're given an array of n integers, and must answer a series of n queries, each of the form: “how many elements of the
array have value between L and R?", where L and R are integers. Design an O(n log n) algorithm that answers all of these
queries.

9. Assume you have an array of 2n distinct integers. Find the largest and the smallest number using 3n - 2 comparisons only.

10. Let M be an n x n matrix of distinct integers M(i, j) Each row and each column of the matrix is sorted in the increasing
order. You need to determine whether M contains an integer x in O(n) time.

11. You are given an array A of n distinct integers.


a) You have to determine if there exists a number (not necessarily in A) which can be written as a sum of squares of
two distinct numbers from A in two different ways (note: m2+n2 and n2+m2counts as a single way) and which runs
in time n2 log n in the worst case performance. Note that the brute force algorithm would examine all quadruples
of elements in A and there are
O(n4) such quadruples.
b) Solve the same problem but with an algorithm which runs in the expected time of O(n2).

12. Suppose that you bought a bag of n bolts with nuts screwed on them. Your 5 year old nephew unscrewed all the nuts
from the bolts and put both the nuts and the bolts back into the bag. The bolts are all of similar quite large size but
are actually of many different diameters, differing only by at most a few millimetres, so the only way to see if a nut
fits a bolt is to try to screw it on and determine if the nut is too small, if it ts or if it is too large. Design an algorithm
for matching each bolt with a nut of a fitting size which runs in the expected time O(n log n).

13. You are given 1024 apples, all of similar but different sizes and a small pan balance which can accommodate only
one apple on each side. Your task is to find the heaviest and the second heaviest apple while making at most 1032
weightings.

14. You are in an orchard which has a quadratic shape of size 4n by 4n with equally spaced trees. You purchased apples
from n2 trees which also form a square, but the owner is allowing to choose such a square anywhere in the orchard.
You have a map with the number of apples on each tree. Your task is to choose such a square which contains the
largest total number of apples and which runs in time O(n 2). Note that the brute force algorithm would run in time
(n4).

15.

16.

You might also like