Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include "algorithm.h"
  2.  
  3.  
  4. int worst_sorterad(int *a, int n){
  5.  
  6.  
  7. int i, j;
  8.  
  9. int sorterad = 1;
  10.  
  11. for (i = 0;i < n - 1; ++i) {
  12.  
  13. for (j = i + 1; j < n; ++j) {
  14. if (a[i] < a[j]) {
  15.  
  16.  
  17. sorterad = 0;
  18. }
  19.  
  20. }
  21. }
  22.  
  23. if(sorterad == 1){
  24. return 1;
  25. }
  26. else{
  27. return 0;
  28. }
  29. }
  30.  
  31. int best_sorterad(int *a, int n){
  32.  
  33.  
  34. int i, j;
  35.  
  36. int sorterad = 1;
  37.  
  38. for (i = 0;i < n - 1; ++i) {
  39.  
  40. for (j = i + 1; j < n; ++j) {
  41. if (a[i] > a[j]) {
  42.  
  43.  
  44. sorterad = 0;
  45. }
  46.  
  47. }
  48. }
  49.  
  50. if(sorterad == 1){
  51. return 1;
  52. }
  53. else{
  54. return 0;
  55. }
  56. }
  57.  
  58.  
  59. void bubble_sort(int *a, int n)
  60. {
  61. int i, j;
  62. int flagga = 0;
  63.  
  64. for (i = 0; !flagga && i < n - 1; ++i) {
  65. flagga = 1;
  66. for (j = i + 1; j < n; ++j) {
  67. if (a[i] > a[j]) {
  68. int t = a[i];
  69. a[i] = a[j];
  70. a[j] = t;
  71. flagga = 0;
  72. }
  73.  
  74. }
  75. }
  76. }
  77.  
  78.  
  79.  
  80. void insertion_sort(int *a, int n) {
  81.  
  82. int i,j,key;
  83. for(j=2;j<n;j++){
  84. key = a[j];
  85. i = j - 1;
  86.  
  87. while(i >= 0 && a[i] > key){
  88. a[i + 1] = a[i];
  89. i = i - 1;
  90. }
  91. a[i + 1] = key;
  92. }
  93. }
  94.  
  95.  
  96.  
  97.  
  98. void quick_sort_best(int *a, int n){
  99. if (n < 2)
  100. return;
  101.  
  102. int pivot = a[n/2];
  103. int *left = a;
  104. int *right = a + n - 1;
  105. while (left <= right) {
  106. if (*left < pivot) {
  107. left++;
  108. }
  109. else if (*right > pivot) {
  110. right--;
  111. }
  112. else {
  113. int temp = *left;
  114. *left = *right;
  115. *right = temp;
  116. left++;
  117. right--;
  118. }
  119. }
  120. quick_sort_best(a, right - a + 1);
  121. quick_sort_best(left, a + n - left);
  122.  
  123.  
  124. }
  125.  
  126. void quick_sort_worst(int *a, int n){
  127.  
  128. if (n < 2)
  129. return;
  130.  
  131.  
  132.  
  133. int pivot = a[n - 1];
  134. int *left = a;
  135. int *right = a + n - 1;
  136. while (left <= right) {
  137. if (*left < pivot) {
  138. left++;
  139. }
  140. else if (*right > pivot) {
  141. right--;
  142. }
  143. else {
  144. int temp = *left;
  145. *left = *right;
  146. *right = temp;
  147. left++;
  148. right--;
  149. }
  150. }
  151. quick_sort_worst(a, right - a + 1);
  152. quick_sort_worst(left, a + n - left);
  153.  
  154.  
  155. }
  156.  
  157. void quick_sort_average(int *a, int n){
  158.  
  159. if (n < 2)
  160. return;
  161.  
  162.  
  163.  
  164. int pivot = a[n/2];
  165. int *left = a;
  166. int *right = a + n - 1;
  167. while (left <= right) {
  168. if (*left < pivot) {
  169. left++;
  170. }
  171. else if (*right > pivot) {
  172. right--;
  173. }
  174. else {
  175. int temp = *left;
  176. *left = *right;
  177. *right = temp;
  178. left++;
  179. right--;
  180. }
  181. }
  182. quick_sort_average(a, right - a + 1);
  183. quick_sort_average(left, a + n - left);
  184.  
  185.  
  186. }
  187.  
  188.  
  189. void quick_sort(int *a, int n)
  190. {
  191.  
  192. if(worst_sorterad(a,n) == 1)
  193. {
  194. quick_sort_worst(a, n);
  195. }
  196. else if(best_sorterad(a,n) == 1){
  197. quick_sort_best(a, n);
  198. }
  199. else
  200. {
  201. quick_sort_average(a,n);
  202. }
  203. // if (n < 2)
  204. // return;
  205. //
  206. // int pivot = a[n - 1];
  207. // int *left = a;
  208. // int *right = a + n - 1;
  209. // while (left <= right) {
  210. // if (*left < pivot) {
  211. // left++;
  212. // }
  213. // else if (*right > pivot) {
  214. // right--;
  215. // }
  216. // else {
  217. // int temp = *left;
  218. // *left = *right;
  219. // *right = temp;
  220. // left++;
  221. // right--;
  222. // }
  223. // }
  224. // quick_sort(a, right - a + 1);
  225. // quick_sort(left, a + n - left);
  226.  
  227. }
  228.  
  229.  
  230. bool linear_search(const int *a, int n, int v)
  231. {
  232. int i;
  233. for(i = 0;i < n;i++){
  234. if(a[i] == v)
  235. return true;
  236. }
  237. return false;
  238. }
  239.  
  240. bool binary_search(const int *a, int n, int v){
  241.  
  242.  
  243. int low = 0;
  244. int high = n - 1;
  245. int mid;
  246. int found = 0;
  247. while (low <= high && !found)
  248. {
  249. mid = ( high - low ) / 2;
  250. if(a[mid] == v){
  251. found = 1;
  252. }
  253. else if( a[mid] > v){
  254. high = mid - 1;
  255.  
  256. }else{
  257. low = mid + 1;
  258.  
  259. }
  260. if(found == 1){
  261. return true;
  262. }else{
  263. return false;
  264. }
  265.  
  266.  
  267. }
  268. return false;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement