fork download
  1. import java.util.Random;
  2.  
  3. class BasicOperations
  4. {
  5. /** Array of integers to be used in several basic operations. */
  6. private int [] v;
  7. /** Number of valid elements stored in the array v. */
  8. private int n;
  9.  
  10. public BasicOperations(int size)
  11. {
  12. v = new int [size];
  13. n = 0;
  14. }
  15.  
  16. public int capacity() { return v.length; }
  17. public int size() { return n; }
  18. public void reset() { n = 0; }
  19.  
  20. @Override
  21. public String toString()
  22. {
  23.  
  24. sb.append(String.format("array size %3d: ", n));
  25. sb.append("{");
  26. if (n > 0) sb.append(v[0]);
  27. for (int i = 1; i < n; i++) sb.append(", " + v[i]);
  28. sb.append("}");
  29. return sb.toString();
  30. }
  31.  
  32. public static String toString(int [] a)
  33. {
  34.  
  35. sb.append("{");
  36. if (a.length > 0) sb.append(a[0]);
  37. for (int i = 1; i < a.length; i++) sb.append(", " + a[i]);
  38. sb.append("}");
  39. return sb.toString();
  40. }
  41.  
  42. public static int [] genRandomArray(int size)
  43. {
  44. int [] v = new int [size];
  45. Random r = new Random();
  46.  
  47. for (int i = 0; i < v.length; i++)
  48. v[i] = r.nextInt(100);
  49.  
  50. return v;
  51. }
  52.  
  53. /**
  54.   * Returns the index of the first position
  55.   * of x in the array, -1 if it does not
  56.   * exist in the array.
  57.   *
  58.   * @param x The value to be found in the array.
  59.   */
  60. public int find(int x)
  61. {
  62. int i = 0;
  63.  
  64. while (i < n && v[i] != x) i++;
  65.  
  66. return (i < n) ? i : -1;
  67. }
  68.  
  69. /**
  70.   * Removes the element at the given position.
  71.   *
  72.   * @param pos Index of the position whose element must be removed.
  73.   */
  74. public void removeAtPos(int pos)
  75. {
  76. if (0 <= pos && pos < n) {
  77. for (int i = pos; i < n; i++) v[i] = v[i + 1];
  78. --n;
  79. }
  80. }
  81.  
  82. /**
  83.   * Removes the given value if contained in the array.
  84.   *
  85.   * @param x The value to be removed from the array if it exists.
  86.   */
  87. public void remove(int x)
  88. {
  89. int pos = find(x);
  90. if (pos >= 0) removeAtPos(pos);
  91. }
  92.  
  93. /**
  94.   * Adds a new element in the array after the stored elements,
  95.   * i.e., in the first free position.
  96.   *
  97.   * @param x New value to be stored in the array.
  98.   */
  99. public void add(int x)
  100. {
  101. if (n == v.length) increaseSize(10);
  102.  
  103. v[n++] = x;
  104. }
  105.  
  106. /**
  107.   * Inserts a new element in the array at the specified position.
  108.   *
  109.   * @param x New value to be stored in the array.
  110.   * @param pos Position at the new element must be inserted.
  111.   */
  112. public void insert(int x, int pos)
  113. {
  114. if (n == v.length) increaseSize(10);
  115.  
  116. // Move all the existing elements from pos to n one position to the right.
  117. for (int i = n; i > pos; i--) v[i] = v[i - 1];
  118.  
  119. // Now, position pos is free and x can be stored in such position.
  120. v[pos] = x;
  121. }
  122.  
  123. /**
  124.   * Inserts a new element in the array at the corresponding position in order
  125.   * to guarantee the array remains sorted in ascending order.
  126.   * Precondition, the array is already sorted in ascending order.
  127.   *
  128.   * @param x New value to be stored in the array.
  129.   */
  130. public void insertInOrder(int x)
  131. {
  132. if (n == v.length) increaseSize(10);
  133.  
  134. int i = n - 1;
  135. // Shifting one position to the right all the values in v greater than x.
  136. while (i >= 0 && v[i] > x) {
  137. v[i + 1] = v[i];
  138. i--;
  139. }
  140. v[i + 1] = x;
  141. n++;
  142. }
  143.  
  144. /**
  145.   * Increases the size of the array, but not the number
  146.   * of elements stored in it.
  147.   *
  148.   * @param s Number of new positions to be available in the array.
  149.   */
  150. public void increaseSize(int s)
  151. {
  152. // Creation of a new array larger than the existing one.
  153. int [] newArray = new int [v.length + s];
  154.  
  155. // Copying the contents of the existing array to the new one.
  156. for (int i = 0; i < n; i++) newArray[i] = v[i];
  157.  
  158. // The new array substitutes the existing one.
  159. v = newArray;
  160. }
  161.  
  162. /**
  163.   *
  164.   */
  165. public static void main(String [] args)
  166. {
  167. BasicOperations bo = new BasicOperations(100);
  168. System.out.println("Capacity = " + bo.capacity() + ", size = " + bo.size());
  169.  
  170. int [] temp = BasicOperations.genRandomArray(10);
  171.  
  172. BasicOperations.toString(temp);
  173.  
  174. for (int i = 0; i < temp.length; i++) {
  175. bo.add(temp[i]);
  176. System.out.println(bo);
  177. }
  178.  
  179. bo.reset();
  180. System.out.println("\nAfter the reset: ");
  181. System.out.println(bo);
  182. System.out.println();
  183.  
  184. for (int i = 0; i < temp.length; i++) {
  185. bo.insertInOrder(temp[i]);
  186. System.out.println(bo);
  187. }
  188. System.out.println("\nLet's remove the elements: ");
  189. for (int i = 0; i < temp.length; i++) {
  190. System.out.println(" removing " + temp[i]);
  191. System.out.println(bo);
  192. bo.remove(temp[i]);
  193. System.out.println(bo);
  194. }
  195. }
  196. }
