sort an array with bubble sort in c – proper messy fun, innit
intro
Oi, you lot! New to C and fancy summat a bit tasty? We’re gonna sling together a lil program to sort an array using bubble sort—ya know, that daft method where numbers bubble up to the right spot. It’s dead simple but proper knackered—like when I was tryna line up my mates by height after a few pints (total chaos). Great for getting your head round loops and 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 pig’s ear on your blog. take a gander:
int main() { // nearly did void, ha!
int arr[10],n,i,j,temp; // 10’s enough, innit
printf("how many numbers ya want: ");
scanf("%d",&n);
if(n<=0||n>10) { // keep it sensible, ya muppet
printf("oi, gimme a number between 1 and 10!\n");
}
else {
printf("gimme %d numbers: ",n);
for(i=0;i
}
printf("before sort: ");
for(i=0;i
}
printf("\n");
for(i=0;i
temp=arr[j]; // stash it
arr[j]=arr[j+1]; // flip em
arr[j+1]=temp; // chuck it back
}
}
}
printf("after sort: ");
for(i=0;i
}
printf("\n");
}
return 0;
}
what ya get
Here’s what it chucks out when ya run it. I lobbed in some numbers—worked bang on, mate!
gimme 5 numbers: 64 34 25 12 22
before sort: 64 34 25 12 22
after sort: 12 22 25 34 64
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 crisps, 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 suss it, pinky swear!
1. that `#include
This is me nicking my toolkit before we start. `
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 plonker!
3. boxes: `int arr[10],n,i,j,temp;`
These are my lil buckets. `arr[10]` is a big array pot—10 numbers max, plenty for a quick go. `n` is how many ya want, `i` and `j` for looping, `temp` for swapping. No faffy floats, just integers!
4. chatting at ya: `printf` and `scanf`
So yeah, `printf("how many numbers ya want: ")` is me barking, “Oi, how many, quick!” Then `scanf("%d",&n)` grabs it. If `n<=0||n>10`, it’s “oi, keep it between 1 and 10!” Else, `printf("gimme %d numbers: ")` and a `for` loop nabs em into `arr`. `%d` for integers, `&` like a post-it saying “bung it here, ya twit!”
5. show em first
`printf("before sort: ")` and a `for` loop spits the array out—`%d ` with a space keeps it neat, `\n` for a new line. Shows the mess before we tidy it!
6. bubble malarky: `for(i=0;i
Here’s the guts! `for(i=0;i
7. showing off: `printf`
`printf("after sort: ")` and another `for` loop blurts the sorted array. `%d ` for each number, `\n` to finish. Forgot a `;` once—CODE JUST DIED, and I was proper raging!
8. `return 0` to leg it
That `return 0;` is me scarpering, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased pig.
And that’s yer lot! A cracking lil program to sort an array with bubble sort. Faff with it—chuck in daft numbers, tweak it to say “tidied up” if ya fancy. First time I ran this, I chucked in 1 2 3 and it stayed the same—duh, already sorted! You’re a star already, mate—keep smashing it! 😊