Mega Code Archive

 
Categories / C / Code Snippets
 

The Selection Sort

#include <string.h> #include <stdio.h> #include <stdlib.h> void select(char *items, int count); int main(void) { char s[255]; printf("Enter a string:"); gets(s); select(s, strlen(s)); printf("The sorted string is: %s.\n", s); return 0; } void select(char *items, int count) { register int x, y, z; int exchange; char t; for(x = 0; x < count-1; ++x) { exchange = 0; z = x; t = items[ x ]; for(y = x + 1; y < count; ++y) { if(items[ y ] < t) { z = y; t = items[ y ]; exchange = 1; } } if(exchange) { items[ z ] = items[ x ]; items[ x ] = t; } } }