1.linsearch: Linear Search
1.linsearch: Linear Search
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
2. List the steps by which a queue can be implemented using two stacks.
5.Solve the following recurrence and find the order of its complexity.
6. (a) If f1(n) is in O(g1(n)) and f2(n) is in O(g2(n)), then f1(n) + f2(n) is in _________________.
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++;
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.
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.
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<<" "; }
}
a)