Advertisement
szmelu

sort

May 31st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <stdlib.h>
  4. #define N 10000
  5. using namespace std;
  6.  
  7. struct node{
  8. node * next;
  9. int value;
  10. };
  11.  
  12.  
  13. void DELL(node *& H){
  14. if(H!=NULL){
  15. node * temp=new node;
  16. temp = H;
  17. H=temp->next;
  18. delete temp;
  19. }
  20. }
  21.  
  22. void ADD( node *& H, int val){
  23. node * temp = new node;
  24. temp->next=H;
  25. temp->value=val;
  26. H=temp;
  27. }
  28.  
  29.  
  30. void show(node * H){
  31. cout <<"H->";
  32. node *temp=H;
  33. while(temp!=NULL){
  34. cout << temp->value<<"->";
  35. temp=temp->next;
  36. }
  37. cout << "NULL"<<endl;
  38. }
  39. void sort(node*&H, node**&tab)
  40. {
  41. if(H!=NULL)
  42. {
  43. node *temp=H;
  44. while(temp->next!=NULL)
  45. {
  46. for(int i=0;i<N/100;i++)
  47. {
  48. if (temp->value <= (i + 1) * 100 && temp->value>i*100)
  49. {
  50. ADD(tab[i], temp->value);
  51. break;
  52. }
  53. }
  54. temp=temp->next;
  55. }
  56. }
  57. }
  58.  
  59. void bubblesort(node*&H)
  60. {
  61. if (H != NULL)
  62. {
  63. int i = 0;
  64. node *temp = H, *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL;
  65. while (temp->next->next != NULL)
  66. {
  67. i = 0;
  68. if (H->value>H->next->value)
  69. {
  70. i++;
  71. tmp = H;
  72. tmp2 = H->next;
  73. tmp3 = H->next->next;
  74. tmp->next = tmp3;
  75. tmp2->next = tmp;
  76. H = tmp2;
  77. temp = H;
  78. }
  79.  
  80. while (temp->next->next != NULL)
  81. {
  82. if (temp->next->value>temp->next->next->value)
  83. {
  84. i++;
  85. tmp = temp->next;
  86. tmp2 = temp->next->next;
  87. tmp3 = temp->next->next->next;
  88. tmp->next = tmp3;
  89. tmp2->next = tmp;
  90. temp->next = tmp2;
  91. temp = temp->next;
  92. }
  93. else
  94. temp = temp->next;
  95. }
  96. if (i != 0)
  97. temp = H;
  98.  
  99. }
  100. }
  101. }
  102. void lacz(node *&p, node **&tab)
  103. {
  104. node* temp = NULL;
  105. temp = tab[0];
  106. for (int i = 0; i < N / 100; i++)
  107. {
  108.  
  109.  
  110. while (temp->next != NULL)
  111. {
  112. temp = temp->next;
  113. }
  114. temp->next = tab[i + 1];
  115. if (i == (N / 100) - 1)
  116. temp->next = NULL;
  117.  
  118.  
  119. }
  120. p = tab[0];
  121. }
  122.  
  123. int main()
  124. {
  125. clock_t s, f;
  126. double czas=0.0, czas2=0.0;
  127. node *p = NULL;
  128. int x = N / 100;
  129. node **tab = new node*[x];
  130. for (int i = 0; i < x; i++)
  131. {
  132. tab[i] = NULL;
  133.  
  134. }
  135. srand(time(NULL));
  136. node * H = NULL, *H2=NULL;
  137. for (int i = 0; i < N; i++)
  138. {
  139. ADD(H, rand() % 10000);
  140. ADD(H2, rand() % 10000);
  141. }
  142.  
  143.  
  144. //show(H);
  145. s = clock();
  146. sort(H, tab);
  147.  
  148. for (int i = 0; i < x; i++)
  149. {
  150. bubblesort(tab[i]);
  151. //cout << "DB[" << i << "]->";
  152. //show(tab[i]);
  153.  
  154. }
  155. lacz(p, tab);
  156. f = clock();
  157.  
  158. czas += (double)(f - s) / CLOCKS_PER_SEC;
  159. s = clock();
  160. bubblesort(H2);
  161. f = clock();
  162. czas2 += (double)(f - s) / CLOCKS_PER_SEC;
  163.  
  164.  
  165. //show(p);
  166. cout << czas << endl;
  167. cout << czas2 << endl;
  168.  
  169.  
  170.  
  171.  
  172.  
  173. system("pause");
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement