Professional Documents
Culture Documents
Q.1 Write an algorithm of Bubble Sort and explain its working Give trace of bubble sort algorithm
using following data 42 23 74 11 65 58 94 36 99 87
Ans Algorithm: BUBBLE_SORT (K, N)
- K is a vector of N elements
- PASS denotes the pass counter and LAST denotes the last unsorted element
- I is a index variable
- EXCH is a variable counting number of exchanges made on any pass
Step 1. [initialize]
LAST N
Step 2. [loop on pass index]
Repeat thru step 5 for PASS = 1, 2, 3,.., N-1
Step 3. [initialize exchange counter ]
EXCH 0
Step 4. [perform pair wise comparison on unsorted elements]
Repeat for I = 1, 2, .., LAST-1
If K[I] > K[I+1]
Then EXCH EXCH +1
Step 5. [were exchanges made?]
If EXCH = 0
Then return (SORTING COMLETED)
Else LAST = LAST -1 (REDUCE SIZE OF UNSORTED LIST)
Step 6. [finished]
Return
Pass Numbers
Unsorted
elements 1 2 3 4 5 6
42 23 23 11 11 11 11
23 42 11 23 23 23 23
74 11 42 42 42 36 36
11 65 58 58 36 42 42
65 58 65 36 58 58 58
58 74 36 65 65 65 65
94 36 74 74 74 74 74
36 94 87 87 87 87 87
99 87 94 94 94 94 94
87 99 99 99 99 99 99
Q.2 Write an algorithm of quick sort. Give trace of Quick sort algorithm using following data
42,23,74,11,65,58,94,36,99,87
K1 K2 K3 K4 K5 K6 K7 K8 K9 K10
{42 23 74 11 65 58 94 36 99 87}
11 23 36 42 58 65 {94 74 99 87}
11 23 36 42 58 65 {74} 87 94 {99}
11 23 36 42 58 65 74 87 94 {99}
11 23 36 42 58 65 74 87 94 99
Q.3
Write and explain algorithm for simple merge sort and give suitable example.
Ans Algorithm: MERGE_SORT (list1,n, list2, m, list3)
This algo sorts two tables into third one (both tables are should be sorted initially)
List1 is the first list (table) of n elements
List2 is the second list (table) of m elements
List3 is the third list (table) where elements are to be sorted
Step1. [initialize]
i0
j0
k0
step2. [initialize loop]
repeat thru step 3 while ((i<n ) and (j<m))
step3. [comparing corresponding elements]
if (list1[i] < list2[j])
then list3[k] list1[i]
i i+1
k k+1
list3[k] list1[x]
k k+1
k k+1
step6. [finished]
return
Q.4 Write algorithm of Radix Sort and explain its working. Give trace of radix sort algorithm using
following data 42,23,74,17,65,57,94,36,99,87,70,81,64,222
Ans
Algorithm: RADIX SORT
- This sorting is used in digital computer
- It is also called bucket sorting method
- FIRST denotes address of the first node
- T denotes rear record for the pocket
- B denotes front record for the record
- LINK is a pointer field of
First Pass
042,023,074,017,065,057,094,036,099,087,070,081,064,222
64 87
222 94 57
70 81 42 23 74 65 36 17 99
Pocket: 0 1 2 3 4 5 6 7 8 9
Now collecting data in queue manner from pocket 0 to pocket 9
1. 070,081,042,222,023,074,094,064,065,036,017,057,087,099
Second Pass
23 65 74 87 99
17 222 36 42 57 64 70 81 94
Pocket: 0 1 2 3 4 5 6 7 8 9
2. 017,222,023.036,042,057,064,065,070,074,081,087,094,099
Third Pass
099
094
087
081
074
070
065
064
057
042
036
023
017 222
Pocket: 0 1 2 3 4 5 6 7 8 9
3. 17 23 36 42 57 64 65 70 74 81 87 94 99 222 (Sorted Data)
Explain working of Heap Sort (Tree Sort) using following data
Q.5
42 23 74 22 11 65 58 94 36 99 87
99
94 65
36 87 58
42
22 23 11 74
- Now doing the second step (sorting the elements from tree)
- In that, remove root node (highest) from tree and put into the array at last position
- Then, replace the root node by placing the node which is entered last in the tree
- Then, check for the value of the root node (as our rule – the value of the root node must be
greater than its children nodes)
- If not, swap the root node with its child node having maximum value and then check.
- Do the same process until we get the perfect tree
- And then, again remove the root (highest) from tree and put into the array at second last
position. Now do the above process in the tree and array.
- Finally, we will have an array that is sorted as per our requirement.
Q.6 List Search Methods. Explain the working of Binary search method with an example and also
write its algorithm.
Ans
There are two methods in searching :
i. Linear search
ii. Binary search method
1 2 3 4 5 6 7 8 9 10
5 8 10 15 20 21 24 29 32 35
Now we compare the value of the middle element with the element to be searched.
If the value of the middle element is greater than the element to be searched then the element
will exist in lower half of the list. So take high = mid – 1 and find value of the middle in this
new interval.
If the value of the middle element is smaller then the element to be searched then the element
will exist in upper half of the list. So take low = mid + 1 and find value of the middle in this
new interval.
This process is repeated until entire list is searched or the element is found.
Suppose we want to find the element 15 in the above list.
First, mid = (low + high ) / 2
= (1 + 10) / 2
=5
Here the value of middle of element is 318 which is greater than 15 so the element is in the
lower half of the list.
Take high = mid – 1 = 5 – 1 = 4
Now mid = (low + high ) / 2
= (1 + 4) / 2
=2
Here the value of middle of element is 151 which is smaller than 15 so the element is in the
lower half of the list.
Take high = mid + 1 = 2 + 1 = 3
Now mid = (low + high ) / 2
= (3 + 4) / 2
=3
Here the value of middle of element is 203 which is smaller than 15 so the element is in the
lower half of the list.
Take high = mid + 1 = 3 + 1 = 4
Now mid = (low + high ) / 2
= (4 + 4) / 2
=4
Here the value of the middle element is 15 which we want to find.
Algorithm: BINARY_SEARCH(k, N, X)
k is a vector consisting of N elements in the ascending order
Variables LOW, MID and HIGH denote the lower, middle and upper limit of search interval
If search is successful, this function returns the index of the serched element, 0 otherwise
X is the element is to be searched
Step1. [initialize]
LOW 1
HIGH N
Step2. [perform search]
Repeat thru step 4 while LOW <= HIGH
Step3. [obtain index of middle point value]
MID |(LOW + HIGH) / 2|
Step4. [compare]
If (X < k[MID])
then HIGH MID – 1
else if (X > k[MID])
then LOW MID + 1
else write (“SUCCESSFUL SEARCH”)
return (MID)
step5. [unsuccessful search]
write (“UNSUCCESSFUL SEARCH”)
return (0)
Q.7
Write short-note on Hash table, Hashing Methods
Ans HASH TABLES
- is a data structure whose search can be independent of the no. of entries of a table
- For this, the position of the particular entry in a table is determined by the key value for the entry,
this can be done by hashing function
- HASH FUNCTIONS – maps the key value of the item to a hash value and this hash value used as an
index into the hash table.
- So, hash function is also called key-to-address transformation