staafl

cpp exercise

Nov 5th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int isMonotoneIncreasing(int numbers[], int count)
  5. {
  6. if (count == 0) return 1;
  7. int i = 0;
  8. while(i < count - 1 &&
  9. numbers[i] <= numbers[i+1])
  10. {
  11. i += 1;
  12. }
  13. return i == count - 1;
  14. }
  15.  
  16. int scalarProduct(int a[], int b[], int count)
  17. {
  18. int sum = 0;
  19. for(int i = 0; i < count; ++i)
  20. {
  21. sum += a[i] * b[i];
  22. }
  23. return sum;
  24. }
  25. int longestNonDecreasingLength(int numbers[], int arrayLength)
  26. {
  27. int maxCount = 1;
  28. int currentCount = 1;
  29. for(int i = 0; i < arrayLength - 1; ++i)
  30. {
  31. if(numbers[i] <= numbers[i+1]) {
  32. currentCount += 1;
  33. maxCount = max(maxCount, currentCount);
  34. }
  35. else {
  36. currentCount = 0;
  37. }
  38. }
  39. currentCount += 1;
  40. maxCount = max(maxCount, currentCount);
  41. return maxCount;
  42.  
  43. }
  44. bool isSubset(int a[], int countA, int b[], int countB)
  45. {
  46. for(int i = 0; i < countA; ++i)
  47. {
  48. bool found = false;
  49. for(int j = 0; j < countB; ++j)
  50. {
  51. if (a[i] == b[j])
  52. {
  53. found = true;
  54. break;
  55. }
  56. }
  57. if (!found)
  58. return false;
  59. }
  60. return true;
  61. }
  62.  
  63. int sameElements(int a[], int countA, int b[], int countB)
  64. {
  65. return isSubset(a, countA, b, countB) &&
  66. isSubset(b, countB, a, countA);
  67. }
  68.  
  69. int subSequenceCount(int sequence[], int sequenceCount,
  70. int subSequence[], int subSequenceCount)
  71. {
  72. if (subSequenceCount > sequenceCount)
  73. return 0;
  74.  
  75. int count = 0;
  76. int j = 0;
  77. for(int i = 0; i < sequenceCount - subSequenceCount; ++i)
  78. {
  79. for(int j = 0; j < subSequenceCount; ++j)
  80. {
  81. if (sequence[i+j] == subSequence[j])
  82. {
  83. if (j == subSequenceCount - 1)
  84. {
  85. count += 1;
  86. }
  87. }
  88. else
  89. {
  90. j = subSequenceCount;
  91. }
  92. }
  93. }
  94. return count;
  95. }
  96.  
  97. void bubbleSort(int a[], int count)
  98. {
  99. int k = 0;
  100. int pass = 0;
  101. while(count > 1)
  102. {
  103. k = 0;
  104. for(int i = 0; i < count - 1; ++i)
  105. {
  106. if(a[i] > a[i+1])
  107. {
  108. swap(a[i], a[i+1]);
  109. k=i;
  110. }
  111. }
  112. count = k + 1;
  113. }
  114. }
  115.  
  116. int binSearch(int a[], int count, int elem)
  117. {
  118. if (count == 0)
  119. return -1;
  120. int left = -1;
  121. int right = count;
  122. int middle = 0;
  123. while(right - left > 1 &&
  124. a[middle] != elem)
  125. {
  126. middle = (left + right) / 2;
  127. if (a[middle] > elem) right = middle;
  128. else left = middle;
  129. }
  130. if (a[middle] == elem)
  131. return middle;
  132. return -1;
  133.  
  134. int l=0,r=count-1;
  135. while(l<=r)
  136. {
  137. middle=(l+r)/2;
  138. if (elem<a[middle]) r=middle-1;
  139. else if (elem>a[middle]) l=middle+1;
  140. else if (elem==a[middle]) return 1;
  141. else return -1;
  142. }
  143.  
  144. }
  145.  
  146. void merge(int a[],
  147. int left, int countLeft,
  148. int right, int countRight,
  149. int buf[])
  150. {
  151. int bufIndex = 0;
  152. int leftStart = left;
  153. while(true)
  154. {
  155. if (countLeft <= 0)
  156. {
  157. if (countRight <= 0)
  158. break;
  159. buf[bufIndex] = a[right];
  160. right++;
  161. bufIndex++;
  162. countRight--;
  163. }
  164. else if (countRight <= 0)
  165. {
  166. buf[bufIndex] = a[left];
  167. left++;
  168. bufIndex++;
  169. countLeft--;
  170. }
  171. else if (a[left] < a[right])
  172. {
  173. buf[bufIndex] = a[left];
  174. left++;
  175. bufIndex++;
  176. countLeft--;
  177. }
  178. else
  179. {
  180. buf[bufIndex] = a[right];
  181. right++;
  182. bufIndex++;
  183. countRight--;
  184. }
  185. }
  186. for(int i = 0; i < bufIndex; ++i)
  187. {
  188. a[leftStart+i] = buf[i];
  189. }
  190. }
  191.  
  192. void mergeSort(int a[], int start, int count, int buf[])
  193. {
  194. if (count <= 1)
  195. return;
  196.  
  197. mergeSort(a, start, count / 2, buf);
  198. mergeSort(a, start + count/2, count/2 + count%2, buf);
  199. merge(a, start, count/2, start+count/2, count/2+ count%2, buf);
  200. }
  201.  
  202.  
  203.  
  204. int main()
  205. {
  206. int a[] = {1, 2, 3, 4, 5, 6};
  207. int b[] = {1, 4, 3, 4, 5, 6, 2};
  208. int k[] = {1,2,1,3,2,1};
  209. int l[] = {2,1};
  210. int binSearchArray[] = {1,1,2,2,3,4,7,8,9,9,10};
  211. int aa[] = {'a','a'};
  212. int aaaaa[] = {'a','a','a','a','a'};
  213. cout<<"isMonotoneIncreasing a: "<<isMonotoneIncreasing(a, 6)<<endl;
  214. cout<<"isMonotoneIncreasing b: "<<isMonotoneIncreasing(b, 6)<<endl;
  215. cout<<"scalarProduct a*b: "<<scalarProduct(a, b, 6)<<endl;
  216. cout<<"longestNonDecreasing a,b: "<<longestNonDecreasingLength(b, 7)<<endl;
  217. cout<<"sameElements a,b: "<<sameElements(a, 6, b, 7)<<endl;
  218. cout<<"subSequenceCount [1,2,1,3,2,1],[2,1]: "<<
  219. subSequenceCount(k, 6, l, 2)<<endl;
  220. cout<<"subSequenceCount [a,a,a,a,a],[a,a]: "<<
  221. subSequenceCount(aaaaa, 5, aa, 2)<<endl;
  222. bubbleSort(k, 6);
  223. cout<<"bubbleSort of [1,2,1,3,2,1]: ";
  224. for(int i = 0; i < 6; ++i)
  225. cout<<k[i]<<" ";
  226. cout<<endl;
  227. cout<<"binSearch of 2 in [1,1,2,2,3,4,7,8,9,9,10]: "<<binSearch(binSearchArray, 11, 2)<<endl;
  228. cout<<"binSearch of 4 in [1,1,2,2,3,4,7,8,9,9,10]: "<<binSearch(binSearchArray, 11, 4)<<endl;
  229. cout<<"binSearch of 5 in [1,1,2,2,3,4,7,8,9,9,10]: "<<binSearch(binSearchArray, 11, 5)<<endl;
  230.  
  231. int mergeSortArray[] = {1, 5, 2, 3, 6, 1, 3, 2, 9 };
  232. int mergeSortBuf[9];
  233.  
  234. mergeSort(mergeSortArray, 0, 9, mergeSortBuf);
  235. cout<<"mergeSort of of 5 in [1, 5, 2, 3, 6, 1, 3, 2, 9]: "<<endl;
  236. for(int i = 0; i < 9; ++i)
  237. cout<<mergeSortArray[i]<<" ";
  238. cout<<endl;
  239. //std::vector<double> kAsVector = new std::vector
  240.  
  241.  
  242. }
Advertisement
Add Comment
Please, Sign In to add comment