Skip to content

Instantly share code, notes, and snippets.

@caotic123
Last active May 24, 2018 15:01
Show Gist options
  • Save caotic123/f93d792febb4aa8c3637287eff8f5a0d to your computer and use it in GitHub Desktop.
Save caotic123/f93d792febb4aa8c3637287eff8f5a0d to your computer and use it in GitHub Desktop.
Two Algorithms of sort vectors : a macro version of selection sort and bubble sort.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//selection sort
#define xsort_vec(x__, t_, n, f_, p) {\
int n_ = n - 1;\
int i_ = 0;\
int y__ = 0;\
t_ t__;\
t_ buffer;\
while (i_ <= n_) {\
t__ = * (t_ * ) x__[i_]; \
for (int s__ = i_; s__ <= n_; s__++) {\
if (!f_( * (t_ * ) x__[s__], t__) || * (t_ * ) x__[s__] == t__) {\
y__ = s__;\
memcpy(&t__, ((t_ * ) x__) + s__, sizeof(t_)); \
}\
}\
memcpy(&buffer, ((t_ * ) x__) + i_, sizeof(t_)); \
memcpy(((t_ * ) x__)+i_, ((t_ * ) x__) + y__, sizeof(t_)); \
memcpy(((t_ * ) x__)+y__, &buffer, sizeof(t_)); \
i_++;\
}\
}\
x__
void* bubl_sort(void** p, int len, int _, int i, void* func) { // bubble sort
void* __ = malloc(len);
if ( (((int(*)(void*, void*)) func))((void*)((__intptr_t)p + (i*len)), (void*)((__intptr_t)p + ((i+1)*len)))){
memcpy(__, (void*)((__intptr_t)p + (i*len)), len); //se sim troca
memcpy((void*)((__intptr_t)p + (i*len)), (void*)((__intptr_t)p + ((i+1)*len)), len);
memcpy((void*)((__intptr_t)p + ((i+1)*len)), __, len);
}
if (i >= (_-1)) {return (_ <= 0) ? p : bubl_sort(p, len, _-1, 0, func);}
return bubl_sort(p, len, _, i+1, func);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment