Question regarding the mid value used in the most searching and sorting algorithms. | Sololearn: Learn to code for FREE!

+6

Question regarding the mid value used in the most searching and sorting algorithms.

" The safest way to find the middle of two numbers without getting an overflow is as follows: mid = start + (end-start)/2 " Why it can't be mid = (start + end) / 2 ?

4/10/2021 5:07:50 PM

Prince Gupta

16 Answers

New Answer

+5

start + (end - start) / 2 Is preferred because there is a possibility in a very large array that the result from (start + end) could cause an integer overflow where the resulting value is larger than the max value for int. This would end up in a negative value, causing the index to be out of the bounds of the array.

+2

Thank You.

+1

https://code.sololearn.com/c9aYXjD55L6E/?ref=app

+1

Use binary sort 😎

+1

it is same, m = (l+r)/2 you should know what it does, In array it takes the array indices and then it find the mid value on its basis we access the mid position's value. let l = start, r = end, m = mid; m = l + (r-l)/2 = l + r/2 - l/2 = l/2+r/2 = (l+r)/2. That's how the fancy formula is straight forward written.

0

May anyone provide me with an example ?

0

good...to know this thing!!

0

thnx for the help wil try apllying it

0

Your article content is being very much interested, I am very impressed with your post. I hope to receive more great posts. https://www.mygroundbiz.me/

0

Thanks for sharing this is useful information. https://www.skylightpaycard.me/

0

One measure of efficiency is the time used by a computer to solve a problem using the algorithm, when input values are of a specified size. https://www.dgcustomerfirst.page/

0

Every time I open the Q&A tab I see trending discussions. I, for one, am rarely interested in those since they often have tons of answers already. Once in a while though they do contain things that are of interest... I digress... What I am saying is having a FAQs or well, the name might be different, https://www.firstcallonline.us/ section like trending just in the dropdown menu for the Q&A where the most common discussion questions are kept, not just questions about SoloLearn in general but all those other questions that we all know: difference between java & javascript, what does the % operator do and so on...

0

One measure of efficiency is the time used by a computer to solve a problem using the algorithm, when input values are of a specified size https://www.navyarmyccu.ltd/

0

Thanks for sharing, I found a lot of interesting information here. A really good post, very thankful and helpful. https://www.mycardstatement.pro/

0

Hello Everyone, I have also faced this problem for quite along. But after visiting https://www.exactlly.com/blog/index.php/discarding-payroll-complexities-with-proven-methods/ I came to know more things related to Javascript. But I wanted to learn more things. So after researching I landed on this forum and it's a great thing for me. So now I can learn more things related to my topics.