You are on page 1of 8

Assignment No 02 Q2:

Implement heap sort and provide the following. i. Pseudo code for the algorithm. ii. C/C++ Source code. iii. Time complexity analysis using Recursive equations. Details for programming section (part ii): Display list for each step.

A2(i)
************************************************************************* Algorithm Insert(S, L,D); Input: Sequence S that stores heap values, Data D to be inserted, Last value index L Output: Heap S storing inserted values If L is heap_size-1 then return Heap max indication LL+1; S[L]D; reHeapUp(S,L) Algorithm reHeapUp(S,L): Input: Sequence S that store heap values, Last value index L Output: Heap S storing inserted values Parent(L-1)/2 If (S[L]<S[Parent]) Hold= S[Parent]; S[Parent]=S[L]; S[L] = Hold; reHeapUp(S,Parent); ************************************************************************* Algorithm Delete(S,L,D): Input: Sequence S that have heap values, Last value index L, Root data D being removed Output: Heap S with root node removed If L < 0 then return Heap end indication DS[0]; LL-1; reHeapDown(S,0,L) Algorithm reheapDown(S,Root,L) Input: Sequence S that have heap values, Root index, Last value index L Output: Heap S containing deleted value

If(Root * 2 +1) <= L L_valS[root*2+1] If(Root *2+1) <=L R_val=S[root*2+2] Else R_val-1; If(L_val < R_val) Child_IndexRoot*2+1; Else Child_IndexRoot*2+2; If(S[Root] > S[Child_Index]) HoldS[Root]; S[Root]S[Child_Index] S[Child_Index]Hold reheapDown(S,Child_Index,L) **********************************************************************

A2(ii) Q2.cpp A2(iii) T(n) = T(n/2) + cn

Q3:
Two-way merge sort (we have studied in class) divides the problem into two halves, recursively sort each half and then combines using two-way merge algorithm to get sorted array. A four-way sort divides the array in four equal parts, sorts each half recursively and then combines using four-way merge algorithm to get sorted array. You have to implement four-way merge sort and provide the following. i. Pseudo code for the algorithm. ii. C/C++ Source code. iii. Time complexity analysis using Recursive equations. Details for programming section (part ii): Get unsorted list from user and display list at each step.

A3(i)
Algorithm Merge(S1, sizeS1,S2, sizeS2,S3, sizeS3,S4, sizeS4,S,sizeS): Input: Sequences of S1, S2, S3 and S4 sorted in non decreasing order, and empty sequence S Sizes of S1,S2,S3 and S4 Output: Sequence S containing the elements from S1,S2,S3 and S4 in non decreasing order While(S1size<S1tempsize or S2size<S2tempsize or S3size<S3tempsize or S4size<S4tempsize) do If (S1[S1tempsize] <= S2[S2tempsize] && S1[S1tempsize] <= S3[S3tempsize] && S1[S1tempsize] <= S4[S4tempsize] && S1tempsize<sizeS1) S[M] = S1[S1tempsize], S1tempsize++,M++ If (S2[S2tempsize] <= S1[S1tempsize] && S2[S2tempsize] <= S3[S3tempsize] && S2[S2tempsize] <= S4[S4tempsize] && S2tempsize<sizeS2) S[M] = S1[S1tempsize], S2tempsize++,M++ If (S3[S3tempsize] <= S1[S1tempsize] && S3[S3tempsize] <= S2[S2tempsize] && S3[S3tempsize] <= S4[S4tempsize] && S3tempsize<sizeS3) S[M] = S3[S3tempsize], S3tempsize++,M++ If (S4[S4tempsize] <= S1[S1tempsize] && S4[S4tempsize] <= S3[S3tempsize] && S4[S4tempsize] <= S1[S1tempsize] && S4tempsize<sizeS4) S[M] = S4[S4tempsize], S4tempsize++,M++ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (refer code) If(S1tempsize eq sizeS1 && S2tempsize eq sizeS2 && S3tempsize eq sizeS3) While (S4tempsize<sizeS4) S[M]=S4[S4tempsize], S4tempsize++,M++ {move remaining elements of S4 in S} If(S2tempsize eq sizeS2 && S1tempsize eq sizeS1 && S4tempsize eq sizeS4) While (S2tempsize<sizeS2) S[M]=S2[S2tempsize], S2tempsize++,M++ {move remaining elements of S2 in S}

