Computer Science, asked by srushtikuchanur, 1 year ago

Consider the array A[]= {5,4,9,1,3} apply the insertion sort to sort the array . Consider the cost associated with each sort is 25 rupees, what is the total cost of the insertion sort when element 1 reaches the first position of the array?​

Answers

Answered by pruthaasl
0

Answer:

The total cost of insertion sort when element 1 reaches the first position of the array is Rs. 125

Explanation:

  1. The cost of each insertion is given to be Rs. 25.
  2. Let n be the number of times swapping takes place till element 1 reaches the first position.
  3. Let us start with the first insertion sort, i.e., 5 and 4. Since 5 > 4, swapping will take place. Therefore, n = 1.
  4. The array elements will now be A[] = {4, 5, 9, 1, 3}.
  5. The next sort 5 > 9 will give a false result. So, swapping will not take place.
  6. The array elements will be A[] = {4, 5, 9, 1, 3}.
  7. The next sort 9 > 1 gives a true result. So, swapping takes place, and n = 2.
  8. The array elements will be A[] = {4, 5, 1, 9, 3}.
  9. The next sort 9 > 3 gives a true result. So, swapping takes place, and n = 3.
  10. The array elements will be A[] = {4, 5, 1, 3, 9}.
  11. The next sort 4 > 5 gives a false result, so swapping does not take place.
  12. The next sort 5 > 1 gives a true result. Swapping takes place and n = 4.
  13. The array elements will be A[] = {4, 1, 5, 3, 9}.
  14. The next sort 5 > 3 gives a true result. Swapping takes place and n = 5. The array elements will be A[] = {4, 1, 3, 5, 9}.
  15. The next sort 5 > 9 gives a false result and hence swapping does not take place.
  16. The next sort 4 > 1 gives a true result. Swapping takes place and n = 5.
  17. The array elements will be A[] = {1, 4, 5, 3, 9}.

Now, n = 5, and element 1 is in the first position. So, the total cost of insertion will be 5 × 25 = 125

Therefore, the total cost of insertion sort when element 1 reaches the first position is Rs. 125

#SPJ3

Answered by Jasleen0599
1

Sort array={1,3,4,5,9} and total cost=50 Rupees

the cost associated with each sort is 25 rupees, what is the total cost of the insertion sort when element 1 reaches the first position of the array

  • Two comparisons are all that are needed when element 1 reaches the top of the array, therefore

=25 * 2

=50 rupees.

the array A[]= {5,4,9,1,3} apply the insertion sort to sort the array .:

  • The straightforward sorting algorithm known as insertion sort functions similarly to how you would arrange playing cards in your hands. In a sense, the array is divided into sorted and unsorted parts. Values are chosen and assigned to the appropriate positions in the sorted part of the data from the unsorted part.

Insertion Sort:

Step1: {5,4,9,1,3}

Step2:{1,3,4,5,9}

Hence, Sort array={1,3,4,5,9} and total cost=50 Rupees

#SPJ3

Similar questions