You are on page 1of 3

# -----------------Answer 1 ------------------In the preceding algorithm, * at step (v), value of j must be decremented * and at step (vii), value of i must

be incremented. The correct algorithm is as follows: 1. Repeat step 2 varying incr from 3 to 1 2. Repeat steps 3 and 4, varying L from 0 to incr - 1 3. Apply insertion sort on the Lth sublist: a. Set i=L + incr b. Repeat until i becomes greater than or equal to n: i. Set temp = arr[i] ii. Set j=i-incr iii. Repeat steps iv and v until j becomes less than 0 or arr[j] becomes less than or equal to temp iv. Shift the value at index j to index j+incr v. Decrement j by the value contained in incr vi. Store temp at index j+incr vii. Increment i by the value contained in incr ------------------Answer 2 ------------------Following are the errors: a. end of list checking is not done e.g. (low>high) check is not done. b. step 5 and 8 steps must be interchanged. c. swaping of value at i with value of j , where greater element is on the le ft side of smaller one is not done. Correct algorithm is written as follows: 1. If(low>high): a. return 2. Set pivot=arr[low] 3. Set i=low+1; 4. Set j=high 5. Repeat step 6 until i>high or arr[i]>pivot 6. Increment i by 1 7. Repeat step 8 until j<low or arr[j]<pivot 8. Decrement j by 1 9. If(i<j): a.Swap arr[i] with arr[j] 10. If(i<=j): a. Go to step 5 11. If(low < j): a. Swap arr[low] with arr[j] 12. QuickSort(low,j-1) 13. QuickSort(j+1,high) --------------------------------------Answer 3 ---------------------------------------

Follwing are the errors: List empty check is not done in this algorithm follwing is the correct algorithm: 1. Allocate memory for the new node. 2. Assign value to the data field of the new node. 3. If START is null then, a. Make START point to new node b. Go to point 6. 4. Locate the last node in the list, and mark it as currentNode. To locate the l ast node in the list, execute the following steps: a. b. c. 5.Make the 6.Make the Mark the first node as current Node. Repeat step c until the successor of currentNode becomes NULL. Make currentNode point to the next node in sequence. next field of currentNode point to the new node next field of the new node point to NULL.

-------------------------------------Answer 5 --------------------------------------You need to perform following steps for searching value in binary search tree: 1. Make currentNode point to the root node. 2. If currentNode is null: a. Display "Not found" b. Exit 3. Compare the value o be searched with the value of currentNode. Depending on t he result of the comparioson, there can be three possibilities: a.If the value is equal to the value of currentNode: i. Display "found" ii. Exit b. If the value is less than the value of currenNode: i. Make currentNode point to its left child ii. Go to step 2. c. If the value is greater than the value of currentNode: i. Make the currentNode point to its right child ii. Go to step 2. --------------------------------------Answer 6 --------------------------------------In the preceding algorithm, step 7 and 8 are in incorrect order. they must be in terchanged. Following is the correct algorithm: 1. If the left child of the header node is a thread pointing to itself: a.Display "Tree is empty" b. Exit 2. Mark the left child of the header node as currentNode. 3. Repeat step 4 until the left child of currentNode becomes a thread. 4.Make currentNode point to its left child. 5. Display the information contained in currentNode. 6.Repeat step 7 and 8 until right child of currentNode points to the header node . 7. Find the inorder successor of currentNode, and mark the inorfer successor as currentNode.

8. Display information contained in currentNode. -------------------answer 4 -------------------1.Initialize the stack to be empty 2. Initialize the postfix string to be empty. 3. scan the entries in the infix expression from left to right until the end of expression is encountered: a. If the scanned entry is: i. An operand: append the operand to the postfix string. ii. An operator: compare the precedence of the operator with the operato r on the top of the stack. If the precedence of the operator on the top of the s tack is greater than or equal to the precedence of the scanned operator, pop the operator from the stack and append it to the postfix string. Repeat this step u ntil the operator at the top of the stack has a precedence less than the scanned operator or stack becomes empty. at the end, push the scanned operator in the s tack. iii. A left parenthesis: push it onto the stack iv. A right parenthesis: pop stack elements one by one and append them t o the postfix string until a left parenthesis is popped. 4. pop all the elements from the stack and append them to the postfix string.