//=========================================
//buck sort 5~4-7-26a 1v tested works
//=========================================
//
//----------------------------------------
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//----------------------------------------
#define STORE 500
#define SIZELINE 100
#define NOLINE 100
#define TESTSTR " Now is the winter of our discontent Made glorious \n summer by this sun of York; And all the clouds that \n lour'd upon our house"
#define ZERO 0
#define ONE 1
#define MINCHAR ' '
#define MAXCHAR '~'
#define DELim "',; ./\t\n"
//----------------------------------------
//
void labelformat(int j, char result[]){
//labelformat(j, result);
char label[30];
sprintf(label, "~ :%c:", ' ' + j);
strcat(result, label);
strcat(result, "\n");
}
//----------------------------------------
void bucksort(char str[], char del[], char result[]){
//bucksort(str, del, result);
char *token;
char buck[NOLINE][SIZELINE];
// Enough space for concatenated
// tokens per bucket
//note in english
//words 4.7 to 5.2 letters
//lines 50 to 75 characters(including spaces)
//---------------------------------------
// Initialize buckets to empty strings
for (int i = ZERO; i < MAXCHAR-MINCHAR; i++)
buck[i][0] = '\0';
//---------------------------------------
// Tokenize input string
token = strtok(str, del);
while (token != NULL) {
// Concatenate token and space to the correct bucket
// Convert first character for indexing
char c = token[0];
int idx = c - MINCHAR;
if (c >=MINCHAR && c <= MAXCHAR) {
//cover ascii ' ' to '~'
}
else if (c < MINCHAR){
//all lower char values
idx = ZERO;
}
else if (c > MAXCHAR){
//all upper char values
idx = MAXCHAR - MINCHAR;
}
strcat(buck[idx], token);
strcat(buck[idx], " ");
token = strtok(NULL, del);
}
//---------------------------------------
// 2D 'buck' array into a 1D 'result'
result[0] = '\0'; // Ensure result
//is empty initially
for (int j = ZERO; j < MAXCHAR-MINCHAR; j++) {
if (strlen(buck[j]) > 0) {
strcat(result, buck[j]);
labelformat(j, result);
}
}
}
//----------------------------------------
int main() {
char str[] = TESTSTR ;
char result[STORE] = ""; // Ensure sufficient
// space
char del[10] = DELim ;
printf("%s \n", str);
bucksort(str, del, result);
printf("%s", result);
return 0;
//----------------------------------------
}
//
//---------------------------------------
//char c = toupper(token[0]);
//if (c >= 'A' && c <= 'Z') {
//int index = c - 'A';
//char buck[100][1000];
//---------------------------------------
To embed this project on your website, copy the following code and paste it into your website's HTML: