Ladies_Man

#SORT__Count & Radix ( Подсчет сравнений)

Dec 15th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char src[100];
  6. char dest[100];
  7. int count[100] = {0};
  8.  
  9. int compare (char *str, int a, int b)
  10. {
  11.     int i, j;
  12.  
  13.     for (i = 0; i + a + 1 < strlen(str) && ((str[i + 1 + a] != ' ') && (str[i + 1 + a] != 9)); i++) continue;
  14.     for (j = 0; j + b + 1 < strlen(str) && ((str[j + 1 + b] != ' ') && (str[i + 1 + a] != 9)); j++) continue;
  15.  
  16.     if (i > j) return 1;
  17.     else return 0;
  18. }
  19.  
  20. void restoration (int array[], int k, char *src)
  21. {
  22.     int i, j, m = 0;
  23.     for (i = 0; i < k; i++) {
  24.         for (j = 0; j + array[i] < strlen(src) && src[j + array[i]] != ' '; j++) {
  25.             dest[m] = src[j + array[i]];
  26.             m++;
  27.         }
  28.         dest[m] = ' ';
  29.         m++;
  30.     }
  31.     dest[m] = 0;
  32.     if (dest[strlen(dest) - 1] == ' ')
  33.         dest[strlen(dest)-1] = 0;
  34. }
  35.  
  36. int check (char *str, int i)
  37. {
  38.     if (dest[i] == ' ' && dest[i+1] == ' ') return 1;
  39.     else return 0;
  40. }
  41.  
  42. void radixsort (int array[], int k, char *src)
  43. {
  44.     int i,j;
  45.     for (i = 0; i < k - 1; i++){
  46.         for (j = i + 1; j < k; j++){
  47.             if (compare (src, array[i], array[j]))
  48.                 count[i]++;
  49.             else
  50.                 count[j]++;
  51.         }
  52.     }
  53. }
  54.  
  55. void n_spc (char *dest)
  56. {
  57.     char dest2[100];
  58.     int i, j = 0;
  59.     for (i = 0; i < strlen(dest); i++) {
  60.         if (dest[i] == ' ' && dest[i + 1] == ' ') continue;
  61.         else {
  62.             dest2[j] = dest[i + 1];
  63.             j++;
  64.         }
  65.     }
  66.     dest2[j] = 0;
  67.     printf("%s", dest);
  68. }
  69.  
  70. void csort (char *src, char *dest)
  71. {
  72.     int i, n = 0;
  73.     int array[100];
  74.  
  75.     void word_index (char *str)
  76.     {
  77.         int i = 0;
  78.         for (i = 0; i <= strlen(src); i++) {
  79.             if ((i == 0) || ((src[i] != ' ') && (src[i - 1] == ' '))) {
  80.                 array[n] = i;
  81.                 n++;
  82.             }
  83.         }
  84.     }
  85.  
  86.     word_index (src);//first chars of words
  87.  
  88.     int arr_2[n];//array of indexes of first chars
  89.  
  90.     radixsort (array, n, src);//sort
  91.  
  92.     for (i = 0; i < n; i++) arr_2[count[i]] = array[i];
  93.  
  94.     restoration(arr_2, n, src);//restoration of string
  95. }
  96.  
  97. int main()
  98. {
  99.     int i = 0;
  100.     gets (src);
  101.  
  102.     csort(src, dest);
  103.  
  104.     if (check(dest, i)) n_spc(dest);
  105.     else         printf("%s", dest);
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment