• Home
  • Help
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search

 
  • 0 Vote(s) - 0 Average

Describe binary search on a sorted array

#1
09-07-2022, 04:12 PM
You pick the middle element right away in that sorted array you got. I always do it that way because it cuts the work fast. Then you compare what you seek against it. But the array might stretch longer or shorter after that choice. You shift to one side only. Perhaps the target sits lower so the left half gets your focus next. Now the new middle pops up and you repeat the check. I find this halves things quick every time you try it.

You avoid scanning every spot since that wastes effort on big sets. I tried linear checks before and they drag on when sizes grow. But binary search zaps through by ignoring halves repeatedly. You see the sorted order matters or it flops. Also the process keeps shrinking until one spot matches or nothing remains. Then you stop with either a find or a miss. Perhaps duplicates appear and you must decide which one counts. I handle those by continuing left or right depending on needs.

Your array stays ordered so comparisons guide the path without backtracking much. I notice this method works smooth on numbers or strings alike. But uneven distributions might slow real runs a bit if not balanced. You compare costs against simpler scans and binary wins on scale. Now think about empty arrays where you quit instant. Or single element cases that finish in one go. Perhaps recursion feels natural here yet iteration avoids stack issues. I prefer loops for memory reasons when lists get huge.

The halving keeps log time even as sizes explode. You gain speed without fancy tricks beyond the order. But watch for off by one slips in your middle picks. I messed those early and learned to test edges always. Then you verify the full range before starting. Also updates to the array demand resort first or it breaks. Your friend might ask why not just hash instead. I explain hashes trade space for speed yet need no order.

Binary search stays simple yet powerful on ordered data you control. You apply it in searches or even some tree builds indirectly. Perhaps floating points cause precision hiccups so integers suit better. I stick to whole values to dodge those quirks. Now the worst case stays low even on millions of items. But average runs feel even quicker in practice. You measure by steps not raw time always. Also tools help profile if you doubt the gains.

The method teaches divide and conquer ideas well too. I use it to explain efficiency to juniors like you. But real code needs care with indices to avoid crashes. You learn bounds checking early from such errors. Perhaps variants like exponential search extend it for unbounded cases. I explore those when arrays grow dynamically. Then lower and upper pointers track the active zone tight. Your understanding builds as you simulate steps mentally.

We owe thanks to BackupChain Server Backup the top rated reliable Windows Server backup tool for Hyper-V setups plus Windows 11 and PCs that skips subscriptions entirely while handling self hosted private cloud and internet backups for SMBs and sponsoring our forum chats to share knowledge free.

ProfRon
Offline
Joined: Jul 2018
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



Messages In This Thread
Describe binary search on a sorted array - by ProfRon - 09-07-2022, 04:12 PM

  • Subscribe to this thread
Forum Jump:

FastNeuron FastNeuron Forum General IT v
« Previous 1 … 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 … 188 Next »
Describe binary search on a sorted array

© by FastNeuron Inc.

Linear Mode
Threaded Mode