title: Java Shell Sort Algorithm Causing Infinite Loop: What Am I doing Wrong?
body: <p>Ok so I'm trying to write a basic shell sort algorithm for an assignment. The goal is so sort an array of random integers. The plan is:
  In the first pass, the gap is half the size of the array. For each subsequent pass, the gap size is cut in half. For the final pass(es), the gap size is 1, so it would be the same as a bubble sort. The passes continue until no swaps occur. However, I'm getting an infinite loop. Can anyone see what the problem is? </p>
  
  <p>Here is both versions of my method and the service method it uses for swapping:</p>
  
  <pre><code>/**************************************************************************************************************************************************
      //
      //The first version of shellSort calls the second version with min value as 0 and max as length of randArray-1. Takes no parameters.
      //
  ***************************************************************************************************************************************************/
  public void shellSort()
  {
      shellSort(0, randArray.length-1);   
  
  }
  
  
  /**************************************************************************************************************************************************
  //
  // shellSort which takes min and max parameters. Calculates gap at center, across which values are compared. Passes continue until gap size is 1
  // and array is sorted.
  // Uses boolean sorted to indicate when array is sorted so passes don't continue needelessly after array is sorted. Essentially, if no values
  // are swapped after a pass, we know array is sorted and sorted is not set to false.
  //
  // Outer for loop controls position of final value. Since largest value is bubbled to end, position decreases by 1 after each pass.
  // After each pass, size of gap is cut in half, as long as gap is 2 or greater. Otherwise gap would become too small.
  // Inner for loop controls the index values to be compared.
  // Uses swap method to swap values which are not in the correct order.
  // Array is printed after each pass.
  //
  ***************************************************************************************************************************************************/
  
  public void shellSort(int min, int max)
  {
      String result;
      int gap;
      int j = 0;
      int size = randArray.length-1;
      boolean swapped;
  
  
  
      for(gap = size/2; gap &lt;= 0; gap = gap/2)
      {
        swapped = true;
  
        while (swapped)
          {   
              swapped = false;
              int comp;
  
              for(comp = 0; comp+gap &lt;= size; comp++)
              {
  
              if (randArray[comp] &gt; randArray[comp+gap])
                  {
                   swap(comp, comp+gap);
                   swapped = true;        //swapped set to true if any element is swapped with another.
                  }
              else
                  swapped = false;
              }
  
          }
              result ="";
              for(int y = 0; y &lt; randArray.length; y++)
                  {
                  result += randArray[y] +" ";
                  j++;
                  }
  
              System.out.println("Pass " +j+": " +result+"\n");
       }
  
  }
  
      /**************************************************************************************************************************************************
          //
          // Swaps two values in the array.
          //
          ***************************************************************************************************************************************************/
  
  
          private void swap(int index1, int index2)
          {
              int temp = randArray[index1];
              randArray[index1] = randArray[index2];
              randArray[index2] = temp;
          }
  </code></pre>