Upload
raditya-yadav
View
216
Download
0
Embed Size (px)
Citation preview
7/30/2019 Other Sorting Techniques and Algorithms
1/6
Other Sorting Techniques and Algorithms
Merge Sort
Merge sort uses the concept of merging. In other words concept of merging two sorted list into
single sorted list. The merge sort firstly partitioned the entire list intolog2
npasses. Where in
each pass algorithm merges the n elements in to the an array. So the total complexity of merge
sort is O( nlog
2n
). Following is the example that will illustrate this concept -
Sort the following sequence with merge-sort Total no of elements = 10 i.e. N=10
20
= 1
21 = 2
22= 4
23= 8
24= 16
24 = 16 , 16 > 10, so algorithm stops here because no further sub arrays are possible. In other
words we can not create a sub array of 16 elements from a list of 10 elements
Complexity of merge sort is O ( n log2n)
12 11 9 4 2 13 1 5615
6 15 11 12 4 9 2 13 1 5
6 11 12 15 2 4 9 13 1 5
2 4 6 9 11 12 13 15 1 5
1 2 3 4 6 9 11 12 13 15
7/30/2019 Other Sorting Techniques and Algorithms
2/6
Radix Sort
Radix Sort , as the name RADIX , means base for example decimal(10), binary(2),
octal(8), Hexa(16). In this case we are taking the base 10 for sorting the list of
numerical values. The algorithm is based upon the digit values of the numbers. It is
very easy to implement but requires lot of computing memory. For example
consider the following list sorted according to Radix sort algorithm
129 345 4 24 120 75 457
The algorithms starts from the Unit Place digit upto the maxDigits available. If a
number does not have digit available then take digit as 0. for example in the above
case 24 does not have hundredth place so take digit 0 in that pass.
In this example the maximum digits are 3. So the algorithm repeats three times.
Step 1 - According to Unit place
0 1 2 3 4 5 6 7 8 9120 4 345 457 129
24 75
The new array is - 120 4 24 345 75 457 129
Step 2 According to Tens place
0 1 2 3 4 5 6 7 8 94 120 345 457 75
24
129
The new array is - 4 120 24 129 345 457 75
7/30/2019 Other Sorting Techniques and Algorithms
3/6
Step 2 According to hundredth place
0 1 2 3 4 5 6 7 8 94 120 345 457
24 129
75
The new Array is 4 24 75 120 129 345 457
Now the process is over the array is sorted
Complexity of Radix sort is as follows
Total Memory D * M
Where M stands for 10 arrays for holding (0 to 9)
D stands for Maximum digits available
So in the above example the maximum digits are 3. so total memory requires
3*10
Time complexity of Radix sort is O( D * n) = O (D n)
Where D stands for maximum digits and n stands for no. of elements to sort.
Algorithm
Arr is the array which we have to sort using radix sort. MaxDigit( ) function
calculates the maximum digits length of any number in array Arr, ResultArr is the
2D array corresponding to digits 0 to 9. Top[ ] is a one dimensional array holding
the current position of each of the 0 to 9 arrays.
Step 1: Set Count = 1, Max = MaxDigit(arr)
Step 2: Repeat while count
7/30/2019 Other Sorting Techniques and Algorithms
4/6
a) For each element of the array Arr , Finds digit right to left corresponding
to the Place specified by count as follows -
For place = 1 to count
If test num > 0 then
Set Dig = num MOD 10Set num = num / 10
[End of If Structure]
[End of For loop]
b) Store each element of Arr , into 2D array, corresponding to their digit
value as
Set Top [Dig] = Top [Dig] + 1 [Incr the Top valueof
Corresponding Array]
Set ResultArr [ Dig ] [ Top[Dig] ] = Arr [ I ] [store the numberinto
Desired array of 2DArray
c) Collect all numbers from 2D array, back to the original Arr array, So to
start next pass.
Set Ind = 1
For I=0 to 9
For J=0 to Top[ I ]
Set Ind=Ind+1
Set Arr[Ind]=ResultArr [ I ] [ J ]
[End of For loop]
[End of For loop]
d) Increment the Count by 1
7/30/2019 Other Sorting Techniques and Algorithms
5/6
Step 3: Exit
Procedure/Algorithm To Convert Infix Expression To Post Fix
The method uses the extensive use of Stacks in its implementation. Operator_stack
stores the operators and operand_stack stores the operand data from incoming infix
expression.
Step 1: Enclose the Infix Expression into left or right parenthesis.
Step 2: Read Expression from Left to Right and repeat the following step
(a) If the incoming character is a ( opening bracket then simply add
it to the Operator_Stack.
(b) If the incoming character is a operand then simply add it to the
Operand_Stack.
(c) If the incoming character is a Operator then repeatedly POP the
operators from operator_stack which has the same or highest
priority than our incoming operator, and put such operators to
Operand_Stack.
At any time where no operator has same or high priority then
simply add the new incoming operator to the top of operator_stack.
(d) If the incoming character is closing ) bracket then POP each
operator from the Top of Operator_stack and add it to
operand_stack until the ( bracket is not encountered. After then
pop that left parenthesis but do not add it to operand_stack.
Step 3 : Print the Operand_Stack
Step 4: Exit
7/30/2019 Other Sorting Techniques and Algorithms
6/6