Without help, write a bubble sort in C

Without help, write a bubble sort in C.

Attached: Capture+_2018-11-22-14-12-55.png (583x262, 21K)

Other urls found in this thread:

en.cppreference.com/w/cpp/algorithm/is_sorted
youtube.com/watch?v=F75vDo2Wx-E
stackoverflow.com/a/1810295
twitter.com/AnonBabble

I don't know how

brainlet

>He can't into homework assignment. Do something else with your life brainlet.

def bubblesort(lst):
while not (lst[0] == min(lst) and lst[-1] == max(lst)):
for i in range(len(lst) - 1):
if lst[i] > lst[i + 1]:
lst[i], lst[i + 1] = lst[i + 1], lst[i]
return lst

There's C underneath. What do I win?

You just did some guys homework for him. Probably an intro class to. How is he suppose to learn?

i'd have to look up what bubble sort is since i never filled my head with terrible algos
this won't sort anything that begins with the lowest element and ends with the greatest element

see >this won't sort anything that begins with the lowest element and ends with the greatest element

That's his todo.

t. the great educator

> write a bubble sort in C
Do my homework
> Without help
Don't copy it from stackoverflow because it will be obvious

based and mentorpilled

OP I hope you don't have trouble with such an easy task. If it at least was Quicksort (which isn't difficult either) but Bubblesort.. come on. You would solve this task by yourself if you had any dignity and self-respect.

