fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void swap(char a[], char b[]){
  4. char tmp;
  5. for(int i=0; i<8; i++){
  6. tmp = a[i],a[i] = b[i], b[i] = tmp;
  7. }
  8. }
  9.  
  10. void compare2(char c[],char d[]){
  11. for(int i=2; i<8; i++){
  12. if(c[i]-d[i]<0){
  13. swap(c,d);
  14. return;
  15. }
  16. else return;
  17. }
  18. }
  19.  
  20. int order(char c){
  21. if(c == 'x') return -1;
  22. return (int)c;
  23. }
  24.  
  25. void BubleSort(char a[][8], int n){
  26. for(int i=0; i<n-1; i++){
  27. for(int j=1; j<n-i; j++){
  28. if(order(a[j][1]) == order(a[j-1][1])){
  29. compare2(a[j], a[j-1]);
  30. }
  31. else if(order(a[j][1]) - order(a[j-1][1]) < 0){
  32. swap(a[j], a[j-1]);
  33. }
  34. }
  35. }
  36. }
  37.  
  38.  
  39. int main(void) {
  40. int n;
  41. scanf("%d" ,&n);
  42. char (*v)[8] = malloc(sizeof(char[8])*n);
  43.  
  44. for(int j=0; j<n; j++){
  45.  
  46. scanf("%s" ,v[j]);
  47. }
  48. BubleSort(v,n);
  49. for(int i=0; i<n; i++){
  50. printf("%s\n", v[i]);
  51. }
  52. free(v);
  53. return 0;
  54. }
Success #stdin #stdout 0s 5308KB
stdin
3
j025083
jx25046
j125394
stdout
jx25046
j025083
j125394