title: Sorting Integer Array (Using My Own Algorithm)- Java
body: <p>I'm doing an extra credit assignment for my programming class. We're doing OOP and this example is to take an array and sort it. All the sorting code will go into the add method. I have some code already, and it works sort of.  As said in the title I have to create my own algorithm with my unique code. How do I go about sorting all the integers in a list from smallest to largest? (The integers being the values added with the add method.) Here's the instructions the instructor gave me:</p>
  
  <p>• Walk down the array until you find the place where the new element should go. Since the list is already
  sorted you can just keep looking at elements until you find one that is at least as big as the one to be
  inserted.</p>
  
  <p>• Move down every element that will go after the new element, that is, everything from the one you stop
  on to the end. This creates a slot in which you can put the new element. Be careful about the order in
  which you move them or you'll overwrite your data!</p>
  
  <p>Now you can insert the new element in the location you originally stopped on. All of this will go into your add method.</p>
  
  <p>The two classes of the program:</p>
  
  <pre><code>   private int[] list;
     private int numElements = 0;
  
  
  
     //-------------------------------------------------------------
     // Constructor -- creates an integer list of a given size.
     //-------------------------------------------------------------
     public IntList(int size)
     {
        list = new int[size];
     }
     //------------------------------------------------------------
     // Adds an integer to the list. If the list is full,
     // prints a message and does nothing.
     //------------------------------------------------------------
  
     public void add(int value)
     {
        if (numElements == list.length){
        System.out.println("Can't add, list is full");
        }
        else
        { 
          list[numElements] = value;
          for(int i = 0; i &lt; numElements; i++){
              //May be I should use a nested loop?
              //for(k = 0; k &lt;)
               if(value &lt; list[i]){
                   list[i+1]= list[i];
                   list[i]=value;
               }
         }
          numElements++;
  
              }
  
  
        }
  
  
  
     //-------------------------------------------------------------
     // Returns a string containing the elements of the list with their
     // indices.
     //-------------------------------------------------------------
     public String toString()
     {
        String returnString = "";
        for (int i=0; i&lt;numElements; i++)
           returnString += i + ": " + list[i] + "\n";
        return returnString;
     }
  }
  </code></pre>
  
  <p>The other class:</p>
  
  <pre><code>public class IntListThing {
  
  
  public static void main(String[] args){
  
            IntList myList = new IntList(10);
            myList.add(84);
            myList.add(27);
            myList.add(250);
            myList.add(18);
            myList.add(94);
            myList.add(8);
            myList.add(87);
            System.out.println(myList);
         }
      }
  </code></pre>