#include<stdio.h> #include<string.h> typedef struct { char *mem; char len; }m_table_t; int table_cmp(m_table_t *p1, m_table_t *p2) { int ret; if(p1->len == p2->len) return memcmp(p1->mem, p2->mem, p1->len); if(p1->len > p2->len) ret = memcmp(p1->mem, p2->mem, p2->len); else ret = memcmp(p1->mem, p2->mem, p1->len); if(0 == ret) return p1->len - p2->len; return ret; } void sort(m_table_t *a, int num) { int i,j; m_table_t temp; for(i=0;i<num;i++) { for(j=0;j<num;j++) { if( table_cmp(&a[j], &a[j+1] ) > 0) { memcpy( &temp, &a[j], sizeof(m_table_t) ); memcpy( &a[j], &a[j+1], sizeof(m_table_t) ); memcpy( &a[j+1], &temp, sizeof(m_table_t) ); } } } } void quickSort(m_table_t *s, int l, int r) { int i, j; m_table_t x; if (l < r) { i = l; j = r; memcpy(&x, &s[i], sizeof(m_table_t) ); while (i < j) { while(i < j && (table_cmp(&s[j], &x) > 0)) j--; if(i < j) { memcpy( &s[i++], &s[j], sizeof(m_table_t) ); } while(i < j && (table_cmp(&x, &s[i]) > 0 )) i++; if(i < j) { memcpy(&s[j--], &s[i], sizeof(m_table_t) ); } } memcpy(&s[i], &x, sizeof(m_table_t) ); quickSort(s, l, i - 1); quickSort(s, i + 1, r); } } int str2table(m_table_t *table, char *str, int *index) { int i,w; *index = 0; for(i=0,w= -1; i<strlen(str); i++) { if(' ' != *(str+i) ) { if( (-1 == w) ) { w = i;//start } } else { if(-1 != w)//stop { table[*index].mem = str+w; table[*index].len = i - w; (*index)++; w = -1; } } } if(-1 != w) { table[*index].mem = str+w; table[*index].len = i - w; (*index)++; w = -1; } return 0; } void display(m_table_t *table, int index) { int i; char tmp[15] = {0}; for(i=0; i<index; i++) { memset(tmp, 0, 15); memcpy(tmp, table[i].mem, table[i].len); printf("%s\n", tmp); } return; } int main(void ) { int index; char input[100] = {0}; m_table_t g_table[15]= {0}; gets(input); str2table(&g_table[0], input, &index); quickSort(&g_table[0], 0, index-1);//sort(&g_table[0], index-1); display(&g_table[0], index); return 0; }