Вы находитесь на странице: 1из 11

Subject: Algorithms Subject Code: BSIT 41 Assignment: TB

1. Hand simulate the sequential search algorithm on the data set given below and the search elements being 10, 05, 76, 85, 200 Data set 10, 80, 0!, 0", 55, !2, 100, 07, 05, 02, 12, 65, 6!, 22, "2, 81, #8, 76 $ns % &n sequential search, we start to search 'rom the beginning o' the list and e(amine each element till the end o' the list, i' the desired element is 'ound we sto) the search and return the inde( o' that element. &' the item is not 'ound and the list is e(ecuted the search return a *ero value. +e have to 'ind the element 10,05,76,85,200 in the given list o' 18 elements. ,et us ta-e an inde( that contains ! records. .irst o' all we have to aseend the data. $'ter that we have to divide into three )arts each having 6 elements Dataset 10, 80, 6!, 0", 55, !2, 100, 07, 05,02, 12, 65, 6!, !!, "2, 81, #8, 76 in ascending order with inde(
&nde( 02 0 05 1 07 2 0" ! 10 # 12 5 22 6 !2 7 #8 8 55 " 6! 10 6! 11 65 12 76 1! 80 1# 81 15 "2 16 100 17

/he -e0s element have to searched 10, 05, 76, 85, 200 /he -e0 element 10 is smaller than 22 in the 02 22 65 05 !2 76 07 #8 80 0" 55 81 10 6! "2 12 6! 100 inde( there'ore we do not need to search the entire list. +e can sim)l0 search 'or the -e0 element in the sub list (1022.63. so the inde( o' 10 is 4#5 6imilarl0, 05, 76, will be 'ind. and 85, 200, we will not 'ind it them its list is e(hausted and the search return a *ero value.

2. Hand simulate the binar0 search technique on the dataset given in 1163 'or the same -e0s7 $ns % &n the 8inar0 search ,the a))ro(imate mid entr0 is located and its -e0 value is e(amined . &t the mid value is greater than (, than the list is cho))ed o'' at the 1mid %13th location now the list gets reduced to hal' the original list. /he -ee)s on the other hand i' the mid value is larger than (, the list is cho))ed o'' at 1mid913th location. &t will continue till desired -e0 is not 'ound.

Dataset 10 80 6! 0" 55 !2 100 07 05 02 12 65 6! 22 "2 81 #8 76 6tart as an ascending order with inde( 02 0 05 1 07 2 0" ! 10 # 12 5 22 6 !2 7 #8 8 55 " 6! 10 6! 11 65 12 76 1! 76 1# 80 15 81 16 "2 17

/he -e0 elements have to be searched 10,05,76,85,200 let:s search 10; i.3 Divide the list into two equal values and have to divide the list, we need to obtain the inde( o' the middlemost element in the list. +e can determine the inde( o' the middlemost element with the hel) o' the 'ollowing 'ormula % <id=1,ower 8ound 9 >))er 8ound3?2 there'ore, <id= 10917?23 = 81a))ro(.3 0 02 1 05 2 07 ! 0" # 10 5 12 6 22 7 !2 8 #8 " 55 10 6! 11 6! 12 65 1! 76 1# 80 15 81 16 "2 17 100

<iddle @lement ii.3 /he element at inde( 8 is #8, which is greater than 10, there'ore we will search the element towards the le't o' the middle element, i.e in the list arr405 to arr475 here ,8 is 0 and >8 is equal to mid%1 %

0 02

1 05

2 07

3 09

4 10

5 12

6 22

7 32

<iddle @lement iii.3 $gain divide the list into two values <id = 0 9 7 ? 2 = !1a))ro(.3 there'ore, the middle element at inde( ! /he element at inde( ! is smaller than 10. 6o, we will search the element right o' the middle element. i.e. here ,8 is 0 and >8 is equal to mid%1

0 10

1 12
<.@

2 22

3 32

<id = 09! ? 2 = 1

/he middle element is at 1 and the element at inde( 1 is greater than 10 6o, we have onl0 one element le't at inde( 0 so, &t is the desired element. 6imilarl0 % 05, 76 will be 'ind, and 85, 200, we will not able to 'ind it them its list is e(hausted and search return a *ero value.

3. Hand simulate the insertion sort algorithm 'or the 'ollowing data set 10, #5, 80, 0!, !", 0", 55, !2, #!, 02, 07, 01, 10, 15. $ns /he insertion sort algorithm devised the list into two )arts sorted and insorted. &nitiall0 the sorted )art contains onl0 one element. &n each case, one element 'rom the unsorted list is inserted at its correct )osition in the sorted list. $s a result, the sorted list grows b0 one element and the unsorted list shrin-s b0 one elements in each )ass. +e have a data set let:s sort 10 #5 80 0! !" 0" 55 !2 #! 02 07 01 01 15 0 1 2 ! A >nsorted list 13 &n )ass1, ta-e the 'irst element 10, 'rom the unsorted list, and store, it at its correct )osition in the sorted list. # 5 6 7 8 " 10 11 12 1!

10 0

45 1

6orted list 23 &n )ass2, ta-e the 'irst element 1803 'rom

80 03 39 09 55 32 43 02 07 01 10 15 2 3 4 5 6 7 8 9 10 11 12 13
>nsorted list !3 &n )ass!, ta-e the 'irst element 0!, 'rom the unsorted list, and store, it at its correct )osition in the sorted list.

