- jcodeeInactive forumer
- Posts : 3
Join date : 2009-11-06
Age : 46
Home Address : AUH, United Arab Emirates
Shaker Sort ANSI C
Tue Dec 08, 2009 4:52 pm
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void shaker(char *npts, int count);
int main(void)
{
char s[255];
printf("Enter a string:");
gets(s);
shaker(s, strlen(s));
printf("Sorted: %s.\n", s);
return 0;
}
void shaker(char *npts, int count) {
register int i;
int exchange;
char t;
do {
exchange = 0;
for(i = count - 1; i > 0; --i) {
if(npts[i - 1] > npts[ i ]) {
t = npts[i - 1];
npts[i - 1] = npts[ i ];
npts[ i ] = t;
exchange = 1;
}
}
for(i = 1; i < count; ++i) {
if(npts[i - 1] > npts[ i ]) {
t = npts[i-1];
npts[i - 1] = npts[ i ];
npts[ i ] = t;
exchange = 1;
}
}
} while(exchange); /* sort until no exchanges */
}
#include <stdio.h>
#include <stdlib.h>
void shaker(char *npts, int count);
int main(void)
{
char s[255];
printf("Enter a string:");
gets(s);
shaker(s, strlen(s));
printf("Sorted: %s.\n", s);
return 0;
}
void shaker(char *npts, int count) {
register int i;
int exchange;
char t;
do {
exchange = 0;
for(i = count - 1; i > 0; --i) {
if(npts[i - 1] > npts[ i ]) {
t = npts[i - 1];
npts[i - 1] = npts[ i ];
npts[ i ] = t;
exchange = 1;
}
}
for(i = 1; i < count; ++i) {
if(npts[i - 1] > npts[ i ]) {
t = npts[i-1];
npts[i - 1] = npts[ i ];
npts[ i ] = t;
exchange = 1;
}
}
} while(exchange); /* sort until no exchanges */
}
Permissions in this forum:
You cannot reply to topics in this forum