Algorithm
Algorithm
Something that needs to be true before the use case is triggered is called a
precondition. For instance, "The user has accessed the website and wants to
buy a book" may be a prerequisite for the use case "Buy Book on WebSite".
2. What is a postcondition?
Something that needs to be true once the use case is complete is called a
post condition. For instance, the user's book purchase was successful.
4. How time-complexity of the above code can be minimized? Justify your answer by
writing another program for the same problem. (Hint: Think of using some sort of
formula to sum up all the numbers.)
To minimize the time complexity of the code, we can use a
mathematical formula instead of looping through each number.Sum of
numbers from 1 to n directly in constant time, O(1).
4. pseudocode
function reverseLinkedList(head):
previous = null
current = head
nextNode = current.next
current.next = previous
previous = current
current = nextNode
head = previous
return head
5. Previous = null
current = A
Store nextNode = C.
FUNCTION main()
CALL functionA()
END FUNCTION
FUNCTION functionA()
CALL functionB()
END FUNCTION
FUNCTION functionB()
CALL functionC()
END FUNCTION
FUNCTION functionC()
END FUNCTION
→ [56, 40] and [30, 78, 23], [7, 9] and [10, 12, 5]
→ [56] and [40], [30] and [78, 23], [7] and [9], [10] and [12, 5]
→ [56], [40], [30], [78], [23], [7], [9], [10], [12], [5]
3. *Continue merging:*
- Merge [23, 78] and [30, 40, 56]: [23, 30, 40, 56, 78]
4. *Final merge:*
- Merge [23, 30, 40, 56, 78] and [5, 7, 9, 10, 12]: [5, 7, 9, 10, 12, 23, 30,
40, 56, 78]
For Figure 6:
*Level-order traversal:* 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
---
Visit the left subtree, root, and then the right subtree.
For Figure 6: