Guest User

Untitled

a guest
Jan 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. package Task;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Main {
  6.  
  7. /**
  8. * sorts an array of bits in a single pass (<=)
  9. * because boolean not equals to 0 or 1, type is int
  10. * @param array - array of bits
  11. * @return void
  12. */
  13. public static void bitSort(int[] array){
  14. int i=0,j=array.length-1;
  15. while(i<j){
  16. while((i<array.length)&&(array[i]==0))++i;
  17. while((j>-1)&&(array[j]==1))--j;
  18. if(i<j){
  19. int temp=array[i];
  20. array[i]=array[j];
  21. array[j]=temp;
  22. }
  23. }
  24. }
  25.  
  26. /*
  27. * use it for testing..
  28. */
  29. public static void testBitSort(){
  30. System.out.print("try testing bitSort: ");
  31. int[] test1={};
  32. bitSort(test1);
  33. int[] res1={};
  34. if(Arrays.equals(test1, res1))
  35. System.out.print('.');
  36. else
  37. System.out.print('E');
  38.  
  39. int[] test2={0,0,0};
  40. bitSort(test1);
  41. int[] res2={0,0,0};
  42. if(Arrays.equals(test2, res2))
  43. System.out.print('.');
  44. else
  45. System.out.print('E');
  46.  
  47. int[] test3={1,1,0};
  48. bitSort(test1);
  49. int[] res3={1,1,0};
  50. if(Arrays.equals(test3, res3))
  51. System.out.print('.');
  52. else
  53. System.out.print('E');
  54.  
  55. System.out.println();
  56. }
  57.  
  58. /**
  59. * @param array1
  60. * @param array2 (both without repetition in each)
  61. * @return int number of repetitive elements in both arrays, O(n log n)
  62. */
  63. public static int repeatsNum(int[] array1, int[]array2){
  64. Arrays.sort(array1);
  65. Arrays.sort(array2);
  66. int i=0, j=0, result=0;
  67. while((i<array1.length)&&(j<array2.length)){
  68. if(array1[i]==array2[j]){
  69. ++result;
  70. ++i;
  71. ++j;
  72. }else if(array1[i]>array2[j]){
  73. ++j;
  74. }else{
  75. ++i;
  76. }
  77. }
  78. return result;
  79. }
  80.  
  81. /*
  82. * use it for testing..
  83. */
  84. public static void testRepeatsNum(){
  85. System.out.print("try testing repeatsNum: ");
  86. int[] test10={1,1,1}, test11={0,0,0};
  87. int res= repeatsNum(test10,test11);
  88. if(res==0)
  89. System.out.print('.');
  90. else
  91. System.out.print('E');
  92.  
  93. int[] test20={4,2,3}, test21={3,2,1,4};
  94. res= repeatsNum(test20,test21);
  95. if(res==3)
  96. System.out.print('.');
  97. else
  98. System.out.print('E');
  99.  
  100. int[] test30={5,4,3}, test31={};
  101. res= repeatsNum(test30,test31);
  102. if(res==0)
  103. System.out.print('.');
  104. else
  105. System.out.print('E');
  106.  
  107. System.out.println();
  108. }
  109.  
  110. /*
  111. * @param matrix[0..n-1][0..m-1], что matrix[i][j]<matrix[i][j+1] && matrix[i][j]<matrix[i+1][j]
  112. * @param v - search item
  113. * begin with matrix[n-1][0]. right, if larger. up - if less. if can't go - the element is not exist in matrix
  114. */
  115. public static boolean isInMatrix(int v, int[][]matrix){
  116. int i=matrix.length-1, j=0;
  117. //пока можем идти
  118. while((i>-1)&&(j<matrix[0].length)){
  119. if(v>matrix[i][j]){
  120. ++j;
  121. }else if(v<matrix[i][j]){
  122. --i;
  123. }else{
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129.  
  130. /*
  131. * use it for testing..
  132. */
  133. public static void testIsInMatrix(){
  134. System.out.print("try testing isInMatrix: ");
  135. int[][] test1={{3,5,8,9},{13,15,17,28},{14,16,18,50}};
  136.  
  137. if(isInMatrix(0,test1)==false)
  138. System.out.print('.');
  139. else
  140. System.out.print('E');
  141.  
  142. if(isInMatrix(16,test1)==true)
  143. System.out.print('.');
  144. else
  145. System.out.print('E');
  146.  
  147. if(isInMatrix(57,test1)==false)
  148. System.out.print('.');
  149. else
  150. System.out.print('E');
  151.  
  152. if(isInMatrix(10,test1)==false)
  153. System.out.print('.');
  154. else
  155. System.out.print('E');
  156.  
  157. System.out.println();
  158. }
  159.  
  160. /*
  161. * use it for testing..
  162. */
  163. public static void testIsCorrect(){
  164. System.out.print("try testing isCorrect: ");
  165.  
  166. if(MyStack.isCorrect("")==true)
  167. System.out.print('.');
  168. else
  169. System.out.print('E');
  170.  
  171. if(MyStack.isCorrect("23rfsd")==true)
  172. System.out.print('.');
  173. else
  174. System.out.print('E');
  175.  
  176. if(MyStack.isCorrect("(((")==false)
  177. System.out.print('.');
  178. else
  179. System.out.print('E');
  180.  
  181. if(MyStack.isCorrect("23)4))")==false)
  182. System.out.print('.');
  183. else
  184. System.out.print('E');
  185.  
  186. if(MyStack.isCorrect("[][]{(2+3)*(2-3)}")==true)
  187. System.out.print('.');
  188. else
  189. System.out.print('E');
  190.  
  191. System.out.println();
  192. }
  193.  
  194. /**
  195. * @param args
  196. */
  197. public static void main(String[] args) {
  198. //start testing..
  199. testBitSort();
  200. testRepeatsNum();
  201. testIsInMatrix();
  202. testIsCorrect();
  203.  
  204.  
  205. }
  206.  
  207. }
Add Comment
Please, Sign In to add comment