user-0 提问: 大家帮忙看看这道面试题，怎么实现比较好？

回复:
user: user-1
content: 我上面的排序是从小到大的排序，输出的时候可以考虑从最后一个输出。

user: user-1
content: #include <stdio.h> #include <stdlib.h>  typedef struct node {     int num;     int cnt; }num_node;  static int cmp_as_num(const void *a, const void *b) {     return (((num_node *)a)->num - ((num_node *)b)->num); }  static int cmp_as_cnt(const void *a, const void *b) {     return (((num_node *)a)->cnt - ((num_node *)b)->cnt); }  num_node *new_space(num_node *p, int num) {     p = (num_node *)realloc(p, num * sizeof(num_node));     if (!p) {         fprintf(stderr, "malloc error!\n");         exit(-1);     }      return p; }  void show_nums(num_node *p, int num) {     int i;      for (i = 0; i < num; i++)         printf("%d : %d (times)\n", p[i].num, p[i].cnt); }  int init_num_nodes(num_node **p, int *arr, int arr_len) {     int i, j, cnt = 0;     num_node *pnum = NULL;     pnum = new_space(pnum, 1);     pnum->num = arr[0];     pnum->cnt = 1;     cnt = 1;      for (i = 1; i < arr_len; i++) {         for (j = 0; j < cnt; j++) {             if (pnum[j].num == arr[i]) {                 pnum[j].cnt++;                 break;             }         }         if (j >= cnt) {             pnum = new_space(pnum, cnt + 1);             pnum[j].num = arr[i];             pnum[j].cnt = 1;             cnt++;         }     }     *p = pnum;      return cnt; }  int main(void) {     int arr[] = { 1, 3, 2, 5, 9, 0, 7, 2, 2, 4, 6, 5, 1, 6};     int len = sizeof(arr) / sizeof(arr[0]);     num_node *pnum = NULL;     int num_cnt = 0;      num_cnt = init_num_nodes(&pnum, arr, len);     show_nums(pnum, num_cnt);      qsort(pnum, num_cnt, sizeof(num_node), cmp_as_num);     qsort(pnum, num_cnt, sizeof(num_node), cmp_as_cnt);     printf("\nAfter sort: \n");     show_nums(pnum, num_cnt);      return 0; } 参考一下吧～

user: user-2
content: #include <stdio.h> #include <stdlib.h> void sort(int a[],int n) { 	int i,j,tmp; 	for(i=0;i<n-1;i++) 		for(j=0;j<n-i-1;j++) 		{ 			if(a[j]<a[j+1]) 			{ 				tmp=a[j]; 				a[j]=a[j+1]; 				a[j+1]=tmp; 			} 		} } void show(int a[],int n) { 	int i; 	for(i=0;i<n;i++) 		printf("%d  ",a[i]); } void tongji(int a[],int n,int b[][10]) { 	int i,tmp=a[0],c=1; 	for(i=1;i<n;i++) 	{ 		if(tmp==a[i]) 			c++; 		else 		{ 			b[c][tmp]=1; 			printf("%d:%d ",tmp,c); 			tmp=a[i]; 			c=1; 		} 	} } void show2(int b[][10]) { 	int i,j; 	for(i=9;i>=0;i--) 		for(j=9;j>=0;j--) 		{ 			if(b[i][j]==1)printf("%d:%d\n",j,i); 		} } void main() { 	int b[10][10]={0}; 	int a[]={1,3,2,5,9,0,7,2,2,4,6,5,1,6}; 	int len=sizeof(a)/sizeof(*a); 	sort(a,len); 	show(a,len); 	printf("\n"); 	tongji(a,len,b); 	printf("\n"); 	show2(b); 	system("pause"); }

user: user-3
content: 一个0~9的计数排序呀 很简单吧

user: user-4
content: #include <stdio.h> #include <stdlib.h>   void swap(int *a,int *b){     int temp = *a;     *a = *b;     *b = temp; }   typedef struct SortVal{     int value;     int count;     struct SortVal *next; }SortValue;   SortValue *head = NULL;   SortValue *newValue(int value,int count) {     SortValue *ret = (SortValue*)malloc(sizeof(SortValue));     ret->value = value;     ret->count = count;     ret->next = NULL;     return ret; }   void pushSortValue(int value,int count) {//插入排序     if (head == NULL){         head = newValue(value,count);     }     else if (count > head->count){         SortValue *next = head;         head = newValue(value,count);         head->next = next;     }     else {         SortValue *cur = head;         while(cur->next != NULL && count <= cur->next->count){             cur=cur->next;         }          SortValue *next = cur->next;         cur->next = newValue(value,count);         cur->next->next = next;     } }   int main(void) {      int a[] = {1,3,2,5,9,0,7,2,2,4,6,5,1,6};     int i,j;     int length = sizeof(a)/sizeof(int);     int valueCount = 0;     for(i=0;i<length;i++){//选择排序         int maxIndex = i;         for(j=i+1;j<length;j++){             if(a[j] > a[maxIndex]){                 maxIndex = j;             }            }         if (maxIndex != i){             swap(&a[maxIndex],&a[i]);         }         if (i!=0 && a[i] != a[i-1]){             pushSortValue(a[i-1], valueCount);             valueCount = 1;         }         else {             valueCount++;         }         if(i == length - 1){ //刚才有bug 补上             pushSortValue(a[i],valueCount);         }     }           while (head!=NULL){         printf("%d:%d\n",head->value,head->count);         head = head->next;     }     return 0; }

