Dsa UNIT-2
Dsa UNIT-2
SYLLABUS
Arrays, Stacks & Queues
Algorithm :
• Take the size of the array from the user.
• Declare an array of given input size.
• Take the input of all elements of the array.
• Now run a for loop from 0 to size-1.
• And for every element check it from all the next elements to it. If
the element is greater than swap that number.
• In this way the array will get sorted in ascending order.
https://www.geeksforgeeks.org/sorting-algorithms/
Stacks
• Stack is a linear data structure that follows a
particular order in which the operations are
performed.
• The insertion of a new element and removal of an
existing element takes place at the same end
represented as the top of the stack.
• The order may be LIFO(Last In First Out) or
FILO(First In Last Out). LIFO implies that the
element that is inserted last, comes out first and
FILO implies that the element that is inserted
first, comes out last.
Stacks
It is named stack because it has the similar operations as the real-world stacks, for
example – a pack of cards or a pile of plates, etc.
Stacks
• There are many real-life examples of a stack.
Consider an example of plates stacked over
one another in the canteen.
• The plate which is at the top is the first one to
be removed, i.e. the plate which has been
placed at the bottommost position remains in
the stack for the longest period of time. So, it
can be simply seen to follow LIFO(Last In First
Out)/FILO(First In Last Out) order.
LIFO( Last In First Out ):
• String reversal: Stack is also used for reversing a string. For example, we want to reverse a
"javaTpoint" string, so we can achieve this with the help of a stack.
First, we push all the characters of the string in a stack until we reach the null character.
After pushing all the characters, we start taking out the character one by one until we reach
the bottom of the stack.
• UNDO/REDO: It can also be used for performing UNDO/REDO operations. For example, we
have an editor in which we write 'a', then 'b', and then 'c'; therefore, the text written in an
editor is abc. So, there are three states, a, ab, and abc, which are stored in a stack. There
would be two stacks in which one stack shows UNDO state, and the other shows REDO state.
If we want to perform UNDO operation, and want to achieve 'ab' state, then we implement
pop operation.
• Recursion: The recursion means that the function is calling itself again. To maintain the
previous states, the compiler creates a system stack in which all the previous records of the
function are maintained.
• DFS(Depth First Search): This search is implemented on a Graph, and Graph uses the stack
data structure.
•
Applications of Stack
• Algorithm:
• Create a temporary stack say tmpStack.
• While input stack is NOT empty do this:
– Pop an element from input stack call it temp
– while temporary stack is NOT empty and top of
temporary stack is greater than temp,
pop from temporary stack and push it to the input
stack
– push temp in temporary stack
• The sorted numbers are in tmpStack
Sort a Stack using Recursion
Due to the fact that queue performs actions on first in first out basis
which is quite fair for the ordering of actions. There are various
applications of queues discussed as below.
• Queues are widely used as waiting lists for a single shared resource
like printer, disk, CPU.
• Queues are used in asynchronous transfer of data (where data is
not being transferred at the same rate between two processes) for
eg. pipes, file IO, sockets.
• Queues are used as buffers in most of the applications like MP3
media player, CD player, etc.
• Queue are used to maintain the play list in media players in order to
add and remove the songs from the play-list.
• Queues are used in operating systems for handling interrupts.
Basic Operations
• We maintain the sorted queue at the end of the queue. For each of the n iterations, we find the
minimum element from the unsorted queue. We store it's value as well as it's index. Then, we
remove minIndex - 1 elements from the queue and p insert them back at the end of the queue. We
pop the minimum element and store it in a variable. Next, we again remove the elements occuring
after the minimum element (elements at indices minIndex+1 to n) and push them at the end f the
queue. The detailed algorithm is discusssed below.
• Algorithm
• The steps to sort a queue using O(1) space are:
Do this N times:
1. Search for the minimum element in the unsorted part of the queue. Store it's value and the
index.
2. Remove the elements occuring before the minimum element from the front of the queue and
insert them at the end of the queue.
3. Once the index of the minimum element is reached, pop the minimum element ans store it in a
variable.
4. Next, remove the elements occuring after the minimum element from the front of the queue
and insert them at the end of the queue.
5. Finally, insert the minimum element (from 3) into the end of the queue.
• For searching the element in the queue int Step 1, we traverse the queue and find the minimum
element. After a complete traversal, the queue comes to the same state as it was before the
traversal started. Hence, Step 1 does not effectively change the queue in any way.
Method 3 : Using recursion
• Idea is to maintain the sorted elements of the queue in the auxiliary stack. We also use an extra
queue for maintaining the order of the sorted elements in the stack when a new element is to be
pushed in the stack.
• Algorithm
• The steps to sort a queue using Stack are:
• Create an auxiliary stack and an auxiliary queue.
• If the stack is empty, push the element in the stack.
• If the next element at the front of the queue is greater than the top of the stack, push it on the
stack.
• Else, pop the elements out of the stack until a smaller or equal element is found at the top of the
stack.
• The popped elements in step 4, must be pushed into the auxiliary queue and once a smaller/equal
element is found, push the current element and empty the auxiliary queue on the stack.
• Continue until the stack size becomes equal to the size of the input queue and he input queue
becomes empty.
• Now transfer all the elements from the stack to the queue. The resulting queue will have the
elements in descending order.
Type of Queues
• Simple Queue or Linear Queue
• Circular Queue
• Priority Queue
• Double Ended Queue (or Deque)
Simple Queue or Linear Queue