Question
Discrete math question

2. Consider to the following two algorithms procedure SortA(a1,a2, ..., an: a list of real numbers with n 2 2) 1, for j := 2 to n 2. i:= 1 3. while aj > ai 4. 5. m: 6. or k 0toj -i-1 7. i:-i+1 aj-k:aj-k-1 ai := m
media%2F185%2F185eab87-3132-4979-93d3-e5
0 0
Add a comment Improve this question Transcribed image text
Answer #1
comparisons are made in while loop.
This is an example of insertion sort.

number of comparisons for j=2 is 1
number of comparisons for j=3 is 2
number of comparisons for j=4 is 3
...
number of comparisons for j=n is n-1

total number of comparisons = 1+2+3+...+(n-1)
=\sum_{i=1}^{n-1}1

formula\;for\;sum\;of\;first\;n-1\;natural\;numbers\;is\;\frac{n(n-1)}{2}

so,\;total\;number\;of\;comparisons=\frac{n(n-1)}{2}

Add a comment
Know the answer?
Add Answer to:
Discrete math question 2. Consider to the following two algorithms procedure SortA(a1,a2, ..., an: a list...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT