For Programmers: Free Programming Magazines  


Home > Archive > C > December 2005 > JAVA









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author JAVA
kaushickmitra@gmail.com

2005-12-15, 3:55 am

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.

Richard Heathfield

2005-12-15, 3:55 am

kaushickmitra@gmail.com 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Gerr

2005-12-15, 7:55 am


> 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 );
}

Richard Heathfield

2005-12-15, 7:55 am

Gerr said:

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


Delightful!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
pete

2005-12-15, 7:55 am

kaushickmitra@gmail.com wrote:
>
> 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 */


--
pete
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com