Here you go:
>>,[>>,]>+]++>>>[[

>I can't do it but I don't want to admit it

I already did it brainlet, I just want to see other anons now.

Attached: Capture+_2018-11-22-14-33-21.png (598x319, 25K)

why the fuck would I ever want to use bubble sort
here's heap sort
static void
swap(int *arr, int i1, int i2)
{
int tmp = arr[i1];
arr[i1] = arr[i2];
arr[i2] = tmp;
}

static void
bubble_down(int *arr, int len, int p)
{
int i, i1, i2;
for (;;) {
i1 = p * 2 + 1;
i2 = p * 2 + 2;
if (i1 >= len)
break;
if (i2 >= len)
i = i1;
else
i = arr[i1] > arr[i2] ? i1 : i2;
if (arr[p] >= arr[i])
break;
swap(arr, p, i);
p = i;
}
}

void
heapify(int *arr, int len)
{
for (int i = len / 2; i >= 0; i--)
bubble_down(arr, len, i);
}

void
heapsort_pop(int *arr, int len)
{
swap(arr, 0, len - 1);
bubble_down(arr, len - 1, 0);
}

void
heapsort(int *arr, int len)
{
heapify(arr, len);
while (len > 1)
heapsort_pop(arr, len--);
}

Holy shit, you are fags.

It's a common code, I could look it up easily and change it a little if I needed it for some assignment.

Here's mine, now show me yours.

Attached: Capture+_2018-11-22-14-36-29.png (1440x2039, 232K)

>Implying you didn't steal that very same version

wow that's really crappy, cleaned up version of the sorting loop:
for (k = n - 1; k > 1; k--) {
for (j = 0; j < k - 1; j++) {
if (a[j] < a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}

how could you ever come up with that noise

>enter 1th value

void bubblesort(int *array, size_t n)
{
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n-1; j++) {
if (array[j] > array[j+1]) {
int tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
}
}
}

I could have used modulus operator and switch case to check the final digit and choose the proper suffix but I didn't feel like it.

I know it's messy. Did it on my phone while trying to pay attention to lectures. Thanks for your input.

not making your homework nigguh

Here OP, from my competitive C++ solutions archive.

Attached: screen.png (1224x738, 102K)

Why re-implement is_sorted?
en.cppreference.com/w/cpp/algorithm/is_sorted

Because my use case is optimised and minimalistique

wow fantastic that's a pass-by-value (O(n)) is_sorted (O(n)) in permutations (O(n!))
Overall O(n*n!)
F--, don't see me after class.

ahahahahaha
anyway, that comment... is that for real? why would this make a name collision?

It's a joke bro

I've been playing EXAPUNKS lately. r8
GRAB 301

MARK BUBBLESORT
TEST EOF
TJMP CHECK
SEEK -1

TEST F > F
TJMP SWAP
FJMP BUBBLESORT

MARK SWAP
SEEK -1
COPY F T
SEEK -2
COPY F F
SEEK -2
COPY T F
COPY 1 X
COPY 0 T
FJMP BUBBLESORT


MARK CHECK
TEST X = 1
COPY 0 X
SEEK -9999
TJMP BUBBLESORT

MARK END
DROP
HALT

Attached: 1523643518788.png (735x195, 20K)

I wish exapunks had online hack matches.

Wanter to post my TIS-100 solution for sequence sorter but turned out I don't have any, shame on me.

People do tournaments where they submit their solution files and then battle out.

Would be way better if I could just queue up and get matched with somebody ranked similarly to me, and we both write our solutions and watch them play out right then.

Clean up your code, son.

Attached: 181010-sommer-peterson-hero_gnzsch.jpg (1480x833, 122K)

That wouldn't work since you wouldn't be able to test the code until both parties are finished. It'd be cool if I could submit my solution to an automated server where it'd battle other players automatically until it hits a top where it can't win any more. Then I'd see the matches, see what went wrong and improve my code, then start again.

>you wouldn't be able to test the code until both parties are finished.
Yeah exactly. You both write your first edition and battle them out, then you modify it and do a new round, etc.

Okay until a top nigger like me writes his own optimising C -> ASM compiler and BTFO's you

I once discovered during a time programming contest that I could do bubble sort on a linked list. Something like...

for (node *p = head; p; p=p->next) {
for (node *q = p->next; q; q = q->next) {
if (q->data.key < p->data.key) {
data_t t = p->data;
p->data = q->data;
q->data = t;
}
}
}

Why do contestants always use the worst fucking naming conventions possible?

for(i=0; i data[i+1]) {
//swap them
}
}

I don't remember what bubble sort means. I know it has something to do with bubbling elements up until the next element is smaller than it but that's about it. I never bothered learning any of those "Learner's sorting algorithms" because I already understand algorithmic complexity.

This. I pity people who waste neurons on memorising shit that's a google search away.

Did you want names like Frodo, Samwise, Meriadoc and Piippin? Or a dozen Hungarian warts?
p and q were the single-character conventions for index pointers in the textbook back when I took data structures in college, just like i and j for loops. Except unlike most of the CS grads these days, I actually paid attention during the class.

If you write an optimizing C to assembly compiler just for exapunks battles, you deserve to win

But also I'm not going to rematch you because you'd be boring to play against.

>Remember

I'm literally a noob and I did it as a personal project because I heard of bubble sorting before and figured I know enough to do it.

>It's too easy I won't do it
Okay, armchair hax0rz

>It's too easy I won't do it
That wasn't said, Mr. Sensitive.

But actually, now that you mention it, yeah, it would be a waste of my time. About the equivalent time effort and value of this post, honestly.

>C tards actually believe in constantly reinventing the wheel in their buggy ass software
>they even take pride in it

kek

std::sort(std:begin(values), std::end(values));

Attached: comparison[1].png (650x456, 19K)

It's O(n+n!), which is just O(n!)
F, retake your complexity theory course.

So why is bubble sort slightly more efficient when you run the inner loop backwards?

Attached: 1531810820798.gif (480x287, 1.21M)

Because memory is weird sometimes.

>Because my use case is optimised and minimalistique

You will never code professionally.

>>C tards actually believe in constantly reinventing the wheel in their buggy ass software
>>they even take pride in it
>what is qsort

qsort... is that the laughably slow generic implementation because the way to do generics in C is with void pointers and type sizes?

>qsort

Attached: tenor[1].gif (220x272, 63K)

int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}

Attached: senior-women-feeling-sick-holding-hand-over-her-mouth.jpg (1100x734, 66K)

>He's learning
>reinventing the wheel
The power of autism.

>I need to sort these elements
>I know, I'll just whip up a quick bubble sort routine

99.8% of C developers. The other 0.2% resort to qsort or some kind of macro abomination.

#include
#include
#include

int
main(int argc, char **argv)
{
char delim, **nums, *tmp;
int i, len, count = 0, didSwap;
long a, b;
delim = *argv[1];
len = strlen(argv[2]);
tmp = malloc(len);
nums = malloc(len * sizeof(char*));
nums[count++] = argv[2];
for (i = 0; i < len; i++) {
if (argv[2][i] == delim) {
argv[2][i] = '\0';
nums[count++] = &argv[2][i + 1];
}
}
didSwap = 1;
while (didSwap) {
didSwap = 0;
for (i = 0; i < count - 1; i++) {
a = atol(nums[i]);
b = atol(nums[i+1]);
if (a > b) {
tmp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = tmp;
didSwap = 1;
}
}
}
for (i = 0; i < count - 1; i++) {
printf("%s,", nums[i]);
}
printf("%s\n", nums[count - 1]);
}

that's not how greentext works, retarded newfag

OP, write a qsort for me.

>he can't admit he's wrong
>must changes "facts" to support my argument
Your autism knows no bounds.

Describe it to me and I'll try.

Calling people autistic is not an argument. It's not even a good insult at that.

Says the autist. KEK

for each letter in the alphabet
if a > q then print b else sort based on the distance from the string 'queue'.

>not using qsort
seriously who needs bubble sort in this age and time

It's being called every iteration of the loop so it's n*n!

It's O(n*n), which is just O(n!)
F, retake your complexity theory course.

Who needs types? Life if so much simpler when everything is a void*.

jesus what an idiot

>n! == n*n
Back to elementary school for you.

Attached: 65246643553.jpg (225x225, 7K)

Well you read a shit textbook, your code is unreadable

Hello Samsung.

Attached: enlightenment_logo_1200px.svg.png (1200x1656, 349K)

it is for 1

efl is actually pretty comfy desu
all the hate is bullshit

Especially that shit about OMG STUFF ISNT SHOWN BY DEFAULT XDDD, like literally just use edje and write a mvc wrapper and you no longer have to give a shit.

youtube.com/watch?v=F75vDo2Wx-E
I just leave this here.

It's pretty nice to watch.

Wrote this like 5 years ago when I started programming
void bubble_sort(int array[], int length)
{
int amount_sorted = 0;
do {
amount_sorted = 0;
for (int i = 0; i < length-1; i++) {
if (array[i] > array[i+1]) {
int temp = array[i+1];
array[i+1] = array[i];
array[i] = temp;
amount_sorted++;
}
}
} while (amount_sorted);
}

>void bubble_sort(int *array, int length)

bubble sort is probably shittest algorithm. why not just use a dictionary search then you can do in O(n) time.

what about it

reminder with the bubble sort you only need to sort up to the nth -1 value on the nth iteration

F-

int[] array

stackoverflow.com/a/1810295

fn sort(arr: &mut [i64]) -> &[i64] {
let mut arr = arr;
for i in 0..arr.len() {
for s in (i + 1)..arr.len() {
if arr[s] < arr[i] {
let tmp = arr[i];
arr[i] = arr[s];
arr[s] = tmp;
}
}
}
return arr;
}


get fucked c fags. your code will never be as safe as minbe

>variable definition inside loop

What about it?

what?

why does all of your code look so complicated?
Literally just
for (int i =0; i < array.length-1;i++)
for (int j = i+1;j< array.length;j++){
if (array[j]

they are the same though, just better formatted

Look at all these nested pajeet loops. Bubble sort is true brainlet O(n^2). Come back with something O(n log(n) ) at worst.

so can we call unoptimized bubblesort pajeetsort?

neat

>purpose of thread is to write bubble sort specifically
>why is everyone in this thread writing bubble sort instead of a better one?
This post doesn't make you look smart for knowing better basic sorts, you just look like an idiot with shit reading comprehension.

Why the fuck you want to implement your own? Just search the code on google, and that's it. It's more important that you know which are the scenarios where is better to use bubblesort than others. Your implementation is optional.

$ ./a.out 4 3 2 1 0 -1 -2 -10 20
-10, -2, -1, 0, 1, 2, 3, 4, 20
Generic bubble sort.

Attached: Screenshot_2018-11-22_17-10-29.png (683x769, 67K)

you mean the language? I recall hearing something similar by some facebook guy about the 'if it looks like a declaration, treat it like one' principle and parentheses being optional somewhere

I like your honesty, you're hired

>greets everyone every morning
>says lets do our best today
>adds useless comments to source code all day

Just use bucket sort