03 0
list.

10 2

45 3

80

Similarly, )ass1, )ass2 and )ass! it -ee)s on going, and lastl0 we have sorted 1 0 02 03 07 09 10 10 15 32 39 43 45 55 80 1 2 3 4 5 6 7 8 9 10 11 12 13

#.

Hand simulate the selection sort algorithm 'or the dataset given in 1183

$ns 6election 6ort involves multi)le )asses through the list o' elements. &n )ass1, we locate the smallest elements 'rom the list and swa) it with the element at the 'irst )osition in the list. $t the end o' )ass1, the smallest value will be )laced at its correct )osition in the list. +e have a dataset to sort 'rom selection sort % &nde( 10, #5, 80, 0!, !", 0", 55, !2, #!, 02, 07, 01, 10, 15 0 1 2 ! # 5 6 7 8 " 10 11 12 1! ,et:s 6ort % i. ,ocate the smallest element in the list, &n the given list , arr4115 is the smallest elements. ii. 6wa) the smallest element with the element at inde( 0 in the arra0. /he resultant arra0.

6+$B
01 0 #5 1 80 2 0! ! !" # 0" 5 55 6 !2 7 #! 8 02 " 07 10 10 11 10 12 15 1!

iii. iv.

,ocate the ne(t smallest element in the list, hence arr4"5 is the ne(t smallest element. 6wa) the smallest element with the element at inde( in the arra0, as li-e
01 02 0 80 1 2 0! ! !" # 0" 5 55 6 !2 7 #! #5 8 07 " 10 10 11 10 12

v.

$gain and again locate the smallest one and swa) them, it -ee)s on going and last we will have a sorted list.
01 0

02
1

0! 2

07 !

0" #

10 5

10 6

15 7

!2 8

39
"

#! 10

#5 11

55 12

80 1!

l0

5.Cet to -now about 'ew more sorting algorithms and )re)are a brie' note on them.

$ns % /here are various sorting techniques, such as bubble sort, quic- sort, and shell sort. @ach o' these sorting techniques is de'ined as 'ollows

Bubble sort &n the bubble sort technique two elements are com)ared at a time and i' the two elements are not in ascending order these elements are interchanged. /his )rocess is re)eatedl0 )er'ormed throughout the given list until the list is com)letel0 sorted. /o sort a list o' n elements using bubble sort 0ou need to ma-e a total o' 1n%132 com)arisons. Quick sort /he basic idea underl0ing quic- sort is to allow a s)eci'ic element DaD within the list D(D to 'ind its )ro)er )osition DED. /he )ro)er )osition DED is 'ound such that it satis'ies the 'ollowing two conditions /he elements on the le't hand side o' )osition DED are all smaller than or equal to DaD /he elements on the right hand side o' )osition DED are all greater than or equal to DaD &' DaD satis'ies these two conditions, then DaD is the Eth smallest element in the list and DaD is )laced at Eth )osition in the 'inall0 sorted list. /his )rocess is then re)eated 'or sub arra0s ( 40..E%15 and ( 4E91..n%15. Shell sort &n shell sort, the given list ( is divided into sub lists containing ever0 -th element o' the given list. .or e(am)le, i' -=5 then one sub list contains ( 405, ( 455, ( 4105..., another sub list contains ( 415, ( 465, ( 4115..., and so on. /he elements o' these sub lists are then com)ared two at a time and i' the two elements are not in ascending order, these elements are interchanged. Fow, a new value o' - is chosen which is smaller than the )revious value o' and the )rocess is re)eated again. /his )rocess is re)eated until the value o' is set to 1 so that the sub list consisting o' the entire list is sorted.

6. +hat is recursion7 +hich data structure is used 'or recursion7 $ns % Gecursion is an o''shoot o' the conce)t o' sub)rograms. $ sub)rogram as we -now is the conce)t o' writing se)arate modules, which can be called 'rom other )oints in the )rogram or other )rograms. /hen came a conce)t where in an0 sub)rogram can call an0 other sub)rogram. /he control goes to the called sub)rogram, )er'orms the assigned tas- and comes bac- to the caller )rograms. 6tac- data structure is used 'or recursion.

7. +hat are the merits and demerits o' recursion. $ns %

<erits o' recursion are <athematical 'unctions, such as .ibonacci series generation can be easil0 im)lemented using recursion as com)ared to iteration technique. Demerits o' recursion are <an0 )rogramming languages do not su))ort recursion; hence, recursive mathematical 'unction is im)lemented using iterative methods. @ven though mathematical 'unctions can be easil0 im)lemented using recursion, it is alwa0s at the cost o' e(ecution time and memor0 s)ace. /he recursive )rograms ta-e considerabl0 more storage and ta-e more time during )rocessing.

