You are on page 1of 25

Module 2 : Arrays and Searching

1.Polynomial representation using Arrays

Array

An array is defined as the collection of similar type of data items stored at contiguous memory
locations. The array is the simplest data structure where each data element can be randomly accessed
by using its index number.

For example, if we want to store the marks of a student in 6 subjects, then we don't need to define
different variables for the marks in the different subjects. Instead of that, we can define an array
which can store the marks in each subject at the contiguous memory locations.

We can declare an array in the c language in the following way.

data_type array_name[array_size];

Now, let us see the example to declare the array.

int marks[5]; //Here, int is the data_type, marks are the array_name, and 5 is the array_size.

Representation of Polynomial

A polynomial is an expression that contains more than two terms. A term is made up of coefficient
and exponent. An example of polynomial is

P(x)= 4x3 +6 x2+7x+9

Array representation assumes that the exponents of the given expression are arranged from 0 to the
highest value(degree), which is represented by the subscript of the array beginning with 0. The
coefficients of the respective exponent are placed at an appropriate index in the array. The array
representation for the above polynomial expression is given below

1
Creating a structure for polynomial
struct poly
{
float coeff;
int exp;
}a[3]; // Here an array of structure is defined. A polynomial with three terms can be read to this
array. Constant term at index 0 , first term with degree one at index 1, second term with degree 2 at
index 2. Coefficients can be read from input and corresponding exponent is the array index itself.

coeff
0
exp
coeff
1
exp
coeff
2
exp

Addition of Two Polynomial

For adding two polynomials using arrays is straight forward method, since both the arrays may be
added up element wise beginning from 0 to n-1, resulting in addition of two polynomials. Addition
of two polynomials requires comparing the exponents, and wherever the exponents are found to be
same, the coefficients are added up. For terms with different exponents, the complete term is simply
added to the result thereby making it a part of addition result.
Algorithm

Step1: Define a structure that holds the polynomial terms and a structure variable as array with size
to hold as many terms in polynomial.

struct poly
{
int coef;
int exp;
}p[3];

//( A structure named poly and structure variable p[3] that can hold three polynomial terms.)

MAIN () FUNCTION
Step 1 : Start
Step 2 : Declare variables to hold the highest degrees of polynomials.
Step 3 : Read the polynomial terms from the user from constant term to the highest degree.
a. Read coefficient from input.
b. Exponent is same as index.
2
Step 4 : Display the polynomials
Step 5 : To add two polynomials
Step 6 : Check highest degree of polynomial 1 greater than or equal to polynomial 2
Step 7 : If true add coefficients of polynomial 1 and 2 whenever corresponding exponents are same
until degree 2.
Step 8 : If any remaining terms in polynomial 1, just append it to the resultant polynomial.
Step 9 : If step 6 is false add coefficients of polynomial 1 and 2 whenever corresponding exponents
are same until degree 1.
Step 10: If any remaining terms in polynomial 2, just append it to the resultant polynomial until
degree

2.Sparse matrix

A matrix is a two-dimensional data object made of m rows and n columns, therefore having total m x
n values. If most of the elements of the matrix have 0 value, then it is called a sparse matrix.

Eg. 00304
00570
00000
02600

Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the
matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we
only store non-zero elements. This means storing non-zero elements with triples- (Row, Column,
value).

2D array is used to represent a sparse matrix in which there are three rows named as
● Row: Index of row, where non-zero element is located
● Column: Index of column, where non-zero element is located
● Value: Value of the non zero element located at index – (row,column)

MAIN () FUNCTION
Step 1 : Start
Step 2 : Declare and initialize the values of a 4*5 sparse matrix.
Note: If no: of zeroes is greater than row*col/2, then the matrix is sparse , else not.
Step 3 : Declare a variable, COUNT to count the number of non zero elements and initialize it as 0.
Step 4 : Check every i, j th element, whether it is non zero, if non zero, increment above variable by
1.

3
Step 5 : Declare a resultant sparse matrix with 3 rows and COUNT (no: of non zero elements)
columns.
Step 6 : Iterate through every i, jth element, On every non zero element corresponding row value,
column value, and the i,jth element is saved to resultant matrix.

