Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**OrderedIntList creates an integer array and
- * sorts the elements into ascending order without
- * duplicates
- *
- * @author Dustin Rosebery
- * PIN: 721
- */
- // works for all integers != 0
- package cse360assign1;
- public class OrderedIntList
- {
- /** Array is the array created to hold the integer values
- * Counter keeps a running total of the elements inside the array */
- private int[] array;
- private int counter = 0;
- /**OrderedIntList initializes the array */
- public OrderedIntList ()
- {
- array = new int[10];
- }
- /** insert is the method used to sort the values inside of the array
- * and then insert the value at the correct index
- * @param value
- * is the actual integer value of the integer to be placed into the array
- */
- public void insert (int value)
- {
- int index = 0;
- int insertIndex = 0;
- boolean insertIndexFound = false;
- boolean insertThisValue = true;
- for (index = 0; index <= 9; index++) // Checks for duplicate values
- {
- if (value == array[index])
- insertThisValue = false;
- }
- index = 0;
- while (index <= 9 && !insertIndexFound && insertThisValue)
- {
- if (value < array[index] || array[index] == 0)
- {
- insertIndex = index;
- //System.out.println("The insertIndex is: " + insertIndex + " "
- // + "The value is: " + value); // debug
- insertIndexFound = true;
- if( counter < 10)
- counter++;
- }
- else
- index++;
- }
- boolean continueSwapping = true;
- int swapCounter = counter - 2;
- index = 0;
- while (swapCounter > insertIndex && continueSwapping && insertIndexFound && insertThisValue)
- {
- if (swapCounter != 10 && swapCounter != 0 && array[index] != 0)
- {
- index = swapCounter - 1;
- //System.out.println ("Value: " + array[index] +
- // " is going to array index: " + (index + 1) ); // debug
- //System.out.println ("swapCounter: " + swapCounter +
- // " " + "Index: " + index); // debug
- array[index + 1] = array[index];
- swapCounter = swapCounter - 1;
- }
- else
- {
- continueSwapping = false;
- }
- }
- if (insertThisValue)
- array[insertIndex] = value;
- }
- /** Print correctly formats and displays the elements of the array */
- public void print ()
- {
- for (int index = 0; index <= counter; index++)
- {
- if (index % 5 == 0)
- System.out.println();
- if( index < 10 && array[index] != 0)
- System.out.print (array[index] + "\t");
- }
- //System.out.println("Finished Print Iterations"); // debug
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement