Question

Using the method introduced in class, show all intermediate steps of Insertion Sort when sorting the...

Using the method introduced in class, show all intermediate steps of Insertion Sort when sorting the list

27, 11, 8, 47, 88, 71, 28, 4, 19

in increasing order. For every intermediate step, show the number of comparisons of list elements needed. Finally, give the total number of comparisons needed.

Homework Answers

Answer #1

Steps We follow in Insertion sort

1: Iterate from start till end over the array.
2: Compare the current element to its last element
3: If the element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position right to make space for the element that we swap

We have Array as : 27, 11, 8, 47, 88, 71, 28, 4, 19

Step 1

Insert 27

Step 2

Insert 11

Compare 11 and 27 as 11 <27 so swap them

Now Array is 11 27

Comparisons = 1

Step 3

insert 8

11 27 8

We compare 8 with 27 first so shift 27 one right then we compare 8 with 11 and shift 11 also one place right

Now Array is 8 11 27

Comparisons = 2

Step 4

Insert 47

47 and 27 is compared so as 47> 27 so shift is needed

Now Array is 8 11 27 47

Comparisons = 1

Step 5

Insert 88

88 and 47 is compared so as 88> 47 so shift is needed

Now Array is 8 11 27 47 88

Comparisons = 1

Step 6

Insert 71

71 and 88 are compared so as 71 < 88 so 88 will shift one place right .

Now 71 is compared with 47 . As 71 > 47 so no shift .

Now Array is 8 11 27 47 71 88

Comparisons = 2

Step 7

Insert 28

It will be compared with 47 , 71 , 88 and all three will shift one place right

Now compare it with 27 as 28 > 27 so no shift

Now Array is 8 11 27 28 47 71 88

Comparisons = 4

Step 8

Insert 4

So as 4 has to go at first place so it is compared with each element

Now Array is 4 8 11 27 28 47 71 88

Comparisons = 7

Step 9

Insert 19

It will be compared with 47 , 71 , 88 , 27 ,28 and all will shift one place right

Now compare it with 11 as 19 > 11 so no shift

Now Array is 4 8 11 19 27 28 47 71 88

Comparisons = 6

Hence we get the Sorted Array : 4 8 11 19 27 28 47 71 88

Total Number of Comparison = 0 + 1+ 2+ 1 + 1 + 2 + 4 + 7 + 6 = 24

This is how insertion sort works and we can count number of comparisons in each step .

Thank You

If u like the answer then do Upvote it and have any doubt ask in comments

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do this manually, i.e. step through the algorithm yourself without a computer. This question is related to data structure and algorithm in javascript (.js). Please do not copy from stackabuse or stackoverflow, Please explain that algorithm with comments. i really want to learn this concept.
Write a code in c++ using linear insertion following the steps below. Comment your work. 1....
Write a code in c++ using linear insertion following the steps below. Comment your work. 1.    Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (*) as the first and only character on a line should terminate the program. 2.     You can use a statically-allocated one-dimensional array of doubles for this with length 100. You...
23 30 35 43 4 10 22 4 Complete the following: a) Use the bubble sort...
23 30 35 43 4 10 22 4 Complete the following: a) Use the bubble sort to sort the values showing the order of the values in the list after every pass of the sorting algorithm.   b) What is the time complexity of the sort (use the number of comparisons required as a measure of time.)    c) Modify the algorithm so it stops if there are no swaps in a complete pass. How many comparisons would be needed in your...
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method....
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method. When the text file is processed, each line of text will be passed to processLine . Each line contains 4 fields: title, album, artist, and play time. The album field is optional. Each field is separated by a comma. TODO 2: Implement the songToCSVString method. This method takes a Song object as a parameter and returns a String which is the csv representation of...
I am making a game like Rock Paper Scissors called fire water stick where the rules...
I am making a game like Rock Paper Scissors called fire water stick where the rules are Stick beats Water by floating on top of it Water beats Fire by putting it out Fire beats Stick by burning it The TODOS are as follows: TODO 1: Declare the instance variables of the class. Instance variables are private variables that keep the state of the game. The recommended instance variables are: 1. 2. 3. 4. 5. 6. A variable, “rand” that...
In narrative essay format, I want you to address a business/organization case study using multiple concepts...
In narrative essay format, I want you to address a business/organization case study using multiple concepts from class. The case question and case text begin on page 5 of this document. You need to demonstrate their best understanding of management and organizational behavior theory, and the application of those ideas to improve the understanding of various issues. You need to clearly identify at least 3 distinct, substantive issues. For each issue you need to 1), identify evidence from the case...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
Gender Bias in the Executive Suite Worldwide The Grant Thornton International Business Report (IBR) has described...
Gender Bias in the Executive Suite Worldwide The Grant Thornton International Business Report (IBR) has described itself as "a quarterly survey of business leaders from across the globe … surveying 11,500 businesses in 40 economies across the globe on an annual basis." 1 According to the 2011 IBR, the Asia Pacific region had a higher percentage (27 percent) of female chief executive officers (CEOs) than Europe and North America. Japan is the only Asia Pacific region exception. The report further...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...