Guest User

Untitled

a guest
Jul 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1.  
  2. /**
  3. *
  4. * @author Timothy
  5. */
  6. public class SortedArray {
  7. public int size;
  8. public int increment;
  9. public int top;
  10. Comparable[] a = new Comparable [size];
  11.  
  12. public SortedArray(int initialSize, int incrementAmount)
  13. {
  14. top = -1;
  15. size = initialSize;
  16. increment = incrementAmount;
  17.  
  18. }
  19. public int appropriatePosition(Comparable value)
  20. {
  21. int hold = 0;
  22. if(top == -1)
  23. {
  24. return 0;
  25. }
  26. else
  27. {
  28. for(int i = 0; i <= top; i++)
  29. {
  30. if(a[i].compareTo(value) > 0)
  31. {
  32. hold = i;
  33. break;
  34. }
  35. }
  36. }
  37. return hold;
  38. }
  39. public Comparable smallest()
  40. {
  41. int min = 0;
  42. for(int i = 0; i < top; i++)
  43. {
  44. if( a[i].compareTo(a[i + 1]) <= 0)
  45. {
  46. min = i;
  47. }
  48. }
  49. return a[min];
  50.  
  51. }
  52. public Comparable largest()
  53. {
  54. int max = 0;
  55. for(int i = 0; i < top; i++)
  56. {
  57. if( a[i].compareTo(a[i + 1]) >= 0)
  58. {
  59. max = i;
  60. }
  61. }
  62. return a[max];
  63.  
  64. }
  65. public void insert(Comparable value)
  66. {
  67. //Shifting numbers to the top
  68. if(full() == true)
  69. {
  70. Comparable[] tempArray = new Comparable[top + increment];
  71. for(int i= 0; i< size; i++)
  72. {
  73. tempArray[i]= a[i];
  74. a = tempArray;
  75. }
  76. size = top + increment;
  77. }
  78. if(a[appropriatePosition(value) + 1] != null)
  79. {
  80. for(int i = top; i < appropriatePosition(value); i--)
  81. {
  82. a[i + 1] = a[i];
  83. }
  84. }
  85. a[appropriatePosition(value) + 1]= value;
  86.  
  87. }
  88. public int find(Comparable f)
  89. {
  90. for(int i = 0; i < top; i++)
  91. {
  92. if(a[i] == f)
  93. {
  94. return i;
  95. }
  96. }
  97. return -1;
  98. }
  99. public void delete(Comparable d)
  100. {
  101. if(top > -1)
  102. {
  103. a[find(d)] = a[find(d) + 1];
  104. for(int i = find(d); i < size; i++)
  105. {
  106. a[i] = a[i+1];
  107. }
  108. top--;
  109. Comparable[] tempArray = new Comparable[top];
  110. for(int i= 0; i< size; i++)
  111. {
  112. tempArray[i]= a[i];
  113. a = tempArray;
  114. }
  115. size = top - 1;
  116. }
  117. else
  118. {
  119. System.out.println("Array is already empty.");
  120. }
  121.  
  122. }
  123. public void print()
  124. {
  125. for(int i = 0; i < size; i++)
  126. {
  127. System.out.println(a[i]);
  128. }
  129. }
  130. public void clear()
  131. {
  132. for(int i = 0; i < size; i++)
  133. {
  134. a[i] = null;
  135. }
  136. }
  137. public boolean full()
  138. {
  139. for(int i = 0; i <= size; i++)
  140. {
  141. if(a[i] != null)
  142. {
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. public boolean empty()
  149. {
  150. for(int i = 0; i < size; i++)
  151. {
  152. if(a[i] != null)
  153. {
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. public int compareTo(Object obj)
  160. {
  161. Comparable x = (Comparable) obj;
  162. if(this.compareTo(obj) > 0)
  163. {
  164. return 1;
  165. }
  166. else if(this.compareTo(obj) == 0)
  167. {
  168. return 0;
  169. }
  170. else if(this.compareTo(obj) < 0)
  171. {
  172. return -1;
  173. }
  174. return 0;
  175. }
  176.  
  177. }
Add Comment
Please, Sign In to add comment