Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 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 into log 2 n passes. Where in each pass algorithm merges the n elements in to the an array. So the total complexity of merge sort is O( n log 2 n). 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
0

= 1

15

12

11

13

= 2

15

11

12

13

= 4

11

12

15

13

= 8

11

12

13

15

= 16

11

12

13

15

= 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
2

Complexity of merge sort is O ( n

log

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
120

4
4 24

5
345 75

7
457

9
129

The new array is - 120

24

345

75

457

129

Step 2 According to Tens place 0


4

2
120 24 129

4
345

5
457

7
75

The new array is - 4

120

24

129

345

457

75

Step 2 According to hundredth place 0


4 24 75

1
120 129

3
345

4
457

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 <= Max

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 10 Set 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 of Corresponding Array] Set ResultArr [ Dig ] [ Top[Dig] ] = Arr [ I ] into Desired array of 2D Array [store the number [Incr the Top value

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

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) (b) (c) If the incoming character is a ( opening bracket then simply add it to the Operator_Stack. If the incoming character is a operand then simply add it to the Operand_Stack. 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

You might also like