0% found this document useful (0 votes)
7 views1 page

Sorting

Dsa Shorting

Uploaded by

nirajant1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Sorting

Dsa Shorting

Uploaded by

nirajant1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

1) Bubble sort Algorithm to convert infix to postfix notation

In this article, we will discuss the Bubble sort Here we use a single stack.
Algorithm. The working procedure of bubble sort is 1. Start
simplest. This article will be very helpful and 2. Scan the Infix string from left to right.
interesting to students as they might face bubble 3. Initialize an empty stack.
sort as a question in their examinations. So, it is 4. If the scanned character is an operand, add it to
important to discuss the topic. the Postfix string.
Algorithms 5. If the scanned character is an operator and if the
Let's look at implementing the optimized version of stack is empty push the character to stack.
bubble sort: 6. If the scanned character is an Operator and the
1. Start stack is not empty, compare the precedence of the
2. For the first iteration, compare all the elements character with the element on top of the stack.
(n). For the subsequent runs, compare (n- 1) (n-2) 7. If top Stack symbol has higher precedence over
and so on. the scanned character then pop the top element of
3. Compare each element with its right side stack else push the scanned character to stack.
neighbor. 8. Repeat step 7 until the stack is not empty and top
4. Swap the smallest element to the left. Stack has precedence over the character.
5. Keep repeating steps 1-3 until the whole list is 9. Repeat 4 and 8 steps till all the characters are
covered. scanned.
6. Stop 10. After all characters are scanned, we have to add
2) Selection Sort Algorithm any character that the stack may have to the Postfix
In this article, we will discuss the Selection sort string.
Algorithm. The working procedure of selection sort 11. If stack is not empty add top Stack to Postfix
is also simple. This article will be very helpful and string and Pop the stack.
interesting to students as they might face selection 12. Repeat step 11 as long as stack is not empty.
sort as a question in their examinations. So, it is 13. Stop
important to discuss the topic. Converting an Infix expression to prefix expression
Algorithm by using stack
1. Start 1. Start
2. Consider the first element to be sorted and the 2. Scan one character at a time of an infix expression
rest to be unsorted from right to left
3. Assume the first element to be the smallest 3. Opstack=the empty stack
element. 4. Repeat till there is data in infix expression
4. Check if the first element is smaller than each of 4.1 If scanned character is ')' then push it to opstack
the other elements: 4.2 If scanned character is operand then push it to
 If yes, do nothing prestack
 If no, choose the other smaller element as 4.3 If scanned character is operator then if (opstack!
minimum and repeat step 3 =-1) While (precedence (opstack [otos])>precedence
5. After completion of one iteration through the list, (scan character)) then pop and push it into prestack
swap the smallest element with the first element of Otherwise
the list. Push scanned character into opstack
6. or Now consider the second element in the list to 4.4 If scanned character is '(' then
be the smallest and so on till all the elements in the Pop and push into prestack until ')' is not found and
list are covered. ignore both symbols
7. Stop 5. Pop and push into prestack until opstack is not
3)Insertion Sort Algorithm empty.
In this article, we will discuss the Insertion sort 6. Pop and display prestack which gives required
Algorithm. The working procedure of insertion sort prefix expression
is also simple. This article will be very helpful and 7. Stop
interesting to students as they might face insertion Conversion of postfix expression to infix expression
sort as a question in their examinations. So, it is 1. While there are input symbol left in given postfix
important to discuss the topic. expression
Algorithm 2. Read the next symbol from input i.e. scan one
step 1 − If it is the first element, it is already sorted. character at a time from left to right of given postfix
return 1; expression
Step 2 − Pick next element 3. If the symbol is an operand
Step 3 − Compare with all elements in the sorted Push it onto the stack.
sub-list 4. Otherwise,
Step 4 − Shift all the elements in the sorted sub-list The symbol is an operator.
that is greater than the value to be sorted 5. If there are fewer than 2 values on the stack
Step 5 − Insert the value Show Error /* input not sufficient values in the
Step 6 − Repeat until list is sorted expression */
4) Merge Sort Algorithm 6. Else
In this article, we will discuss the merge sort Pop the top 2 values from the stack.
Algorithm. Merge sort is the sorting technique that Put the operator, with the values as arguments and
follows the divide and conquer approach. This article form a string. Encapsulate the resulted string with
will be very helpful and interesting to students as parenthesis.
they might face merge sort as a question in their Push the resulted string back to stack.
examinations. In coding or technical interviews for 7.If there is only one value in the stack
software engineers, sorting algorithms are widely That value in the stack is the desired infix string.
asked. 8.If there are more values in the stack
Algorithm Show Error /* the user input has too many values */
1. Start Conversion of prefix expression to infix expression
2. Split the unsorted list into two groups recursively 1. While there are input symbol left
until there is one element per group 2. Read the next symbol from input i.e. read one
3. Compare each of the elements and then sort and character at a time from right to left of given prefix
group them expression
4. Repeat step 2 until the whole list is merged and 3. If the symbol is an operand
sorted in the process Push it onto the stack.
5. Stop 4. Otherwise,
5) Quick Sort Algorithm The symbol is an operator.
In this article, we will discuss the Quicksort 5. If there are fewer than 2 values on the stack
Algorithm. The working procedure of Quicksort is Show Error /* input not sufficient values in the
also simple. This article will be very helpful and expression */
interesting to students as they might face quicksort 6.Else
as a question in their examinations. So, it is Pop the top 2 values from the stack.
important to discuss the topic. Put the operator, with the values as arguments and
Algorithm form a string.
Step 1 − Choose the highest index value has pivot Encapsulate the resulted string with parenthesis.
Step 2 − Take two variables to point left and right of Push the resulted string back to stack.
the list excluding pivot 7.If there is only one value in the stack
Step 3 − left points to the low index That value in the stack is the desired infix string.
Step 4 − right points to the high 8.If there are more values in the stack
Step 5 − while value at left is less than pivot move Show Error /* the user input has too many values */
right
Step 6 − while value at right is greater than pivot
move left
Step 7 − if both step 5 and step 6 does not match
swap left and right
Step 8 − if left ≥ right, the point where they met is
new pivot

You might also like