+ 1
Insertion Sort using pointer
https://code.sololearn.com/ca219A127A23 after sorting the pointer array is not showing and also there is timeout error. Ques: 7.17 Implement the Indirect Insertion Sort. (See Problem 6.18 on page 144 and Example 7.17 on page 170.)
9 Answers
+ 2
https://code.sololearn.com/ca21a23a22A2
@Martin I have corrected the mistakes you have told me but this time the output is shown after sorting but not in the ascending oder.
+ 1
@Martin thank you so much ..I'll fix the code according to what you have said
+ 1
Ramisa Fariha
On line 12, `*p[j-i]` is incorrect. It is also accessing memory that does not belong to `p` when j < i (j<i => j-i is negative). The correct value to compare is `*p[j - 1]`. So line 12 should be
`while (j > 0 && *p[j - 1] > *p[j])`
Martin Taylor didn't mention this exactly but as you can see, they have written `*p[j-1]` in the fixed code in their answer.
Also, is there any specific reason for first making an array of float pointers and sorting that instead of directly sorting the array of float values? Is it to preserve the order in the original array? Or is it how you have to do it in the question?
+ 1
@Martin Taylor
OP corrected the code. They had written `*p[j-i]` instead of `*p[j-1]` on line 12.
Yes you told me that in another thread. That is why I was just asking if the question says it has to be done this way, or is it the OP's solution. Also, I just now noticed it's supposed to be 'indirect' insertion sort.
+ 1
@Martin I typed i instead of 1 at first and then XXX mentioned about it.Later I corrected it.
0
@XXX thank you so much for the correction.Martin was right..I,myself wrote i instead of 1 mistakenly..I have to do that according to the example mentioned in the question and also they mention the word indirect not directly changing the array