You are on page 1of 10

ASSIGNMENT-3

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

Ans Algorithm: QUICK_SORT (K, LB, UB)


- K is a vector containing N elements
- LB and UB denotes the lower and upper bound of the table
- FLAG is a logical variable
- KEY is a key value which is being placed at its final position after each pass
Step 1. [initialize]
FLAG  true
Step 2. [perform sort]
If LB < UB
Then I  LB
J  UB +1
KEY  K[LB]
Repeat while FLAG
II+1
Repeat while K[I] < KEY
I=I+1
JJ–1
Repeat while K[J] > KEY
J=J–1
If I < J
Then K[I] K[J]
Else FLAG  false
K[LB]  K[J]
Call QUICK_SORT (K, LB, J-1)
Call QUICK_SORT (K, J+1, UB)
Step 3. [finished]
Return
Tracing of quick sort :-

K1 K2 K3 K4 K5 K6 K7 K8 K9 K10

{42 23 74 11 65 58 94 36 99 87}

{11 23 36} 42 {65 58 94 74 99 87}

11 23 36} 42 {65 58 94 74 99 87}

11 23 {36} 42 {65 58 94 74 99 87}

11 23 36 42 {58} 65 {94 74 99 87}

11 23 36 42 58 65 {94 74 99 87}

11 23 36 42 58 65 {87 74} 94 {99}

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]
i0
j0
k0
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

else if (list1[i] > list2[j])

then list3[k]  list2[j]


j  j+1
k  k+1
else //if elements of both lists are same
list3[k]  list1[i]
i  i+1
k  k+1
step4. [size of list1 is greater than list2]
if (I < n)

then repeat for x = i, i+1,….., n-1

list3[k]  list1[x]

k  k+1

step5. [size of list2 is greater than list1]


else if (j<m)
then repeat for y =j, j+1,….., m-1
list3[k]  list2[j]

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

Step1. [perform sort]


Repeat thru step 4 for j = 1,2,…, M
Step2. [initialize the pass]
Repeat for i = 0,1,..,9
T[i]  B[i]  NULL
R  FIRST
Step3. [distribute each record in the appropriate pocket]
Repeat while R != NULL
D  bj
NEXT  LINK (R)
If T[D] = NULL
Then T[D]  B[D]  R
Else LINK (T[D]  R
T[D]  R
LINK(R)  NULL
R  NEXT
Step4. [combine pocket]
P0
Repeat while B[P] = NULL
PP+1
FIRST  B[P]
Repeat for i = P+1, P+2,…, 9
PREV  T[i-1]
If T[i] != NULL
Then LINK (PREV)  B[i]
Else T[i]  PREV
Step5. [finished]
Return
Tracing of Radix sort

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

Ans There are two steps in heap sorting


1. Create Heap
2. Sort the elements from Heap

- Now Doing the first step ( Creating Heap tree )


- While create heap, take one thing in mind that the value of the root node is always maximum
among all the nodes in the tree. (OR value of the parent node is always greater than its children
nodes)
- Take element one by one and adjust in the tree at appropriate place as described above and we
get heap tree like:

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

Working of Binary search :-


 This method is used to search an element in an ordered list.
 It is very efficient method to search element.
 For this method, it’s necessary to have list in ordered (sorted) manner.
 Let low represents lower limit of the list and high represents the higher limit.
 First we calculate the value of middle as :
Mid= |(Low + High) / 2|

Low Middle High

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

HASHING FUNCTIONS OR HASH TABLE METHODS


- A function that transforms a key into table index is called ‘Hash function’
- If H is a hash function and m is a key then H(m) is called the Hash of the key m and is the record
where the element to be placed
There are different Hash tables methods available to build various Hash functions, such are:

(1) The Division Method


- This method is simplest among all methods
- In this approach, the key is divided by an appropriate number and then to use the remainder of
H(k) = k mod m
For ex : H (35) = 35 mod 11 = 2
The division method generates the key value belongs to the set {1,2,.., m)
- For the best result m should be prime number, so we can minimize the chances of collisions
(2) Mid Square Method
- In this method key is multiplied by itself and the address is obtained by selecting an appropriate
number of digits from the middle of the square
For ex : consider a six digit key 123456. If we square it, it gives 15241383936. If a 3-digit address is
required, position 5 to 7 could be chosen, giving address 138.
(3) Folding Method
- In a folding method, the key is partitioned into a number of parts, each part has equal length. The
parts are then added together, ignoring the final carry, to form the address.
For ex: the key 356942781 is to be transformed into a three-digit address. So the parts are 356,
948, 781 are added together and the result will be 079 (final carry 1 is ignored)
- The disadvantage of this method is that, if two different keys have the same value then Hash
function has the same value, so the collision occurs.
(4) Multiplicative Hashing Method
- Disadvantage of the folding method can be avoided by this method
- This method is a simple variation of the mid-square method. In this method instead of multiplying
the key by itself, we multiply the key by a constant i, such that 0 < i <1, the function is
H(k) = [m(ik mod 1)] + 1
Here ik mod 1 is the fractional part of ik and denotes the greatest integer less than or equal to its
content.
Q.8 Explain collision resolution technique.

Ans Collision Resolution techniques


- When a hashing function maps several keys into same address space, it is known as collision.
- At the time of collision, an element is already there in the table, so inserting that element into the
table should be failed.
- To avoid such situations, some techniques are used, which known as collision resolution
techniques.
- Some popular collisions resolution techniques are as follows:
A) Open Addressing OR Linear Probing
B) Quadratic Probing
C) Random Probing
D) Re-Hashing (Double Hashing)
E) Chaining
A) Open Addressing OR Linear Probing
- This is the simplest method of Re-Hashing
- This technique finds the open slots for colliding records.
- When collision occurs, look in the neighboring slots I the table, if slot is empty, it calculates the
new address extremely quickly.
Ex – modern RICS processor
- This technique doing sequential (linear) search for finding new address (empty location), so called
Linear Probing.
- Major drawback of these methods is that, as table becomes half full, then there is a tendency
towards clustering, creating instability.
B) Quadratic Probing
- If there is a collision at Hash address h, this method probes the table location h+1, h+4, h+9,,, i.e
h+i2
- This method reducing clustering
C) Random Probing
- The effect of clustering can be reduced or minimize using this technique.
- This method generates a random sequence of positions rather than an ordered sequence as in
case of linear probing.
D) Re-Hashing (Double Hashing)
- If there is a collision, we re-hash until an empty slot or address in the table is found.
- The re-hashing function can either be a new funtion or a re-application of the original one.
- Using this method - it must not be the same as the primary hash function
E) Chaining
- In this method, keeping a distinct linked list for all records whosse keys hash into a particular
value.
- This allows unlimited number of collisions to be handled and dosen’t require a prior knowledge
about the colliding records.
- In open addressing, collisions are resolved by looking for an open cell in the table, while in
chaining a linked list is installed at each index into the hash table

You might also like