In: Computer Science
1) Use insertion sort to sort: 25, 17, 31, 13, 2. Show your work step-by-step.
2) Use Euclidean algorithm to find gcd(248, 198)
3) (ABCDEF)16 to binary, octal and decimal
1) Use insertion sort to sort: 25, 17, 31, 13, 2. Show your work step-by-step.
Ans.
Let us now understand working with the following example:
Consider the following array: 25, 17, 31, 13, 2
First Iteration: Compare 25 with 17. The comparison shows 17< 25. Hence swap 17 and 25.
The array now looks like:
17, 25, 31, 13, 2
Second Iteration: Begin with the second element (25), but it was already swapped on for the correct position, so we move ahead to the next element.
Now hold on to the third element (31) and compare with the ones preceding it.
Since 31> 25, no swapping takes place.
Also, 31> 17, no swapping takes place and 31 remains at its position.
The array after the Second iteration looks like:
17, 25, 31, 13, 2
Third Iteration: Start the following Iteration with the fourth element (13), and compare it with its preceding elements.
Since 13< 31, we swap the two.
Array now becomes: 17, 25, 13, 31, 2.
But there still exist elements that we haven’t yet compared with 13. Now the comparison takes place between 25 and 13. Since, 13 < 25, we swap the two.
The array becomes 17, 13, 25, 31, 2.
The last comparison for the iteration is now between 17 and 13. Since 13 < 17, we swap the two.
The array now becomes 13, 17, 25, 31, 2.
Fourth Iteration: The last iteration calls for the comparison of the last element (2), with all the preceding elements and make the appropriate swapping between elements.
Since, 2< 31. Swap 2 and 31.
Array now becomes: 13, 17, 25, 2, 31.
Compare 2 with 25, 17, 13.
Since, 2< 25. Swap 25 and 2.
13, 17, 2, 25, 31.
Compare 2 with 17 and 13.
Since, 2<17. Swap 2 and 17.
Array now becomes:
13, 2, 17, 25, 31.
The last comparison for the Iteration is to compare 2 with 13.
Since 2< 13. Swap 2 and 13.
The array now becomes:
2, 13, 17, 25, 31.
This is the final array after all the corresponding iterations and swapping of elements.
2) Use Euclidean algorithm to find gcd(248, 198)
Ans.
Euclidean Algorithm for finding GCD(A,B) is as follows:
A=198, B=50
A=50, B=48
A=48, B=2
A=2, B=0
So
GCD(248,198) = 2
3) (ABCDEF)16 to binary, octal and decimal
To Binary : (ABCDEF)16
Step 1 : Convert the decimal equivalent of each hexadecimal digit to 4 binary digits.
Step 2 : Combine the binary groups
is the answer.
To Octal:
Step 1: First the individual digits are converted into its binary bits(each having 4 bits)
A B C D E F
1010 1011 1100 1101 1110 1111
Step 2: After that the subsequents bits are grouped into 3 bits.If.they are unable to form group of 3 the zero padding is done from the left.
101 010 111 100 110 111 101 111
Step 3: After that simply we convert the binary bite to its equivalent octal number.
101 010 111 100 110 111 101 111
5 2 7 4 6 7 5 7
So, (ABCD)16 =(52746757)8
To decimal : (ABCDEF)16
To decimal
Multiply each digit of the hex number with its corresponding power of 16( F=16^0, E=16^1,D=16^2........ A=16^5 )
=(Ax165+Bx164+Cx163+Dx162+Ex161+Fx160)
Since A=10,B=11,C=12,D=13,E=14,F=15
= (10x165+11x164+12x163+13x162+14x161+15x160)10
= (10485760+720896+49152+3328+224+15)10
= (11259375)10 is answer.