import java.util.Random; class BasicOperations { /** Array of integers to be used in several basic operations. */ private int [] v; /** Number of valid elements stored in the array v. */ private int n; public BasicOperations(int size) { v = new int [size]; n = 0; } public int capacity() { return v.length; } public int size() { return n; } public void reset() { n = 0; } @Override { sb.append("{"); if (n > 0) sb.append(v[0]); for (int i = 1; i < n; i++) sb.append(", " + v[i]); sb.append("}"); return sb.toString(); } { sb.append("{"); if (a.length > 0) sb.append(a[0]); for (int i = 1; i < a.length; i++) sb.append(", " + a[i]); sb.append("}"); return sb.toString(); } public static int [] genRandomArray(int size) { int [] v = new int [size]; for (int i = 0; i < v.length; i++) v[i] = r.nextInt(100); return v; } /** * Returns the index of the first position * of x in the array, -1 if it does not * exist in the array. * * @param x The value to be found in the array. */ public int find(int x) { int i = 0; while (i < n && v[i] != x) i++; return (i < n) ? i : -1; } /** * Removes the element at the given position. * * @param pos Index of the position whose element must be removed. */ public void removeAtPos(int pos) { if (0 <= pos && pos < n) { for (int i = pos; i < n; i++) v[i] = v[i + 1]; --n; } } /** * Removes the given value if contained in the array. * * @param x The value to be removed from the array if it exists. */ public void remove(int x) { int pos = find(x); if (pos >= 0) removeAtPos(pos); } /** * Adds a new element in the array after the stored elements, * i.e., in the first free position. * * @param x New value to be stored in the array. */ public void add(int x) { if (n == v.length) increaseSize(10); v[n++] = x; } /** * Inserts a new element in the array at the specified position. * * @param x New value to be stored in the array. * @param pos Position at the new element must be inserted. */ public void insert(int x, int pos) { if (n == v.length) increaseSize(10); // Move all the existing elements from pos to n one position to the right. for (int i = n; i > pos; i--) v[i] = v[i - 1]; // Now, position pos is free and x can be stored in such position. v[pos] = x; } /** * Inserts a new element in the array at the corresponding position in order * to guarantee the array remains sorted in ascending order. * Precondition, the array is already sorted in ascending order. * * @param x New value to be stored in the array. */ public void insertInOrder(int x) { if (n == v.length) increaseSize(10); int i = n - 1; // Shifting one position to the right all the values in v greater than x. while (i >= 0 && v[i] > x) { v[i + 1] = v[i]; i--; } v[i + 1] = x; n++; } /** * Increases the size of the array, but not the number * of elements stored in it. * * @param s Number of new positions to be available in the array. */ public void increaseSize(int s) { // Creation of a new array larger than the existing one. int [] newArray = new int [v.length + s]; // Copying the contents of the existing array to the new one. for (int i = 0; i < n; i++) newArray[i] = v[i]; // The new array substitutes the existing one. v = newArray; } /** * */ { BasicOperations bo = new BasicOperations(100); int [] temp = BasicOperations.genRandomArray(10); BasicOperations.toString(temp); for (int i = 0; i < temp.length; i++) { bo.add(temp[i]); } bo.reset(); for (int i = 0; i < temp.length; i++) { bo.insertInOrder(temp[i]); } for (int i = 0; i < temp.length; i++) { bo.remove(temp[i]); } } }
add $s0, $t2, $s0
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: {}