The following are the advantages of using a sparse matrix:

o Storage: As we know, a sparse matrix that contains lesser non-zero elements than zero so less
memory can be used to store elements. It evaluates only the non-zero elements.
o Computing time: In the case of searching n sparse matrix, we need to traverse only the
non-zero elements rather than traversing all the sparse matrix elements. It saves computing
time by logically designing a data structure traversing non-zero elements.

3.Stacks

Stack is a linear data structure which follows a particular order in which the operations are
performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).

There are many real-life examples of a stack. Consider an example of plates stacked over one
another in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate
which has been placed at the bottommost position remains in the stack for the longest period of time.
So, it can be simply seen to follow LIFO(Last In First Out)/FILO(First In Last Out) order.

Operations performed on Stacks

The following are the basic operations served by the Stacks.


● Push: This function adds an element to the top of the Stack.
● Pop: This function removes the topmost element from the stack.
● IsEmpty: Checks whether the stack is empty.
4
● IsFull: Checks whether the stack is full.
● Top: Displays the topmost element of the stack.

Time Complexity of Stack Operations

● As mentioned above, only a single element can be accessed at a time in Stacks.


● While performing push() and pop() operations on the stack, it takes O(1) time.

Applications of Stack

● Evaluation of Expressions
● Tower of Hanoi
● N queens problem
● Conversion of Infix to prefix or postfix expression.

ALGORITHM:
Step1: Define the maximum size of stack as 5
MAIN () FUNCTION
Step 1: Start
Step 2: Initialize the top of stack (top) to -1
Step 3: Display the list of choice available for the user to choose from :
1. Push 2. Pop 3. Display 4. exit
Step 4: Read the choice from the user as choice
Step 5: If choice ==1 then read an item to be pushed from user as item.
If stack is full then display “stack is full” else call the function push() with item as argument .
Step 6: If choice==2 then check whether “stack is empty”.
If stack is empty then display “stack is empty” else call the function pop().
Step 7: If choice ==3 then call the display() function
Step 8: If choice==4 then go to step9
Step 9: Stop.
TO CHECK WHETHER STACK IS FULL.
Step 1: If top of stack (top) is greater than or equal to size-1 then
Return 1 else return 0.
VOID PUSH (INT ITEM) – FUNCTION TO PUSH AN ITEM INTO STACK.
Step 1: Increment the value of top of stack(top) by 1.
Step 2: Store the item in the top position of the stack as follows: Stack[top]=item;
TO CHECK WHETHER STACK IS EMPTY.
Step 1: if top of stack (top) is -1 then return 1 else return 0.
INT POP () – FUNCTION TO POP AN ELEMENT FROM STACK.
Step 1: Store the element at top of the stack to item
Step 2: Decrement the value of top by 1
Step 3: Return the value of item.
VOID DISPLAY () – FUNCTION TO DISPLAY THE ELEMENTS PRESENT IN THE
STACK
Step 1: Check whether the stack is empty by calling the function stempty()
If it is empty, then display “stack is empty” else go to step2
Step 2: Display the elements in the stack using for loop shown

5
4.Queues

A queue in C is basically a linear data structure to store and manipulate the data elements. It
follows the order of First In First Out (FIFO). In queues, the first element entered into the array is the
first element to be removed from the array. A good example of a queue is any queue of consumers
for a resource where the consumer that came first is served first.
The difference between stacks and queues is in removing. In a stack we remove the item that is most
recently added; in a queue, we remove the item that is least recently added.

Operations on Queue:

Mainly the following four basic operations are performed on queue:

● Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow
condition.
● Dequeue: Removes an item from the queue. The items are deleted in the same order in which
they are inserted. If the queue is empty, then it is said to be an Underflow condition.
● Front: Get the front item from queue.
● Rear: Get the last item from queue.

Working of Queue

Queue operations work as follows:

● two pointers FRONT and REAR


● FRONT track the first element of the queue
● REAR track the last element of the queue
● initially, set value of FRONT and REAR to -1

Enqueue Operation

● check if the queue is full


● for the first element, set the value of FRONT to 0
● increase the REAR index by 1
● add the new element in the position pointed to by REAR

