Advertisement
ElfikCo

Sort Kubelk - doesn't work

Jun 13th, 2019
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. struct lista{
  5. struct lista *next;
  6. float key;
  7. };
  8.  
  9. int dlugosc_T(float * A);
  10. int dlugosc_L(struct lista * head);
  11. void lista_dodaj(struct lista * head, struct lista * nowy);
  12. struct lista * sortwstaw(struct lista * head);
  13. float * sort_kubelkowe(float * A);
  14.  
  15. int main(){
  16. int i;
  17. float tablica[] = {0.23, 0.12, 0.56, 0.97, 0.91, 0.54, 0, 0.34, 0.35, -1};
  18. float * tab = tablica;
  19. tab = sort_kubelkowe(tablica);
  20.  
  21. for(i = 0; i < 9 ; i++){
  22. printf("%f\n", tab[i]);
  23. }
  24.  
  25. getchar();
  26. getchar();
  27. return 1;
  28. }
  29.  
  30. int dlugosc_T(float * A){
  31. int i;
  32. for(i = 0; ; i++){
  33. if(A[i] == -1) break;
  34. }
  35. return i + 1;
  36. }
  37.  
  38. int dlugosc_L(struct lista * head){
  39. int i = 0;
  40. struct lista * x = head;
  41.  
  42. while(x != NULL){
  43. i++;
  44. x = x -> next;
  45. }
  46. return i;
  47. }
  48.  
  49. void lista_dodaj(struct lista * head, struct lista * nowy){
  50. struct lista * x = head;
  51. struct lista * n = calloc(1, sizeof(struct lista));
  52.  
  53. n -> key = nowy -> key;
  54.  
  55. if(head == NULL){
  56. head = n;
  57. return;
  58. }
  59.  
  60. while(x != NULL || x -> next != NULL)
  61. x = x -> next;
  62.  
  63. x -> next = n;
  64. }
  65.  
  66. struct lista * sortwstaw(struct lista * head){
  67. int i = 0, j;
  68. int n = dlugosc_L(head);
  69. struct lista * x = head -> next, *y = NULL, *z = NULL ;
  70. struct lista * nowa = calloc(1, sizeof(struct lista));
  71. lista_dodaj(nowa, head);
  72. y = nowa; z = nowa;
  73.  
  74. while(x != NULL){
  75. if(y -> key < x -> key && (y -> next -> key > x -> key || y -> next == NULL)){
  76. x -> next = y -> next;
  77. y -> next = x;
  78. y = z;
  79. }
  80. else if(y -> key > x -> key){
  81. x -> next = y;
  82. y = x;
  83. z = y;
  84. }
  85. x = x -> next;
  86. }
  87.  
  88. return nowa;
  89. }
  90.  
  91. float * sort_kubelkowe(float * A){
  92. int n = dlugosc_T(A);
  93. int i;
  94. float * W = calloc(n, sizeof(float));
  95. struct lista ** B = calloc(10, sizeof(struct lista *));
  96. struct lista * C = B;
  97. struct lista * M = calloc(1, sizeof(struct lista *));
  98.  
  99. for(i = 0; i < n; i++){
  100. M -> key = A[i];
  101. n = floor(10 * A[i]);
  102. lista_dodaj(B[n], M);
  103. }
  104. for(i = 0; i< n - 1; i++){
  105. B[i] = sortwstaw(B[i]);
  106. }
  107.  
  108. for(i = 0; i < 10; i++){
  109. while(B[i] != NULL){
  110. W[i] += B[i] -> key;
  111. B[i] = B[i] -> next;
  112. }
  113. }
  114. return W;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement