Advertisement
DustinRosebery

Assign1_360_v1

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