Dequeue Operation

● check if the queue is empty


● return the value pointed by FRONT
● increase the FRONT index by 1
● for the last element, reset the values of FRONT and REAR to -1

6
ALGORITHM:
Step 1: Define the maximum size of the queue
MAIN () FUNCTION
Step 1: Start
Step 2: Display the list of choices available to the user to choose from:
1. INSERT 2. DELETE 3. DISPLAY4. EXIT
Step 3: If ch==1, then call the function insert() and display()
Step 4: If ch==2 then call the function delete() and display()
Step 5: If ch==3 then call the function display()
Step 6: If ch==4 then exit
Step 7: Stop.
VOID INSERT() – ENQUEUE -FUNCTION TO INSERT AN ELEMENT IN TO QUEUE
Step 1: If rear=size-1 then display “Queue is Full”
Step 2: Increment rear by 1.
Step 3: Read the element from the user and store it in queue_array [rear]
Step 4: If front==-1 then display “Queue is empty”
VOID DELETE() –DEUEUE- FUNCTION TO REMOVE AN ELEMENT FROM QUEUE

7
Step 1: If front==-1 then display “Queue is empty”
Step 2: Dequeue the element at arr[front]
Step 3: If front==rear then assign -1 to front and rear else increment front by 1.
VOID DISPLAY ()- FUNCTION TO DISPLAY THE ELEMENT IN QUEUE
Step 1: If front== -1 then display, “Empty Queue”
Step 2: Display the element using for loop from i=front to i<=rear, print queue_array [i]

4.1 Circular Queues

Circular Queue is a linear data structure in which the operations are performed based on FIFO (First
In First Out) principle and the last position is connected back to the first position to make a circle. It
is also called ‘Ring Buffer’. In a normal Queue, we can insert elements until the queue becomes full.
But once the queue becomes full, we cannot insert the next element even if there is a space in front
of the queue.

Circular Queue Operations

● Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow
condition.
● Dequeue: Removes an item from the queue. The items are deleted in the same order in which
they are inserted. If the queue is empty, then it is said to be an Underflow condition.

The circular queue work as follows:

● two pointers FRONT and REAR


● FRONT track the first element of the queue
● REAR track the last elements of the queue
● initially, set value of FRONT and REAR to -1

1. Enqueue Operation

● check if the queue is full


● for the first element, set value of FRONT to 0
● circularly increase the REAR index by 1 (i.e. if the rear reaches the end, next it would be at
the start of the queue)
● add the new element in the position pointed to by REAR

2. Dequeue Operation

● check if the queue is empty


● return the value pointed by FRONT
● circularly increase the FRONT index by 1
● for the last element, reset the values of FRONT and REAR to -1

8
However, the check for full queue has a new additional case:

● Case 1: FRONT = 0 && REAR == SIZE - 1


● Case 2: FRONT = REAR + 1

The second case happens when REAR starts from 0 due to circular increment and when its value is
just 1 less than FRONT, the queue is full.

