merge two sorted arrays in c – dead clever, innit
intro
Oi, you lot! New to C and fancy summat a bit tasty? We’re gonna sling together a lil program to mash two sorted arrays into one big sorted one—ya know, like mixing two queues at the bar into one tidy line. It’s dead simple but proper smart—like when I was tryna merge my mates’ pint orders into one round (messed it up, too smashed). 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:
void mergeArrays(int arr1[], int n1, int arr2[], int n2, int merged[]) {
int i=0,j=0,k=0; // pointers, innit
while(i
merged[k]=arr1[i];
i++; // move up first
}
else { // second one’s smaller
merged[k]=arr2[j];
j++; // move up second
}
k++; // move up merged
}
while(i
i++;
k++;
}
while(j
j++;
k++;
}
}
int main() { // nearly did void, ha!
int arr1[]={1,3,5}; // first sorted lot
int arr2[]={2,4,6}; // second sorted lot
int n1=3,n2=3; // sizes, innit
int merged[6]; // big enough for both
mergeArrays(arr1,n1,arr2,n2,merged); // mash em
printf("merged array: ");
for(int i=0;i
}
printf("\n"); // forgot ; once, ugh!
return 0;
}
what ya get
Here’s what it chucks out when ya run it. I hard-coded two lil sorted arrays—worked bang on, 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 arr1[]={1,3,5};` and pals
These are my lil buckets. `arr1` and `arr2` are the sorted arrays—hard-coded with `{1,3,5}` and `{2,4,6}`. `n1=3` and `n2=3` are their sizes, `merged[6]` is the big pot for the mash-up. All `int` coz we’re keeping it simple!
4. `mergeArrays` funtion 5. chucking it out: `printf` and loop 6. `return 0` to scarper And that’s yer lot! A cracking lil program to merge two sorted arrays. Faff with it—chuck in daft sorted numbers, tweak it to say “mashed up” if ya fancy. First time I ran this, I mixed 1,2 with 3 and got 1,2,3—dead chuffed, eh! You’re a star already, mate—keep smashing it! 😊
Here’s the guts! `mergeArrays(int arr1[], int n1, int arr2[], int n2, int merged[])` takes the two arrays, their sizes, and the merged one. `i`, `j`, `k` are pointers—`i` for `arr1`, `j` for `arr2`, `k` for `merged`. First `while(i
`mergeArrays(arr1,n1,arr2,n2,merged)` does the mash, then `printf("merged array: ")` sets it up. A `for` loop from 0 to `n1+n2` prints each `merged[i]` with a space, `\n` tidies it. 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.