If(S3tempsize eq sizeS3 && S2tempsize eq sizeS2 && S4tempsize eq sizeS4) While (S1tempsize<sizeS1) S[M]=S1[S1tempsize], S1tempsize++,M++ {move remaining elements of S1 in S} If(S4tempsize eq sizeS4 && S1tempsize eq sizeS1 && S2tempsize eq sizeS2) While (S3tempsize<sizeS3) S[M]=S3[S4tempsize], S3tempsize++,M++ {move remaining elements of S3 in S}

Algorithm Split(S,n) Input: Sequence S containing unordered elements, n containing size of S Output: Recursively splitting S into four equal parts S1Array[n/4] For (i<n/4;i++) S1[i]=S[i]; S2Array[2*n/4-n/4] For (i< 2*n/4;i++,j++) S1[j]=S[i]; S1Array[3*n/4-2*n/4] For (i< i<3*n/4;i++,k++) S1[k]=S[i]; S1Array[4*n/4-3*n/4] For (i< i<4*n/4;i++,l++) S1[l]=S[i]; Split(S1,n/4) Split(S2,(2*n/4-n/4)); Split (S3,(3*n/4-2*n/4)); Split (S4,(4*n/4-3*n/4)); Merge(S1,n/4,S2,(2*n/4-n/4),S3,(3*n/4-2*n/4),S4,(4*n/4-3*n/4),S,n) A3(ii) Q3.cpp A3(iii) T(n) = 4T(n/4) + cn

Q4:
Implement Binary search (as discussed in class) using array and tree and provide the following. i. Pseudo code for the algorithm. ii. C/C++ Source code. iii. Time complexity analysis using Recursive equations. Details for programming section (part ii): In addition to the search results, display the list.

Tree Data structure


Algorithm TreeSearch(k,v) Input: Search key k, and node v of a binary search tree T Output: Node w of the subtree T(v) of T rooted at v, such that either w is an internal node storing key k or w is the external node where an item with key k would belong if it existed If v is an external node then Return v Else if k<key(v) then Return TreeSearch(k,T.leftChild(v)) Else {we know k>key(v)} Return TreeSearch(k,T.rightChild(v)) Else {match found} Return v Source Code:- Q4-Tree.cpp

Time Complexity:- T(n) = T(n/2) + c

Array Data structure


Algorithm find (k,v) Input: Search key k, and node v of a binary search tree T Output: Node w of the subtree T(v) of T rooted at v, such that either w is an internal node storing key k or w is the external node where an item with key k would belong if it existed If T[v] eq -1 then Return v Else if k<T[v] then Return TreeSearch(k,T.leftChild(v)) Else {we know k>T[v]} Return TreeSearch(k,T.rightChild(v)) Else {match found} Return v Source Code:Q4-Array.cpp

Time Complexity:- T(n) = T(n/2) + c

Q1:
We want to build a dictionary of 3 letters (maximum) which uses sum of letters in the word for hashing e.g. if word the comes then sum would be 20+8+5 = 30 (where a=A1, b=B2, c=C3 .z=Z26) and word the will be stored at location 30 mod 26, now if some other word with same sum comes in, then it will be stored at same location but in proper manner so that it can be recovered efficiently. In this case, Collisions will be handled by chaining and case of word is ignored i.e. the and The will be treated same way. You have to perform following operations: Build dictionary using hash table and handle the duplicate word scenario i.e. word will not be added if it already exists. Search for the word entered by user. You have to provide the following for above mentioned operations. i. Pseudo code for the algorithms. ii. C/C++ Source code. iii. Time complexity analysis using Recursive equations. Details for programming section (part ii): Build dictionary by scanning the input file assign02_list.txt (Attached with this assign). assign02_list.txt is list of random words (1,2 and 3 letters long). Get word from user to add in dictionary. Get word from user to search in dictionary and respond accordingly i.e. Found or not found. Your output should consist of menus and sub menus (if required), would be something like this.
Scanning File Dictionary built 1. Add word to dictionary. 2. Search Word. 3. Exit. 1 Enter word to add: the Added or exists. (And handle rest of the cases accordingly.)

A1(i)
Find Element (k) BA[h(k)] If B is empty then Return No-such-key Else Return B.findElement(k) Insert Element(k,e) If A[h(k)] is empty then A[h(k)]B Else BA[h(k)] B.insertItem(k,e) A1(ii) Q1.cpp A1(iii) T(n) = O(n) in worst case for FindElement