#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;
// Check if the queue is full
int isFull()
{
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1))
return 1;
return 0;
}
// Check if the queue is empty
int isEmpty()
{
if (front == -1)
return 1;
return 0;
}
// Adding an element
void enQueue(int element)
{
if (isFull())
printf("\n Queue is full!! \n");
else
{
if (front == -1)
front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}
// Removing an element
int deQueue()
{
int element;
if (isEmpty())
{
printf("\n Queue is empty !! \n");
return (-1);
}
else

9
{
element = items[front];
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}
// Display the queue
void display()
{
int i;
if (isEmpty())
printf(" \n Empty Queue\n");
else
{
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE)
{
printf("%d ", items[i]);
}
printf("%d ", items[i]);
}
}
int main()
{
int ch;
while (1)
{
printf(“Enter the choice”);
printf(“ 1: Insert\t 2: Delete\t 3.Display\t 4. Exit”);

switch(ch)
{
case 1: printf(“Enter the element to be inserted”);
scanf(“%d”, &x);
enQueue(x);
break;
case 2 : deQueue();
break;
case 3: display();
break;
case 4: exit(1);

10
default : printf("Wrong choice \n");
}
}
}

11
12
4.2Priority Queues

A priority queue is a special type of queue in which each element is associated with a priority
value. And, elements are served on the basis of their priority. That is, higher priority elements are
served first.

However, if elements with the same priority occur, they are served according to their order in the
queue.

Assigning Priority Value

Generally, the value of the element itself is considered for assigning the priority. For example,

The element with the highest value is considered the highest priority element. However, in other
cases, we can assume the element with the lowest value as the highest priority element.

We can also set priorities according to our needs

Types of Priority Queue:

● Min Priority Queue: In min priority Queue minimum number of value gets the highest
priority and lowest number of element gets the highest priority.

● Max Priority Queue: Max priority Queue is the opposite of min priority Queue in it
maximum number value gets the highest priority and minimum number of value gets the
minimum priority.

Operations on priority queue.

Insertion

● Ask the data and its priority from the user.


● If front is equal to 0 and rear is equal to n-1 then Queue is full.
● Else if front is equal to the -1 them queue is empty so we have initialize front and rear with 0.
● Insert the data entered by user in Queue using rear.

13
● If rear is equal to n-1 and front is not equal to 0 then there is empty space in queue before the
front for using that space we will shift the elements of the queue with the help of front and
rear.
● Insert the data in the queue before at the position where given priority is greater than priority
of the element in queue.

Deletion

● Remove the element and the priority from the front of the queue.
● Increase front with 1.

Print

● Using loop take the starting point from the front of the queue and ending point from the rear
of the queue and print the data.

4.3Double Ended Queues

Deque

Deque or Double Ended Queue is a type of queue in which insertion and removal of elements can
either be performed from the front or the rear. Thus, it does not follow FIFO rule (First In First Out).

Types of Deque

● Input Restricted Deque


In this deque, input is restricted at a single end but allows deletion at both the ends.

● Output Restricted Deque


In this deque, output is restricted at a single end but allows insertion at both the ends.

Deque provides four operations.

14
Below Figure shows the basic operations on a deque.

● enqueue_front: insert an element at front.


● dequeue_front: delete an element at front.
● enqueue_rear: insert element at rear.
● dequeue_rear: delete element at rear.

5 Evaluation of Expressions

In the C programming language, an expression is evaluated based on the operator precedence and
associativity. When there are multiple operators in an expression, they are evaluated according to
their precedence and associativity. The operator with higher precedence is evaluated first and the
operator with the least precedence is evaluated last.

To understand expression evaluation in c, let us consider the following simple example expression…

10+4*4 /2

In the above expression, there are three operators +, * and /. Among these three operators, both
multiplication and division have the same higher precedence and addition has lower precedence. So,
according to the operator precedence both multiplication and division are evaluated first and then the
addition is evaluated. As multiplication and division have the same precedence they are evaluated
based on the associativity. Here, the associativity of multiplication and division is left to right. So,
multiplication is performed first, then division and finally addition. So, the above expression is
evaluated in the order of * / and +. It is evaluated as follows...

4 * 3 ====> 12

12 / 2 ===> 6

10 + 6 ===> 16

The expression is evaluated to 16.

What is Operator Precedence?

Operator precedence is used to determine the order of operators evaluated in an expression. In the C
programming language every operator has precedence (priority). When there is more than one
operator in an expression the operator with higher precedence is evaluated first and the operator with
the least precedence is evaluated last.

What is Operator Associativity?

Operator associativity is used to determine the order of operators with equal precedence evaluated in
an expression. In the C programming language, when an expression contains multiple operators with
equal precedence, we use associativity to determine the order of evaluation of those operators.

In c programming language the operator precedence and associativity are as shown in the following
table.

15
Precedence Operator Operator Meaning Associativity

1 () function call Left to Right

[] array reference

-> structure member access

. structure member access

2 ! negation Right to Left

~ 1's complement

+ Unary plus

- Unary minus

++ increment operator

-- decrement operator

& address of operator

* pointer

sizeof returns size of a variable

(type) type conversion

3 * multiplication Left to Right

/ division

% remainder

4 + addition Left to Right

- subtraction

5 << left shift Left to Right

>> right shift

6 < less than Left to Right

<= less than or equal to

16
> greater than

>= greater than or equal to

7 == equal to Left to Right

!= not equal to

8 & bitwise AND Left to Right

9 ^ bitwise EXCLUSIVE OR Left to Right

10 | bitwise OR Left to Right

11 && logical AND Left to Right

12 || logical OR Left to Right

13 ?: conditional operator Left to Right

14 = assignment Right to Left

*= assign multiplication

/= assign division

%= assign remainder

+= assign addition

-= assign subtraction

&= assign bitwise AND

^= assign bitwise XOR

|= assign bitwise OR

<<= assign left shift

>>= assign right shift

15 , Separator Left to Right

17
An expression can be written with constants, variables, and symbols that can act as an operator or
parenthesis. All this expression needs to follow a specific set of rules. According to this rule, the
parsing of the expression is done based on grammar.

An arithmetic expression is expressed in the form of Notation. Now, there are three ways to write an
expression in Arithmetics:

● Infix Notation
● Prefix (Polish) Notation
● Postfix (Reverse-Polish) Notation

Infix Notation
We write expressions in infix notation, e.g. a - b + c, where operators are used in-between operands.
It is easy for us humans to read, write, and speak in infix notation but the same does not go well with
computing devices. An algorithm to process infix notation could be difficult and costly in terms of
time and space consumption.

Prefix Notation
In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For
example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish
Notation.
Conversion of Infix to Prefix using Stack

K + L - M * N + (O^P) * W/U/V * T + Q

If we are converting the expression from infix to prefix, we need first to reverse the expression.

The Reverse expression would be:

Q + T * V/U/W * ) P^O(+ N*M - L + K

To obtain the prefix expression, we have created a table that consists of three columns, i.e., input
expression, stack, and prefix expression. When we encounter any symbol, we simply add it into the
prefix expression. If we encounter the operator, we will push it into the stack.

Input expression Stack Prefix expression

Q Q

+ + Q

T + QT

18
* +* QT

V +* QTV

/ +*/ QTV

U +*/ QTVU

/ +*// QTVU

W +*// QTVUW

* +*//* QTVUW

) +*//*) QTVUW

