i want to ascending sort a word through bubble sort. Suppose i give
kolkata and the output will be like aakllot. i.e. the result. plz give
me full program code. i am beginner. plz help me.
/* BEGIN new.c */
#include <stdio.h>
#include <string.h>
void b_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
int comparison(const void *a, const void *b);
int main(void)
{
char s[] = "kolkata";
puts(s);
b_sort(s, sizeof s - 1, sizeof *s, comparison);
puts(s);
return 0;
}
int comparison(const void *a, const void *b)
{
const char *const letters = "abcdefghijklmnopqrstuvwxyz";
int aa = *(char *)a;
int bb = *(char *)b;
return (int)(strchr(letters, aa) - strchr(letters, bb));
}
void b_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
unsigned char *last, *next, *middle;
unsigned char *p1, *p2, *end, swap;
if (nmemb-- > 1) {
last = base;
last += nmemb * size;
do {
middle = next = base;
do {
if (compar(middle, middle + size) > 0) {
p1 = middle;
p2 = middle + size;
end = p2 + size;
do {
swap = *p1;
*p1++ = *p2;
*p2++ = swap;
} while (p2 != end);
next = middle;
}
middle += size;
} while (middle != last);
last = next;
} while (last != base);
}
}
/* END new.c */