Success #stdin #stdout 0.18s 50996KB
stdin
add $s0, $t2, $s0
stdout
Capacity = 100, size = 0
array size   1: {0}
array size   2: {0, 43}
array size   3: {0, 43, 12}
array size   4: {0, 43, 12, 9}
array size   5: {0, 43, 12, 9, 0}
array size   6: {0, 43, 12, 9, 0, 69}
array size   7: {0, 43, 12, 9, 0, 69, 2}
array size   8: {0, 43, 12, 9, 0, 69, 2, 92}
array size   9: {0, 43, 12, 9, 0, 69, 2, 92, 69}
array size  10: {0, 43, 12, 9, 0, 69, 2, 92, 69, 33}

After the reset: 
array size   0: {}

array size   1: {0}
array size   2: {0, 43}
array size   3: {0, 12, 43}
array size   4: {0, 9, 12, 43}
array size   5: {0, 0, 9, 12, 43}
array size   6: {0, 0, 9, 12, 43, 69}
array size   7: {0, 0, 2, 9, 12, 43, 69}
array size   8: {0, 0, 2, 9, 12, 43, 69, 92}
array size   9: {0, 0, 2, 9, 12, 43, 69, 69, 92}
array size  10: {0, 0, 2, 9, 12, 33, 43, 69, 69, 92}

Let's remove the elements: 
    removing 0
array size  10: {0, 0, 2, 9, 12, 33, 43, 69, 69, 92}
array size   9: {0, 2, 9, 12, 33, 43, 69, 69, 92}
    removing 43
array size   9: {0, 2, 9, 12, 33, 43, 69, 69, 92}
array size   8: {0, 2, 9, 12, 33, 69, 69, 92}
    removing 12
array size   8: {0, 2, 9, 12, 33, 69, 69, 92}
array size   7: {0, 2, 9, 33, 69, 69, 92}
    removing 9
array size   7: {0, 2, 9, 33, 69, 69, 92}
array size   6: {0, 2, 33, 69, 69, 92}
    removing 0
array size   6: {0, 2, 33, 69, 69, 92}
array size   5: {2, 33, 69, 69, 92}
    removing 69
array size   5: {2, 33, 69, 69, 92}
array size   4: {2, 33, 69, 92}
    removing 2
array size   4: {2, 33, 69, 92}
array size   3: {33, 69, 92}
    removing 92
array size   3: {33, 69, 92}
array size   2: {33, 69}
    removing 69
array size   2: {33, 69}
array size   1: {33}
    removing 33
array size   1: {33}
array size   0: {}