P +*//*) QTVUWP

^ +*//*)^ QTVUWP

O +*//*)^ QTVUWPO

( +*//* QTVUWPO^

+ ++ QTVUWPO^*//*

N ++ QTVUWPO^*//*N

* ++* QTVUWPO^*//*N

M ++* QTVUWPO^*//*NM

- ++- QTVUWPO^*//*NM*

L ++- QTVUWPO^*//*NM*L

+ ++-+ QTVUWPO^*//*NM*L

K ++-+ QTVUWPO^*//*NM*LK

QTVUWPO^*//*NM*LK+-++

The above expression, i.e., QTVUWPO^*//*NM*LK+-++, is not a final expression. We need to


reverse this expression to obtain the prefix expression.

The prefix expression is ++-+KL*MN*//*^OPWUVTQ

Rules for the conversion of infix to prefix expression:

19
● First, reverse the infix expression given in the problem.
● Scan the expression from left to right.
● Whenever the operands arrive, print them.
● If the operator arrives and the stack is found to be empty, then simply push the operator into
the stack.
● If the incoming operator has higher precedence than the TOP of the stack, push the incoming
operator into the stack.
● If the incoming operator has the same precedence with a TOP of the stack, push the incoming
operator into the stack.
● If the incoming operator has lower precedence than the TOP of the stack, pop, and print the
top of the stack. Test the incoming operator against the top of the stack again and pop the
operator from the stack till it finds the operator of a lower precedence or same precedence.
● If the incoming operator has the same precedence with the top of the stack and the incoming
operator is ^, then pop the top of the stack till the condition is true. If the condition is not true,
push the ^ operator.
● When we reach the end of the expression, pop, and print all the operators from the top of the
stack.
● If the operator is ')', then push it into the stack.
● If the operator is '(', then pop all the operators from the stack till it finds ) opening bracket in
the stack.
● If the top of the stack is ')', push the operator on the stack.
● At the end, reverse the output.