10. Hand simulate merge sort algorithm to sort a given list o' data elements.7 $ns % &t is also li-e as quic- sort algorithm, it uses the divide and conquer a))roach to sort the list. >sing this algorithm the list to be sorted is divide into two sublists o' si*es as newl0 equal as )ossible, and then the tow sublists are sorted se)aratel0 b0 using merge sort. /he two sorted sublists are then merged into a single sorted list. +e have a dataset 0 $rra0 % 1 5! 2 10 ! !0 # 76 5 ! 6 57 2#

.irst s)lit the list into )arts 'irstl0 we divide the list into two equal halves. &' the list has odd number o' equal elements, then the le't sublist is longer than the right. 6ublist b0 one entr0 as shown in the 'ollowing 'igure. # ! 5 6 57 2# $rra0

0 1 2 ! 5! 10 !0 76 /he two sublists are 'urther divided into sublists 0 1 5! 10 2 ! !0 76 # 5 ! 57

6 2# /he sublists obtained are divided.

0 5!

1 10

2 !0

! 76

# !

5 57

6 2#

Fow, +e nee to start merging, /he sublists45!5 and 4105 are merged to 'orm the sorted list 410,5!5 similarl0. 0 1 5! 10 2 ! !0 76 # 5 ! 57 6 2#

Fow, the 'irst two sublists are merged to 'orm a 'our element sorted list, and remerging also. 0 1 2 ! 5! 10 !0 76 # ! 5 6 57 2#

/hen these sorted lists are merged to 'orm a com)lete sorted list. 0 1 2 ! 5! 10 !0 76 # ! 5 57 5 2#

&t is a sorted list. 11. $ns % Hand simulate the recursive binar0 search algorithm.7 &n the recursive binar0 search, here is also the same logic o' binar0 search that the mid value is greater than ( ,then the list is cho))ed o'' at the 1mid%13th location . Fow the list gets reduced to hal' the original list . /he middle entr0 o' the le't H reduced list is e(amined in a similar manner. +e have a dataset 10 80 6! 0" 55 !2 100

6ort as an ascending order. 0" 1 10 2 !2 ! 55 # 6! 5 80 6 100 7

6u))ose, the -e0 element have to searched 10 ,et:s search 10 % Divide the list into two equal halves. /o divide the list, we need to obtain the inde( o' the middle most element in the list. +e can determine the inde( o' the middle most element with the hel) o' the 'ollowing 'ormula.

<id = 1,ower bound 9 >))er bound3 ? 2 <id = 10963 ? 2 =! 0" 0 10 1 !2 2 55 ! 6! # 80 5 100 6

<iddle element. /he element at inde( ! is 55, +hich is greater than 10, then we will not search the element towards, but we call recursivel0 the same algorithm, li-e to )ass , 'our )arameter. $ = Dataset I = 6earch element High = High value <id = mid value $nd this -ee):s on going , until a4mid5 J I and lastl0 we will have 'ound the desire element.

12

+hat is binar0 tree7 +rite a note on its )ro)erties.7 $ binar0 tree is made u) o' nodes where each node consists o' three elements, le't node, right node and a data element. /he root node is the to)most node o' the binar0 tree. &' there I1root3 = 0 &' im)lies that there are no children. $ node without an0 children is called a lea' node. /he number o' levels in the tree is called the Kde)th: o' the tree. &t has three t0)e. 13 $ com)lete binar0 tree. 23 $ 'ull binar0 tree. !3 6trictl0 binar0 tree. &n the le't )receding binar0 tree, node 8 the is the le't child o' node $, and node L is the right child o' node $. 6imilarl0 node D is the le't child o' node 8 and node @ is the right child o' node 8. /here are some variant o' binar0 tree with some additional characteristics. 6trictl0 binar0 tree, $ binar0 tree is said to be strictl0 binar0 i' ever0 node ,e(ce)t 'or the lea' nodes, have non%em)t0 le't and right children. .ull binar0 tree % $ binar0 tree o' de)th o' Kd: is said to be a 'ull0 binar0 tree i' it has e(actl0 2d%1 nodes. Lom)lete binar0 tree % $ binar0 tree with Kn: nodes and de)th Kd: is com)lete i' and onl0 i' its nodes corres)ond to the nodes numbered 'rom 0 to n%1 in the 'ull binar0 tree o' de)th KI:. <a(imum number o' vertices in a binar0 tree o' de)th Kl: is M 20 9 21 9 2 2 i.e 9 2 !% % % % %2#

