title: Implementing a Selection Sort on an Array Based List
body: <p>My question is what am I doing wrong. The insert function doesn't work correctly. I am unable to retrieve the list itself. And because of this my selection sort won't execute. Any help would be appreciated.</p>
  
  <p>Header:</p>
  
  <pre><code>/** @file ListA.h */
  #include &lt;string&gt;
  using namespace std;
  const int MAX_LIST = 100;
  typedef string ListItemType;
  class List
  {
  
  public:
     List();
     bool isEmpty() const;
     int getLength() const;
     void insert(int index, const ListItemType&amp; newItem, bool&amp; success);
     void retrieve(int index, ListItemType&amp; dataItem, bool &amp; success) const;
     void remove(int index, bool&amp; success);
     int selectionSortArray(int numdata[], int xnums);
     List selectionSort(List selectList);
  private:
      ListItemType items[100];
      int size;
      int translate(int index) const;
  };
  </code></pre>
  
  <p>CPP:</p>
  
  <pre><code>/** @file ListA.cpp */
  #include "ArrayList.h"  // header file
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  List::List() : size()
  {
  }
  bool List::isEmpty() const
  {
     return size == 0;
  }
  int List::getLength() const
  {
     return size;
  }
  void List::insert(int index, const ListItemType&amp; newItem, bool&amp; success)
  {
     success = (index &gt;= 1) &amp;&amp;
               (index &lt;= size + 1) &amp;&amp;
               (size &lt; MAX_LIST);
     if (success)
     {
        for (int pos = size; pos &gt;= index; --pos)
  
           items[translate(pos + 1)] = items[translate(pos)];
        items[translate(index)] = newItem;
  
        ++size;
  
     }
  
  }
  void List::remove(int index, bool&amp; success)
  {
     success = (index &gt;= 1) &amp;&amp; (index &lt;= size);
     if (success)
     {
        for (int fromPosition = index + 1;
         fromPosition &lt;= size;
         ++fromPosition)
           items[translate(fromPosition - 1)] = items[translate(fromPosition)];
        --size;  // decrease the size of the list by one
     }  // end if
  
  }  // end remove
  
  void List::retrieve(int index, ListItemType&amp; dataItem,
  bool&amp; success) const
  {
     success = (index &gt;= 1) &amp;&amp; (index &lt;= size);
     if (success)
        dataItem = items[translate(index)];
  }
  
  int List::translate(int index) const
  {
     return index - 1;
  }
  
  //List List::selectionSort(List selectList)
  
  //{
  
  
  //return selectList;
  
  
  int List::selectionSortArray(int numdata[], int xnums)
  {
  int tmp;
  for (int i = 0; i &lt; xnums -1; i++)
          for (int j = i+1; j &lt; xnums; j++)
              if (numdata[i] &gt; numdata[j])
              {
                  tmp = numdata[i];
                  numdata[i] = numdata[j];
                  numdata[j] = tmp;
              }
      //for( int i = 0; i &lt; 5; i++)
          //cout &lt;&lt; numdata[i] &lt;&lt; " "; 
          return *numdata;
  }
  
   int main()
   {
   ListItemType insertType = "listItem1";
   ListItemType retrieveType = "listitem2";
   int numberofitems;
   cout &lt;&lt; "Please enter the number of data items:" &lt;&lt; endl;
   cin &gt;&gt; numberofitems;
   cout &lt;&lt; endl;
   cout &lt;&lt; "Please enter the data items, one per line:" &lt;&lt; endl;
   int listofitems[numberofitems];
   List myArrayList;
   cout &lt;&lt; myArrayList.getLength() &lt;&lt; endl;
          if (myArrayList.isEmpty())  // tests before
      {
          cout &lt;&lt; "This list is empty \n" &lt;&lt; endl;
      }
      else
      {
          cout &lt;&lt; "List is not empty! \n"&lt;&lt; endl;
      }
   bool mainsucc = true;
   for (int i = 0; i&lt;numberofitems; i++)
   {
   cout &lt;&lt; "Enter number " &lt;&lt; i+1 &lt;&lt; " : " &lt;&lt; endl;
   cin &gt;&gt; listofitems[i];
   }
   for (int i =0; i &lt;numberofitems; i++){
   myArrayList.insert(listofitems[i],  insertType, mainsucc);}
   cout &lt;&lt; "Size of the list is : " &lt;&lt; myArrayList.getLength() &lt;&lt; endl;
  int listRetrieveSize = myArrayList.getLength();
  int listRetrieve[listRetrieveSize];
  for (int j=0; j&lt;listRetrieveSize; j++)
   {
   myArrayList.retrieve(listofitems[j], retrieveType, mainsucc);
  }
  if (myArrayList.isEmpty())  // tests after
      {
          cout &lt;&lt; "This list is empty \n" &lt;&lt; endl;
      }
      else
      {
          cout &lt;&lt; "List is not empty! \n"&lt;&lt; endl;
      }
  numberofitems= myArrayList.selectionSortArray(listofitems, numberofitems);
  for (int i=0;i&gt;numberofitems;i++)
  {
  cout&lt;&lt;listofitems[i];
  }
   return 1;
   }
  </code></pre>