user: user-4
content: #include <stdio.h> #include <stdlib.h>  void swap(int *a,int *b){     int temp = *a;     *a = *b;     *b = temp; }  typedef struct SortVal{     int value;     int count;     struct SortVal *next; }SortValue;  SortValue *head = NULL;  SortValue *newValue(int value,int count) {     SortValue *ret = (SortValue*)malloc(sizeof(SortValue));     ret->value = value;     ret->count = count;     ret->next = NULL;     return ret; }  void pushSortValue(int value,int count) {//插入排序     if (head == NULL){         head = newValue(value,count);     }     else if (count > head->count){         SortValue *next = head;         head = newValue(value,count);         head->next = next;     }     else {         SortValue *cur = head;         while(cur->next != NULL && count <= cur->next->count){             cur=cur->next;         }          SortValue *next = cur->next;         cur->next = newValue(value,count);         cur->next->next = next;     } }  int main(void) {      int a[] = {1,3,2,5,9,0,7,2,2,4,6,5,1,6};     int i,j;     int length = sizeof(a)/sizeof(int);     int valueCount = 0;     for(i=0;i<length-1;i++){//选择排序         int maxIndex = i;         for(j=i+1;j<length;j++){             if(a[j] > a[maxIndex]){                 maxIndex = j;             }            }         if (maxIndex != i){             swap(&a[maxIndex],&a[i]);         }         if (i!=0 && a[i] != a[i-1]){             pushSortValue(a[i-1], valueCount);             valueCount = 1;         }         else {             valueCount++;         }     }          while (head!=NULL){         printf("%d:%d\n",head->value,head->count);         head = head->next;     } 	return 0; }

user: user-1
content: #include <stdio.h> #include <stdlib.h>  typedef struct node {     int num;     int cnt; }num_node;  static int cmp_as_num(const void *a, const void *b) {     return (((num_node *)b)->num - ((num_node *)a)->num); }  static int cmp_as_cnt(const void *a, const void *b) {     return (((num_node *)b)->cnt - ((num_node *)a)->cnt); }  num_node *new_space(num_node *p, int num) {     p = (num_node *)realloc(p, num * sizeof(num_node));     if (!p) {         fprintf(stderr, "malloc error!\n");         exit(-1);     }      return p; }  void show_nums(num_node *p, int num) {     int i;      for (i = 0; i < num; i++)         printf("%d : %d (times)\n", p[i].num, p[i].cnt); }  int init_num_nodes(num_node **p, int *arr, int arr_len) {     int i, j, cnt = 0;     num_node *pnum = NULL;     pnum = new_space(pnum, 1);     pnum->num = arr[0];     pnum->cnt = 1;     cnt = 1;      for (i = 1; i < arr_len; i++) {         for (j = 0; j < cnt; j++) {             if (pnum[j].num == arr[i]) {                 pnum[j].cnt++;                 break;             }         }         if (j >= cnt) {             pnum = new_space(pnum, cnt + 1);             pnum[j].num = arr[i];             pnum[j].cnt = 1;             cnt++;         }     }     *p = pnum;      return cnt; }  int main(void) {     int arr[] = { 1, 3, 2, 5, 9, 0, 7, 2, 2, 4, 6, 5, 1, 6};     int len = sizeof(arr) / sizeof(arr[0]);     num_node *pnum = NULL;     int num_cnt = 0;      num_cnt = init_num_nodes(&pnum, arr, len);     show_nums(pnum, num_cnt);      qsort(pnum, num_cnt, sizeof(num_node), cmp_as_num);     qsort(pnum, num_cnt, sizeof(num_node), cmp_as_cnt);     printf("\nAfter sort: \n");     show_nums(pnum, num_cnt);      return 0; } 改一下两个函数:cmp_as_num, cmp_as_cnt中a和b的位置即可实现从大到小排序，注意两个程序的差异