$ns %

N2 - = e l91 % 1

0O-Ol 1! Di''erentiate between static allocation and d0namic allocation7

$ns % Di''erence between static and d0namic allocation are as 'ollows % 6tatic allocation means that the memor0 allocation 'or re)resenting the binar0 tree using 1D or 2D arra0 is made staticall0 at com)ile time. D0namic %allocation means that the memor0 allocation 'or re)resenting the binar0 tree using lin-ed list is made d0namicall0 at run time.

1# $ns %

Lomment on the s)ace utili*ation in various re)resentation schemes o' a binar0 tree. a.3 .or a &D arra0, the 'ormula 'or calculation )ercentage o' memor0 utili*ation is 1F%1?2#91%13P 100 where n re)resents the number o' nodes and ? re)resents the de)th o' the tree .However, in the given question, the de)th has not s)eci'ied, /here'ore the )ercentage memor0 utili*ation cannot be calculated, b.3 .or a 2D arra0, the )ercentage o' memor0 allocation is 1F%1?n23P100 = 116%1?1623P100 =5.86Q /here'ore, the wastage o' memor0 in 2D arra0 is 100% 5.86="#.1#Q

c.3 .or a lin-ed list, the )ercentage o' memor0 utili*ation is 1F%1?2n3P100= 116%1?2P163P100 % #6.88Q /here'ore, the wastage o' memor0 in 1D is 100%#6.88=5!.1!Q

15. Lonstruct all binar0 tree o' level 5 and traverse them in inorder, )reocder and )ost order and )roduce it the corres)onding sequence7 $ns. &n .ig 1 /he )reorder traversal sequence 'or the binar0 tree is $8DHI<F,@L.&RC /he inorder notation 'or the above binar0 tree <IFH,D8@$&.RLC /he )ostorder notation 'or the above binar0 tree is <FI,HD@8&R.C&$ &n .ig .2 /he inorder notation 'or the above binar0 tree is &$,BD8@<S&$RFG.LCT6I /he )reorder traversal sequence 'or the binar0 tree is

B,HDS<&@8GFR@8GFR.6TICL$ &n .ig .! /he inorder notation 'or the above binar0 tree is HD6T&B8R@I$,.S<G/LCF /he )reorder traversal sequence 'or the binar0 tree is $8DH&T6B@RIL.,<SG/CF /he )ostorder traversal sequence 'or the binar0 tree is H6TB&DRI@8,S/G<.FCL$ &n .ig.# /he inorder notation 'or the above binar0 tree is &8FCRC@H$.I<&,L &n .ig.5 /he )reorder traversal sequence 'or the binar0 tree is $8D@CRF$L.&I<, /he )ostorder traversal sequence 'or the binar0 tree is DFRCH@8<I,&.L$ &n .ig.6 /he )reorder traversal sequence 'or the binar0 tree is $8DHI,F@&L.CR<T /he inorder traversal sequence 'or the binar0 tree is DIHF,8@&$.L<TRC /he )reorder traversal sequence 'or the binar0 tree is IF,HD&@8.T<RCL$ 16. Design a conventional iterative algorithm to traverse a binar0 tree re)resented in one dimensional arra0 in inorder. $ns % $lgorithm &norder traversal &n)ut $ 4 5, one dimensional arra0 re)resenting the binar0 tree 13 /he root address ?? initiall0 i=1 Tut)ut &norder sequence <ethod &' 1$ 4i5M =03 &norder /raversal 1$, 2i3 Dis)la0 1$4i53 &norder /raversal 1$, 2i913 &' end $lgorithm ends.

17. Design a conventional iterative algorithm to traverse a binar0 tree re)resented in one dimensional arra0 in )reorder

$ns

$lgorithm )reorder traversal &n)ut $ 4 5, one dimensional arra0 re)resenting the binar0 tree 23 /he root address ?? initiall0 i=1 Tut)ut )reorder De'erence <ethod &' 1$ 4i5M =03 Dis)la0 1$4i53 Breorder /raversal 1$, 2i3 Breorder /raversal 1$, 2i913 &' end $lgorithm ends.

Вам также может понравиться