Professional Documents
Culture Documents
Q1. Hand simulate the sequential search algorithm on the data set given
below and the search element being 10, 05, 76, 85, 200
Data set:: 10, 80, 03, 09, 55, 32, 100, 07, 05, 02, 12, 65, 63, 22, 92, 81,
48,76
Ans1:
Search element: 10
Data set:: A[]=
80 03 09 55 32 100 07 05 02 12 65 63 22 92 81 48 76
10
↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
i=from subscript 1-18
n=no. of elements in the array A[].
The algorithm for sequential search is as follows:
Algorithm: Sequential search
Input: A, vector of n elements
K, search element
Output: j-index of K
Method: i=1
While (i<n)
{
if(A[i]=K)
{
write(“Search successful”);
write(K is at location i)
exit();
}
else
i++
if end
while end
Write(“Search unsuccessful”);
algorithm ends
Q2. Hand simulate the binary search technique on the given in (Q1) for
the keys.
Ans: Input: A vector of n elements.
Data set:: A[]=
10 80 03 09 55 32 100 07 05 02 12 65 63 22 92 81 48 76
↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Search element: 10
Technique to search: Binary search. For this method it is necessary to
have the vector in an alphabetical order or numerically increasing
order.
The algorithm for binary search is as follows:
Algorithm: binary search
Input: A, vector of n elements
K, search element
Output: low-index of K
Method: low=1 high=n
While(low<=high-1)
{
mid=(low+high)/2
if(K<a[mid])
high=mid
else
low=mid
if end
}
while end
if(K=A[low])
{
write(“search successful”)
write(K is at location low)
exit();
}
else
write(“search unsuccessful”)
if ends
algorithm ends
Note: So, before hand simulating we need to have it in a numerically
increasing order. So the new data set will look as given below.
0 0 0 0 0 1 1 2 3 4 5 6 6 7 8 8 9 10
2 3 5 7 9 0 2 2 2 5 5 3 5 6 0 1 2 0
10 45 80 03 39 09 55 32 43 02 07 01 10 15
i=14
10 45 15 03 39 09 55 32 43 02 07 01 10 80
i=13
10 01 15 03 39 09 55 32 43 02 07 45 10 80
i=12
10 01 15 03 39 09 10 32 43 02 07 45 55 80
i=11
10 01 15 03 39 09 10 32 07 02 43 45 55 80
i=10
10 01 15 03 02 09 10 32 07 39 43 45 55 80
i=9
10 01 15 03 02 09 10 07 32 39 43 45 55 80
i=8
01 10 15 03 02 09 10 07 32 39 43 45 55 80
i=8
01 02 15 03 10 09 10 07 32 39 43 45 55 80
i=7
01 02 03 15 10 09 10 07 32 39 43 45 55 80
i=6
01 02 03 07 10 09 10 15 32 39 43 45 55 80
i=5
01 02 03 07 09 10 10 15 32 39 43 45 45 80
i=4
Q4. Hand simulate the selection sort algorithm for the dataset given in
(Q3).
Ans4: Given dataset: 10, 45, 80, 03, 09, 55, 32, 43, 02, 07, 01, 10, 15
10 45 80 03 39 09 55 32 43 02 07 01 10 15
10 45 02 03 39 09 55 32 43 80 07 01 10 15
10 45 02 03 39 09 10 32 43 80 07 01 55 15
10 01 02 03 39 09 10 32 43 80 07 45 55 15
10 01 02 03 07 09 10 32 43 80 39 45 55 15
↓
10 01 02 03 07 09 32 43 80 39 45 55 15 ↓
01 10 02 03 07 09 32 43 15 39 45 55 80
↓
01 02 10 03 07 09
15 43 32 39 45 55 80
↓
01 02 03 10 07 09
15 32 43 39 45 55 80
↓
01 02 03 07 10 09 15 32 39 43 45 55 80
↓
01 02 03 07 09 10
15 32 39 43 45 55 80
↓
01 02 03 07 09 10 10 15 32 39 43 45 55 80
Selection Sort
Q5. Get to know abut few more sorting algorithm and prepare a brief
not on them.
Ans5:
Bubble Sort: Bubble sort is one of the most popular sorting methods.
Bubble sort can be viewed as a selection sort because this method is also
based on successively selecting the smallest element, second smallest
element, etc as in the case of any other selection sort. In order to find the
successive smallest element, the method relies heavily on the exchange of
adjacent elements. Because of this, the method is often classed as “Sorting
by exchange”.
Heap Sort: Heap is a data structure which can be used to support the
operations defined for a priority queue in a different way. Heaps can be used
efficiently to sort a list of elements. Heaps provide an elegant way to sort an
array of words. On one hand, heap sort does not use any additional array for
intermediate use.
Shuttle Sort: When there are “n” elements in a given array, ”A”, shuttle sort
requires (n-1) passes. By the time the i-th pass (1<i<n-1) begins, the first “I”
elements i.e., elements A[0] to A[i] have been sorted and these occupy the
first “I” positions of the array. To insert the (i+1) –th element, A[i], during
the i-th pass, A[i] is compared with A[i-1] and if the key of the former is
found to be smaller than the two elements are exchanged. If an exchange
takes place then A[i-1] and A[i-2] are compared if necessary, they are
exchanged . The process continues until either no exchange is found
necessary or the beginning of the array is reached.
Shell Sort: This internal sorting technique is due to Donald L. Shell (1959).
This method makes repeated use of straight insertion sort or shuttle sort.
Shell sort operating on an array of “n” elements requires that in each pass, a
number called increment be chosen. The increment must be less than “n”;
they should be progressively smaller and the increment for the last pass must
be equal to “1”.
Demerits of recursion:
1) Many programming languages do not support recursion; hence
recursive mathematical function is implemented using iterative
methods.
2) Even though mathematical functions can be easily implemented using
recursion it is always at the cost of execution time and memory space.
3) A recursive procedure can be called from within or outside itself and
to ensure its proper functioning it has to save in order the return
addresses so that, a return to the proper location will result when the
return to a calling statement is made.
4) The recursive programs needs considerably more storage and will take
more time.
Max =a(P)
Min = a(q)
Else
If ( p- q -1) Then
Max =a(p)
Min =a(q)
Else
Max = a(q)
Min = a(p)
If end
Else
m- (p+q)/2
Max-min (p,m,max1,min1)
Max-min (m+1, Q, max2, min2)
If a(p) > a(q) Then // here a(p)=30 and a(q)=16.So a(p) is greater than a(q)
75 80 85 95 70 65 60 55 1000
Now consider the first element. We want to move this element 75 to its
correct position in the list. At the end of the operation, all elements 75 to the
left of 75 should be less than 75 and those to the right should be greater than
75.
Q10. Hand simulates the merge sort algorithm to sort a given list of data
elements.
Ans10: Sorting is a process of arranging a set of given numbers in some
order. The basic concept of merge sort is like this. Consider a series of
numbers, say A(1), A(2)……….A(n). Suppose individually sort the first set
and also the second set. To get final sorted list, we merge the two sets into
one common set.
We first look into the concept of arranging two individually sorted
series of numbers into a common series using an example:
K , search element
High, the upper limit //initially low=1 and high=n the number
of elements
Mid=(low+high)/2
If( a[mid] == K)
Retrun(mid)
Else
If (a[mid]<k)
Binary search (A, K Low, mid)
Else
Binary Search (A, K, High, mid)
If end
If end
Else
Return(0)
If end
Algorithm ends
Given dataset: 01 09 15 23 50
Search element: 15
Low=1 , high=5
Mid=(1+5)/2 //mid=6/2=3
If( a[mid] == K)
B C
D E F G
For a complete and full binary tree, there is 100% utilization and there is a
maximum wastage if the binary tree is right skewed or left skewed, where
only L+1 spaces are utilized out of the 2l+1-1 spaces possible.
Q15. Construct all binary tree of level 5 and traverse them in inorder,
preoprder and post order and produce the corresponding sequences.
Ans15:
Pre-order: In, this traversal, the nodes are visited in the order of root, left
child and then right child. The preorder traversal sequence for the binary tree
shown in figure is ABDHJLMKIECFG.
In-order: In, this traversal, the nodes are visited in the order of left child,
root and then right child. The preorder traversal sequence for the binary tree
shown in figure is LJMHKDIBEAFCG
Post-order: In, this traversal, the nodes are visited in the order of left child,
right child and then root. The preorder traversal sequence for the binary tree
shown in figure is LMJKHIDEBFGCA.
If( A[i] != 0
In order Traversal (A,2i)
Display (A[i])
In order Traversal (A,2i+1)
If end
Algorithm End