0% found this document useful (0 votes)
32 views

1.linsearch: Linear Search

The document outlines tasks for Phase 1 of a project including: 1) Implementing linear and binary search on arrays, sorting algorithms, and linked lists. 2) Implementing stacks and queues using arrays and linked lists. 3) Proving time complexities of algorithms and solving recurrences. 4) Finding time complexities of functions.

Uploaded by

Gamora Thanos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

1.linsearch: Linear Search

The document outlines tasks for Phase 1 of a project including: 1) Implementing linear and binary search on arrays, sorting algorithms, and linked lists. 2) Implementing stacks and queues using arrays and linked lists. 3) Proving time complexities of algorithms and solving recurrences. 4) Finding time complexities of functions.

Uploaded by

Gamora Thanos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Project –Phase 1

PART 1-

1.LINSEARCH
Given an array of ​n ​integers, write a program to search whether a particular integer, say ​k​, is
present in the array. If present, return the index of the same. Otherwise, return -1. ​Use
linear search​.
2 ​BINSEARCH

Given a sorted array of ​n ​integers, write a program to search whether a particular integer,
say ​k,​ is present in the array. If present, return the index of the same. Otherwise, return -1.
Use binary search.

3. ​SORT

Implement the
i) insertion sort ​algorithm,
ii) merge sort
iii) Quick sort
to sort ​n i​ ntegers​, n should be greater than 100.
Find the time for sorting 100, 1000 and 10000 numbers

4. Implement a singly linked list and doubly linked list and perform the operations given in
the sample code.

5.
STACK
a) Implement a stack using an array.
b) Implement a stack using a linked list.
Your program must support the following functions:
i).​push(stk, element) ​– puts the data specified by element on top of the stack specified by
stk.
ii).​pop(stk) ​– removes and returns the topmost element of the stack specified by stk. Return
null (or some special value), if the stack is empty.
iii)​peek(stk) ​– returns the topmost element of the stack specified by stk, without actually
removing the element from the stack. Return null (or some special value), if the stack is
empty.
iv)​show(stk) ​– displays all the data present in the stack specified by stk.

6
QUEUE
a) Implement a queue using an array.
b) Implement a queue using a linked list.
Your program must support the following functions:

1
Project –Phase 1

i)​enqueue(q, element) ​– puts the data specified by element at the rear end of the queue
specified by q.
ii) ​dequeue(q) ​– removes and returns the element at the front of the queue specified by q.
Return null (or some special value), if the queue is empty.
iii)​peek(q) ​– returns the element at the front of the queue specified by q, without actually
removing the element from the queue. Return null (or some special value), if the queue is
empty.
iv)​show(q) ​– displays all the data present in the queue

Part 2

1. Prove that -n​2​/2 –n/2 = Θ(n​2​)

2. List the steps by which a queue can be implemented using two stacks.

3. Solve the following recurrence and find its complexity


T(n) =1 if n=1
=T(n-1) +n(n-1) if n>=2
4. What is meant by ‘randomised ‘ quick sort ? What is its complexity ,and explain how you
arrive at it.

5.Solve the following recurrence and find the order of its complexity.

T(n) = 2T(n/2) + n for n>1


=1 for n=1

6. (a) If f​1​(n) is in O(g​1​(n)) and f​2​(n) is in O(g​2​(n)), then f​1​(n) + f​2​(n) is in _________________.

(b) If f​1​(n) is in O(g​1​(n)) and f​2​(n) is in O(g​2​(n)), then f​1​(n)f​2​(n) is in _________________

7. Find Theta complexity of the code snippet below.

sum1 = 0;
for (k=1; k<=n; k*=2)
for (j=1; j<=n; j++) sum1++;
sum2 = 0;
for (k=1; k<=n; k*=2)
for (j=1; j<=k; j++) sum2++;

8.​ Find the complexity of the below recurrence:


T (n) = {2T (n − 1) − 1, if n > 0 1, otherwise

2
Project –Phase 1

9. . Consider the following function that takes reference to head of a Doubly Linked List as
parameter. Assume that a node of doubly linked list has previous pointer as ​prev​ ​and next
pointer as ​next​.

void fun(struct node ** head_ref)


{
struct node *temp = NULL;
struct node *current = *head_ref;
while (current != NULL)
{
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if(temp != NULL )
*head_ref = temp->prev;
}
​Assume that reference of head of following doubly linked list is passed to above function
1​↔​2​↔​3​↔​4​↔​5​↔​6.​ What should be the modified linked list after the function call?

10. What is the output of following function for start pointing to first node of following
linked list? ​10→20→30→40→50→60. ​Assume that a node of linked list has two fields
1.integer as ​data ​and​ ​2. next pointer as​ next.

void fun(struct node* start)


{
if(start == NULL)
return;
cout<< start->data;
if(start->next != NULL)
fun(start->next->next);
cout<< start->data;
}

10.​ Find the complexity of the function.

3
Project –Phase 1

function(int n) {
for(int i=0; i< n; i++)
for(int j=i; j< i*i; j++)
If( j%i==0 ) {
for( int k=0; k< j; k++)
cout<<" "; }
}

11. ​What does the following function do for a given singly


Linked List with the first node as ​head? Linked list is
g→f→e→d→c→b→a

void fun1(struct node* head)


{ ​ if(head == NULL)
return;
fun1(head->next);
cout<<head->data<<" ";
}

12.​ .​ With the help of Master’s theorem, solve


(a)

a)

b) T(n)=2T (n/4) + n​0.51


You might also like