P. 1
Other Sorting Techniques and Algorithms

|Views: 2|Likes:

See more
See less

12/15/2012

pdf

text

original

# 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

2

= 1

15

6

12

11

9

4

2

13

1

5

2

1

= 2

6

15

11

12

4

9

2

13

1

5

2

2

= 4

6

11

12

15

2

4

9

13

1

5

2

3

= 8

2

4

6

9

11

12

13

15

1

5

2

4

= 16

1

2

3

4

6

9

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

4

Complexity of merge sort is – O ( n

log

2

n

)

for example in the above case 24 does not have hundredth place so take digit 0 in that pass.According to Unit place 0 120 1 2 3 4 4 24 5 345 75 6 7 457 8 9 129 The new array is . In this case we are taking the base 10 for sorting the list of numerical values. means base for example decimal(10). 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. In this example the maximum digits are 3.Radix Sort Radix Sort . as the name RADIX . binary(2). Hexa(16).120 4 24 345 75 457 129 Step – 2 – According to Ten’s place 0 4 1 2 120 24 129 3 4 345 5 457 6 7 75 8 9 The new array is .4 120 24 129 345 457 75 . The algorithm is based upon the digit values of the numbers. Step – 1 . octal(8). It is very easy to implement but requires lot of computing memory. So the algorithm repeats three times. If a number does not have digit available then take digit as 0.

Algorithm – Arr is the array which we have to sort using radix sort. ResultArr is the 2D array corresponding to digits 0 to 9. MaxDigit( ) function calculates the maximum digits length of any number in array Arr.Step – 2 – According to hundredth place 0 4 24 75 1 120 129 2 3 345 4 457 5 6 7 8 9 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. Top[ ] is a one dimensional array holding the current position of each of the 0 to 9 arrays. Max = MaxDigit(arr) Step – 2: Repeat while count <= Max . 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. Step – 1: Set Count = 1.

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 . back to the original Arr array.a) For each element of the array Arr . into 2D array. 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 . 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. So to start next pass.

Step – 3 : Print the Operand_Stack Step – 4: Exit . 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. 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. At any time where no operator has same or high priority then simply add the new incoming operator to the top of operator_stack. Operator_stack stores the operators and operand_stack stores the operand data from incoming infix expression. and put such operators to Operand_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 – 1: Enclose the Infix Expression into left or right parenthesis. If the incoming character is a operand then simply add it to the Operand_Stack.Step – 3: Exit Procedure/Algorithm To Convert Infix Expression To Post Fix The method uses the extensive use of Stacks in its implementation.

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->