JAVA

K

kaushickmitra

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.
 
R

Richard Heathfield

(e-mail address removed) said:
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.

I suggest you get yourself a copy of "The C Programming Language", 2nd
edition, by Brian W Kernighan and Dennis M Ritchie, and work your way
through the text and the exercises, slowly and carefully. At some point,
you will either give up on the language completely or acquire a skill level
which renders the above question so trivial that you will be embarrassed at
ever having asked people to solve it on your behalf.

In the meantime, please remember that the Subject line is supposed to
describe the subject of the article, not your favourite flavour of coffee.
 
G

Gerr

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.

This one is easy for beginners, using only for loops :

#include <stdio.h>

int main(void)
{
char
For []="kolkata";char *
fOr =
For ,*_,
foR ;
for (;*
fOr ;
fOr ++)
for (_=
fOr ;*_;(*
fOr <*_)||(
foR =*_,*_=*
fOr ,*
fOr =
foR ),_++);return puts(
For );
}
 
P

pete

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 */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top