I am still kinda new to programming and I have a questions (yes it is a homework question), but I am not looking for you to give me the answer. I would like someone to explain to me how to go about determining the number of key comparisons for an insertion sort. The book really doesn't explain it at all, but yet it asks us to figure it out. So the book gives us the following list of keys: 18, 8, 11, 9, 15, 20, 32, 61, 22, 48, 75, 83, 35, 3.

I would like to know how to go about counting up the number of key comparisons for an insertion sort so that I can find the answer to this myself. Any help is much appreciated. Thanks!

Recommended Answers

All 4 Replies

Declare an int variable and whenever you do a comparison increase its value by one

haha I should have clarified my question a little bit better. I guess I wanted to know more along the lines of what a comparison is to be honest. I understand that I am comparing one node to another or whatever. But I want to see how to count the comparisons up. So my question lies more within "how does the insertion sort actually work/what is considered a key comparison in an insertion sort?"

A comparison is when you compare, possibly using the compareTo method (or operators such as <, >, and ==), one Node's value to another Node's value so that you can see if the first value is greater than, less than, or equal to the second value. A key comparison is simply the comparison of the one Node's value to the other Node's value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.