binary search on sorted array in c – dead smart, innit
intro
Oi, you lot! New to C and fancy summat a bit clever? We’re gonna sling together a lil program to do a binary search on a sorted array—ya know, that trick where ya chop the list in half til ya find yer number, like guessing a mate’s pint count in a flash. It’s dead quick but proper nifty—like when I was tryna find my lost fiver in a stack of notes (found it, chuffed). Great for getting your head round arrays in C without a right faff. Let’s have a butcher’s!
code bit
Here’s the code—nowt posh, just a quick job I chucked together. Shoved it in a box so it don’t look like a total shambles on your blog. take a gander:
int binarySearch(int arr[], int size, int key) {
int left=0,right=size-1,mid; // pointers, innit
while(left<=right) { // keep chopping
mid=(left+right)/2; // smack in the middle
if(arr[mid]==key) { // bang on!
return mid; // found it
}
if(arr[mid]
}
else { // too high
right=mid-1; // chop right half
}
}
return -1; // ain’t there
}
int main() { // nearly did void, ha!
int arr[]={2,4,6,8,10}; // sorted lot
int size=5,key; // size and what to find
printf("gimme a number to find: ");
scanf("%d",&key);
int spot=binarySearch(arr,size,key); // hunt it
if(spot==-1) {
printf("%d ain’t in there, mate!\n",key);
}
else {
printf("%d’s at spot %d\n",key,spot); // forgot ; once, ugh!
}
return 0;
}
what ya get
Here’s what it chucks out when ya run it. I hard-coded a sorted array and tested a couple—worked bang on, mate!
6’s at spot 2
gimme a number to find: 7
7 ain’t in there, mate!
how it works, kinda
Right, let’s have a natter bout this like we’re slumped at the boozer with a pint—or a bag of pork scratchings, coz I’m Hank Marvin again. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a doddle once ya clock it, swear down!
1. that `#include
This is me nicking my toolkit. `
2. `int main()` funtion
Gotta have a “main” to kick off—it’s like the door to my local. `int` means I’m lobbing a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a right numpty!
3. boxes: `int arr[]={2,4,6,8,10};` and pals
These are my lil buckets. `arr[]={2,4,6,8,10}` is the sorted array—hard-coded for a quick go. `size=5` is how many, `key` is what we’re hunting. All `int` coz we’re keeping it simple!
4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme a number to find: ")` is me barking, “Oi, number, sharpish!” Then `scanf("%d",&key)` grabs what ya chuck in and shoves it in `key`. `%d` for integers, `&` like a post-it saying “bung it here, ya twit!”
5. `binarySearch` funtion 6. chucking it out: `printf` and check 7. `return 0` to scarper And that’s yer lot! A cracking lil program to binary search a sorted array. Faff with it—chuck in a bigger sorted array, tweak it to say “nicked it” if ya fancy. First time I ran this, I hunted 8 and it nailed it—dead chuffed, eh! You’re a star already, mate—keep smashing it! 😊
Here’s the guts! `binarySearch(int arr[], int size, int key)` takes the array, its size, and the number to find. `left=0`, `right=size-1` set the ends, `mid` is the middle. `while(left<=right)` loops: `mid=(left+right)/2` picks the halfway spot. If `arr[mid]==key`, bingo, return `mid`. If `arr[mid]
`int spot=binarySearch(arr,size,key)` hunts it, then if `spot==-1`, `printf` says “ain’t there!” Else, it says where it’s at with `spot`. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!
That `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.