Advertisement
DustinRosebery

assign1_v2

Jan 26th, 2016
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. /**OrderedIntList creates an integer array and
  2. * sorts the elements into ascending order without
  3. * duplicates
  4. *
  5. * @author Dustin Rosebery
  6. * PIN: 721
  7. */
  8.  
  9. package cse360assign1;
  10.  
  11. public class OrderedIntList
  12. {
  13. /** Array is the array created to hold the integer values
  14. * Counter keeps a running total of the elements inside the array */
  15. private int[] array;
  16. private int counter = 0;
  17.  
  18. /**OrderedIntList initializes the array */
  19. public OrderedIntList ()
  20. {
  21. array = new int[10];
  22. }
  23.  
  24. /** insert is the method used to sort the values inside of the array of size 10
  25. * and then insert the value at the correct index. Duplicates are not inserted.
  26. * @param value
  27. * is the actual integer value of the integer to be placed into the array
  28. */
  29. public void insert (int value)
  30. {
  31. int index = 0;
  32. int insertIndex = 0;
  33. boolean insertIndexFound = false;
  34. boolean insertThisValue = true;
  35.  
  36. for (index = 0; index <= counter - 1; index++) // Checks for duplicate values
  37. {
  38. if (value == array[index])
  39. insertThisValue = false;
  40. }
  41.  
  42. index = 0;
  43.  
  44. while (index <= 9 && !insertIndexFound && insertThisValue)
  45. {
  46. if (value < array[index] || array[index] == 0)
  47. {
  48. insertIndex = index;
  49.  
  50. //System.out.println("The insertIndex is: " + insertIndex + " "
  51. // + "The value is: " + value); // debug
  52.  
  53. insertIndexFound = true;
  54.  
  55. if( counter < 10)
  56. counter = counter + 1;
  57. }
  58. else
  59. index = index + 1;
  60. }
  61.  
  62. int swapCounter = counter - 1;
  63. index = 0;
  64.  
  65. while (swapCounter > insertIndex && insertIndexFound && insertThisValue) // wont execute loop unless insertIndexFound && insertThisValue
  66. {
  67. if (swapCounter != 10)
  68. {
  69. index = swapCounter - 1;
  70.  
  71. //System.out.println ("Value: " + array[index] +
  72. // " is going to array index: " + (index + 1) ); // debug
  73. //System.out.println ("swapCounter: " + swapCounter +
  74. // " " + "Index: " + index); // debug
  75.  
  76. array[index + 1] = array[index];
  77. swapCounter = swapCounter - 1;
  78. }
  79. else
  80. {
  81. swapCounter = insertIndex;
  82. }
  83. }
  84.  
  85. if (insertThisValue)
  86. array[insertIndex] = value;
  87. }
  88.  
  89. /** Print correctly formats and displays the elements of the array
  90. * Format is 5 elements separated by a tab per line*/
  91. public void print ()
  92. {
  93. for (int index = 0; index < counter; index++)
  94. {
  95. if (index % 5 == 0)
  96. System.out.println();
  97.  
  98. if (index < 10)
  99. System.out.print (array[index] + "\t");
  100. }
  101. //System.out.println("Finished Print Iterations"); // debug
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement