assignment
assignment
assignment
Here LA is a linear array with N element and K is a Positive integer such that K<=N. This Algorithm
deletes the Kth element from LA.
STEP#1
Start.
STEP#2
Set ITEM:=A[K]
STEP#3
STEP#4
STEP#5
STEP#6
[End of loop]
SETP#8
Stop
TIME COMPLEXITY FOR DELETION ALGORITHM
Deleting from a linear array takes 0(1) time if you know the index, O(n) in the worst case if searching by
value, and O(n) when shifting elements (beginning/middle).
Q2. which Sorting technique is best among Bubble, insertion and selection, and why?
Ans. The best sorting technique among Bubble, Insertion, and Selection sort depends on the specific
scenario and data characteristics. In general:
. Insertion Sort: It has a best-case time complexity of O(n) when the list is already sorted or nearly
sorted. However, its average and worst-case time complexity is O(n^2), making it less efficient for larger
datasets due to the need for multiple comparisons and swaps.
. Selection Sort: This algorithm has a time complexity of O(n^2) in all cases, including the best, average,
and worst-case scenarios. While it's simple to implement and works well for small datasets, it's generally
less efficient compared to Insertion Sort, especially for larger datasets.
Bubble Sort: Similar to Insertion Sort, Bubble Sort also has a best-case time complexity of O(n) when the
list is already sorted. However, its average and worst-case time complexity is O(n^2), which makes it less
efficient compared to both Insertion and Selection sorts, particularly for larger datasets.
So, for small datasets or nearly sorted lists, Insertion Sort tends to perform slightly better due to its
simplicity and lower overhead, as it has a linear best-case time complexity.
10
Empty
PASS#1
10
PASS#2
10
PASS#3
10
PASS#4
10
PASS#5
10
10
If element of sorted sub list is by one to right and Put Stored element in variable to that space. We
repeated these steps up to the Sorted.
PASS#1
10
PASS#2
12334
PASS#3
10
PASS#4
10
2
In this whole process we take one value from unsorted sub list and insert it in sorted sub list so, it is
called insertion sort.
PASS#5
10
Empty
PASS#1
PASS#3
PASS#4
PASS#5
Remaining last is only one so it is considered as sorted. Above is the sorting on my name.
Ans.
only need to
need to be
elements
2. Dynamic Size:
3. Data:
Linked lists: Only allocate memory for actual elements, avoiding wasted space for empty or unused
positions.
Arrays: Occupy a contiguous block of memory, even for sparse data with many empty elements,
potentially leading to memory waste.
: Linked lists: More natural for representing certain data structures like stacks, queues, graphs, and
trees, where elements have logical relationships rather than being ordered. by index.
Arrays: Less suitable for these structures due to their fixed size and sequential access nature.
Remember: Arrays still excel in random access (reaching any clement directly by index) and cache
performance (due to contiguous memory blocks). Choose the data structure that best suits your specific
needs based on these performance characteristics and the operations you'll perform most frequently.
Ans:
This algo finds the location LOC of the node where ITEM first appears in list or set LOC:=NULL
Else:
Set PTR-LINK[PTR] [Now PTR points to next node] [end of step 2 loop)
5. exit
This algo deletes the node N with location LOC. LOCP is the location of the node which precedes Nor,
when N is first Node, LOCP = NULL
else:
2. [Return deleted Nde to AVAIL] Set LINK [LOG]-AVAIL Set AVAIL LOC
3. Exit
Ans: Insertion:
This is because you only need to update a few pointers to insert a new node, regardless of the list's size.
Even in the worst case, such as inserting at the beginning, you only need to adjust two pointers (head
and first node).
Deletion:
Similar to insertion, deleting a node involves updating a few pointers, taking constant time on average.
Even in the worst case, such as deleting the head node, you only need to update two pointers (head and
second node).
Key points:
Linked lists excel at insertions and deletions because they don't require shifting elements like arrays do.
The constant time complexity for both operations makes them ideal for scenarios where frequent
insertions and deletions are expected.
This efficiency is due to their dynamic structure, where elements are linked using pointers rather than
being stored in contiguous memory like arrays.
Applications of stack:
2. Undo Mechanisms: Many applications use Stacks to implement undo mechanisms. Each action
Performed is Pushed onto the stack, and undoing involves Popping actions off the Stack.
3. Backtracking Algorithms: Backtracking Algorithms are most used by Stack, where decisions need to be
revisited and alternatives explored.
4. Browser History: Stacks can be used to implement the back and used Forward Functionality in web
browsers, managing the use's navigation history
5. Task Scheduling: In operating Systems, Stacks are employed to manage task scheduling and track the
execution of Processes.
Q12. Write the algorithm for traversing of grounded header linked list and circular header linked list.
Let's list be a grounded list in memory. This algorithm traverses list applying an operation process to
each node of list.
4. Set PTR:-LINK[PTR].
5. Exit
Let's list be a circular header list in memory. This algorithm traverses list applying an operation Process
to each node of list.
5. Exit
Q13. write down the benefits of header Linked list over Simple linked list.
Header linked Lists have a distinct advantage header node. some benefits include.
1. Simplifies Operations:
The header node allows for and Header linked the header node a place the list is empty. 22C5107 is
used a header node.
deletion, as it
serves as
Overall, header Linked lists enhance the clarity and simplicity of Linked list operations, especially when
compared to the complexity that can arise in a Simple Linked List.
Q14. Write down the traversing algorithm for 2-way Linked list in Forward direction.
Algorithm:
1. Set PTR:-START
5. Exist.
Q15. Write down the backward traversing algorithm for a 2-way linked list.
Algorithm:
1. Set PTR-End.
Evaluating expressions using a stack is a common technique, especially for arithmetic expressions in infix
notation. Here's a general algorithm to evaluate arithmetic expressions using a stack:
4. If the current character is an operator (+), pop the top two operands from the stack, perform the
operation, and push the result back onto the stack.
6. At the end, the result will be the only element left on the stack.