Postfix Notation
This notation style is known as Reversed Polish Notation. In this notation style, the operator is
postfixed to the operands i.e., the operator is written after the operands. For example, ab+. This is
equivalent to its infix notation a + b.

Infix Expression Prefix Expression Postfix Expression

(A + B) * C *+ABC AB+C*
Some additional examples of infix expressions and the equivalent prefix and postfix expressions.

Infix Expression Prefix Expression Postfix Expression

A+B*C+D ++A*BCD ABC*+D+

(A + B) * (C + D) *+AB+CD AB+CD+*

20
A*B+C*D +*AB*CD AB*CD*+

A+B+C+D +++ABCD AB+C+D+

However, when the expression is written, the output of the desired expression remains the same.
Conversion of infix to postfix- algorithm
1. Scan the Infix string from left to right.
2. Initialize an empty stack and an output string for Postfix string.
3. If the scanned character is an operand, add it to the Postfix string.
4. If the scanned character is an operator and if the stack is empty push the character to stack.
5. If the scanned character is an Operator and the stack is not empty, compare the precedence of
the character with the element on top of the stack.
6. If top of Stack has higher precedence over the scanned character,
a. pop the stack
b. else push the scanned character to stack.
7. Repeat this step 6 until the stack is not empty and top Stack has precedence over the
character.
8. Repeat 4 and 5 steps till all the characters are scanned.
9. After all characters are scanned, we have to add any character that the stack may have to the
Postfix string.
10. If stack is not empty pop and add top of Stack to Postfix string.
11. Repeat this step as long as the stack is not empty.

21
5 Evaluation of Postfix Expressions

As Postfix expression is without parenthesis and can be evaluated as two operands and an operator
at a time, this becomes easier for the compiler and the computer to handle.

Evaluation rule of a Postfix Expression states:

1. While reading the expression from left to right, push the element in the stack if it is an
operand.
2. Pop the two operands from the stack, if the element is an operator and then evaluate it.
3. Push back the result of the evaluation. Repeat it till the end of the expression.

22
Example 3: 456*+

6 Searching

1. Sequential Search: In this, the list or array is traversed sequentially and every element
is checked. For example:Linear Search.
2. Interval Search: These algorithms are specifically designed for searching in sorted
data-structures. These type of searching algorithms are much more efficient than Linear
Search as they repeatedly target the center of the search structure and divide the search
space in half. For Example: Binary Search

23
6.1 Linear Search
A linear search scans one item at a time, without jumping to any item .

● The worst case complexity is O(n), sometimes known an O(n) search


● Time taken to search elements keep on increasing as the number of elements are increased.

ALGORITHM:
Step 1: start
Step 2: read the limit of the array
Step 3: read the elements a[i] into the array for i=0 to limit
Step 4: read the value to be searched
Step 5: for i =0 to limit , repeat steps 6 -7
Step 6: if searched value = a[i] then return element found at position i+1 and exit
Step 7: else, continue with i =i+1 and set flag=1 whenever searched element is found.
Step 8: if flag =0 print searched element not found

6.2 Binary Search

A binary search cut down your search to half as soon as you find middle of a sorted list.

● The middle element is looked to check if it is greater than or less than the value to be
searched.
● Accordingly, search is done to either half of the given list.

ALGORITHM:
Step 1: start
Step 2: read the limit of the array
Step 3: read the elements a[i] into the array for i=0 to limit
Step 4: read the value to be searched
Step 5: set first = 0, last = n – 1, middle = (first + last)/2

24
Step 6: while first <= last
Step 7: if array[middle] < search then first = middle + 1;
Step 8: else if array[middle] == search
Step 9: print searched element found at location middle+1 and exit
Step 10: else last = middle – 1
Step 11: middle = (first + last)/2
Step 12: if first > last then print searched element not found
Step 13: stop

Important Differences

● Input data needs to be sorted in Binary Search and not in Linear Search
● Linear search does the sequential access whereas Binary search access data randomly.
● Time complexity of linear search -O(n) , Binary search has time complexity O(log n).
● Linear search performs equality comparisons and Binary search performs ordering
comparisons

25

You might also like